11package com .getui .push .v2 .sdk .core ;
22
33import com .getui .push .v2 .sdk .IJson ;
4- import com .google .gson .FieldNamingPolicy ;
5- import com .google .gson .Gson ;
6- import com .google .gson .GsonBuilder ;
4+ import com .getui .push .v2 .sdk .dto .res .statistic .StatisticDTO ;
5+ import com .google .gson .*;
76
87import java .lang .reflect .Type ;
8+ import java .text .NumberFormat ;
9+ import java .util .List ;
10+ import java .util .Map ;
11+
912
1013/**
1114 * create by getui on 2020/9/25
@@ -18,6 +21,37 @@ public class DefaultJson implements IJson {
1821 public static Gson createGson () {
1922 GsonBuilder gsonBuilder = new GsonBuilder ();
2023 gsonBuilder .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES );
24+ gsonBuilder .registerTypeAdapter (Map .class , new JsonDeserializer <StatisticDTO >() {
25+ @ Override
26+ public StatisticDTO deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
27+ StatisticDTO statisticDTO = new StatisticDTO ();
28+ JsonObject jo = json .getAsJsonObject ();
29+ for (Map .Entry <String , JsonElement > mx : jo .entrySet ()) {
30+ String key = mx .getKey ();
31+ JsonElement v = mx .getValue ();
32+ if (v .isJsonArray ()) {
33+ statisticDTO .put (key , context .deserialize (v , List .class ));
34+ } else if (v .isJsonPrimitive ()) {
35+ Object value = v .getAsString ();
36+ try {
37+ Number numValue = NumberFormat .getInstance ().parse ((String ) value );
38+ if (numValue != null && numValue .toString ().equals (value )) {
39+ if (numValue instanceof Long && (Long ) numValue <= Integer .MAX_VALUE && (Long ) numValue >= Integer .MIN_VALUE ) {
40+ value = Integer .valueOf ((String ) value );
41+ } else {
42+ value = numValue ;
43+ }
44+ }
45+ } catch (Exception ignored ) {
46+ }
47+ statisticDTO .put (key , value );
48+ } else if (v .isJsonObject ()) {
49+ statisticDTO .put (key , context .deserialize (v , Map .class ));
50+ }
51+ }
52+ return statisticDTO ;
53+ }
54+ });
2155 return gsonBuilder .create ();
2256 }
2357
0 commit comments