package com.zhilin.patrol.tool;

import android.os.Debug;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

/**
 * 
 * @ClassName: JsonUtils
 */
public class JsonUtils {

	/**
	 * json通过Gson框架生成对象
	 * 
	 * @param jsonData
	 * @param c
	 * @return
	 */
	public static <T> Object parseJson2Obj(String jsonData, Class<T> c) {
		if (null == jsonData) {
			return null;
		}
		Object obj = null;
		try {
			Gson gson = new Gson();
			obj = gson.fromJson(jsonData, c);
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		}
		return obj;
	}

	/**
	 * json通过Gson框架生成对象
	 * 
	 * @param jsonData
	 * @return
	 */
	public static <T> Object parseJson2Obj(String jsonData, TypeToken<T> type) {
		if (null == jsonData) {
			return null;
		}
		Gson gson = new Gson();
		Object obj = gson.fromJson(jsonData, type.getType());
		return obj;
	}

	/**
	 * 将java对象转换成json对象
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseObj2Json(Object obj) {

		if (null == obj) {
			return null;
		}
		Gson gson = new GsonBuilder().serializeNulls().create();
		// Gson gson = new Gson();
		String objstr = gson.toJson(obj);
		if (Debug.isDebuggerConnected()) {
			Log.i("parseObj2Json", objstr);
		}
		return objstr;
	}
	public static Object parseObjJson(String s,Class c) {

		if (null == s) {
			return null;
		}
		Gson gson = new Gson();
		Object obj = gson.fromJson(s,c);
//		Gson gson = new GsonBuilder().serializeNulls().create();
		// Gson gson = new Gson();
//		String str = gson.toJson(s);
		if (Debug.isDebuggerConnected()) {
			Log.i("parseObj2Json", obj.toString());
		}
		return obj;
	}

}
