1
1
package com .getui .push .v2 .sdk .core ;
2
2
3
3
import 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 .*;
7
6
8
7
import java .lang .reflect .Type ;
8
+ import java .text .NumberFormat ;
9
+ import java .util .List ;
10
+ import java .util .Map ;
11
+
9
12
10
13
/**
11
14
* create by getui on 2020/9/25
@@ -18,6 +21,37 @@ public class DefaultJson implements IJson {
18
21
public static Gson createGson () {
19
22
GsonBuilder gsonBuilder = new GsonBuilder ();
20
23
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
+ });
21
55
return gsonBuilder .create ();
22
56
}
23
57
0 commit comments