123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package com.example.json;
- import com.example.utils.Utils;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonDeserializationContext;
- import com.google.gson.JsonDeserializer;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonPrimitive;
- import com.google.gson.JsonSerializationContext;
- import com.google.gson.JsonSerializer;
- import java.lang.reflect.Type;
- import java.util.Date;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class JsonFactory {
- private static Gson gson;
- private static Gson getExposeGson;
- public JsonFactory() {
- }
- public static Gson getGson() {
- if (gson == null) {
- gson = createGsonBuilder().create();
- }
- return gson;
- }
- public static Gson getExposeGson() {
- if (getExposeGson == null) {
- getExposeGson = createGsonBuilderWithExpose().create();
- }
- return getExposeGson;
- }
- public static GsonBuilder createGsonBuilder() {
- JsonBooleanAsIntTypeAdapter booleanAsIntAdapter = new JsonBooleanAsIntTypeAdapter();
- return (new GsonBuilder()).registerTypeAdapter(Boolean.class, booleanAsIntAdapter).registerTypeAdapter(Boolean.TYPE, booleanAsIntAdapter).excludeFieldsWithModifiers(new int[]{16, 128, 8}).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
- public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
- return new JsonPrimitive(src.getTime());
- }
- }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
- public Date deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
- return jsonPrimitive.isNumber() ? new Date(jsonPrimitive.getAsLong()) : Utils.getDate(jsonPrimitive.getAsString());
- }
- }).registerTypeAdapter(Double.class, new JsonDeserializer<Double>() {
- public Double deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
- return jsonPrimitive.isNumber() ? jsonPrimitive.getAsDouble() : 0.0D;
- }
- }).registerTypeAdapter(Long.TYPE, new JsonDeserializer<Long>() {
- public Long deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
- return jsonPrimitive.isNumber() ? jsonPrimitive.getAsLong() : 0L;
- }
- }).registerTypeAdapter(Long.class, new JsonDeserializer<Long>() {
- public Long deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
- return jsonPrimitive.isNumber() ? jsonPrimitive.getAsLong() : 0L;
- }
- }).registerTypeAdapter(JSONArray.class, new JsonDeserializer<JSONArray>() {
- public JSONArray deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JSONArray jsonArray = null;
- try {
- if (p1.isJsonArray()) {
- jsonArray = new JSONArray(p1.getAsJsonArray().toString());
- } else {
- jsonArray = new JSONArray(p1.getAsString());
- }
- } catch (Exception var6) {
- var6.printStackTrace();
- }
- return jsonArray;
- }
- }).registerTypeAdapter(JSONObject.class, new JsonDeserializer<JSONObject>() {
- public JSONObject deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- JSONObject jsonObject = null;
- try {
- if (p1.isJsonObject()) {
- jsonObject = new JSONObject(p1.getAsJsonObject().toString());
- } else {
- jsonObject = new JSONObject(p1.getAsString());
- }
- } catch (Exception var6) {
- var6.printStackTrace();
- }
- return jsonObject;
- }
- }).registerTypeAdapter(Object.class, new JsonDeserializer<Object>() {
- public Object deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
- return p3.deserialize(p1, p2);
- }
- });
- }
- public static GsonBuilder createGsonBuilderWithExpose() {
- return createGsonBuilder().excludeFieldsWithoutExposeAnnotation();
- }
- }
|