package org.zywx.wbpalmstar.engine;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.zywx.wbpalmstar.base.BDebug;
import java.lang.reflect.Type;
/**
* Created by ylt on 2015/4/28.
*/
public class DataHelper {
public static Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
@Override
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
if (src == src.longValue())
return new JsonPrimitive(src.longValue());
return new JsonPrimitive(src);
}
}).create();
/**
* 转换成适合阅读的Json
*/
public static String toPrettyJson(String json){
try {
json = json.trim();
if (json.startsWith("{")) {
JSONObject jsonObject = new JSONObject(json);
return jsonObject.toString(4);
}
if (json.startsWith("[")) {
JSONArray jsonArray = new JSONArray(json);
return jsonArray.toString(4);
}
} catch (JSONException e) {
if (BDebug.DEBUG){
e.printStackTrace();
}
}
return json;
}
}