JsonFactory.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.example.json;
  2. import com.example.utils.Utils;
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonDeserializationContext;
  6. import com.google.gson.JsonDeserializer;
  7. import com.google.gson.JsonElement;
  8. import com.google.gson.JsonPrimitive;
  9. import com.google.gson.JsonSerializationContext;
  10. import com.google.gson.JsonSerializer;
  11. import java.lang.reflect.Type;
  12. import java.util.Date;
  13. import org.json.JSONArray;
  14. import org.json.JSONObject;
  15. public class JsonFactory {
  16. private static Gson gson;
  17. private static Gson getExposeGson;
  18. public JsonFactory() {
  19. }
  20. public static Gson getGson() {
  21. if (gson == null) {
  22. gson = createGsonBuilder().create();
  23. }
  24. return gson;
  25. }
  26. public static Gson getExposeGson() {
  27. if (getExposeGson == null) {
  28. getExposeGson = createGsonBuilderWithExpose().create();
  29. }
  30. return getExposeGson;
  31. }
  32. public static GsonBuilder createGsonBuilder() {
  33. JsonBooleanAsIntTypeAdapter booleanAsIntAdapter = new JsonBooleanAsIntTypeAdapter();
  34. return (new GsonBuilder()).registerTypeAdapter(Boolean.class, booleanAsIntAdapter).registerTypeAdapter(Boolean.TYPE, booleanAsIntAdapter).excludeFieldsWithModifiers(new int[]{16, 128, 8}).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
  35. public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
  36. return new JsonPrimitive(src.getTime());
  37. }
  38. }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
  39. public Date deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  40. JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
  41. return jsonPrimitive.isNumber() ? new Date(jsonPrimitive.getAsLong()) : Utils.getDate(jsonPrimitive.getAsString());
  42. }
  43. }).registerTypeAdapter(Double.class, new JsonDeserializer<Double>() {
  44. public Double deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  45. JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
  46. return jsonPrimitive.isNumber() ? jsonPrimitive.getAsDouble() : 0.0D;
  47. }
  48. }).registerTypeAdapter(Long.TYPE, new JsonDeserializer<Long>() {
  49. public Long deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  50. JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
  51. return jsonPrimitive.isNumber() ? jsonPrimitive.getAsLong() : 0L;
  52. }
  53. }).registerTypeAdapter(Long.class, new JsonDeserializer<Long>() {
  54. public Long deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  55. JsonPrimitive jsonPrimitive = p1.getAsJsonPrimitive();
  56. return jsonPrimitive.isNumber() ? jsonPrimitive.getAsLong() : 0L;
  57. }
  58. }).registerTypeAdapter(JSONArray.class, new JsonDeserializer<JSONArray>() {
  59. public JSONArray deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  60. JSONArray jsonArray = null;
  61. try {
  62. if (p1.isJsonArray()) {
  63. jsonArray = new JSONArray(p1.getAsJsonArray().toString());
  64. } else {
  65. jsonArray = new JSONArray(p1.getAsString());
  66. }
  67. } catch (Exception var6) {
  68. var6.printStackTrace();
  69. }
  70. return jsonArray;
  71. }
  72. }).registerTypeAdapter(JSONObject.class, new JsonDeserializer<JSONObject>() {
  73. public JSONObject deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  74. JSONObject jsonObject = null;
  75. try {
  76. if (p1.isJsonObject()) {
  77. jsonObject = new JSONObject(p1.getAsJsonObject().toString());
  78. } else {
  79. jsonObject = new JSONObject(p1.getAsString());
  80. }
  81. } catch (Exception var6) {
  82. var6.printStackTrace();
  83. }
  84. return jsonObject;
  85. }
  86. }).registerTypeAdapter(Object.class, new JsonDeserializer<Object>() {
  87. public Object deserialize(JsonElement p1, Type p2, JsonDeserializationContext p3) {
  88. return p3.deserialize(p1, p2);
  89. }
  90. });
  91. }
  92. public static GsonBuilder createGsonBuilderWithExpose() {
  93. return createGsonBuilder().excludeFieldsWithoutExposeAnnotation();
  94. }
  95. }