1
1
package org .javawebstack .abstractdata ;
2
2
3
- import com .google .gson .ExclusionStrategy ;
4
- import com .google .gson .FieldAttributes ;
5
- import com .google .gson .Gson ;
6
- import com .google .gson .GsonBuilder ;
7
- import com .google .gson .annotations .Expose ;
8
3
import org .javawebstack .abstractdata .mapper .Mapper ;
9
- import org .javawebstack .abstractdata .util .GsonAbstractDataAdapter ;
10
4
11
5
public class AbstractMapper {
12
6
13
- // This allows
14
- public static boolean enableExperimentalMapper = false ;
15
-
16
- private Gson gson ;
17
7
private final Mapper mapper = new Mapper ();
18
8
private NamingPolicy namingPolicy = NamingPolicy .NONE ;
19
9
private String dateFormat = "yyyy-MM-dd HH:mm:ss" ;
20
10
private boolean exposeRequired = false ;
21
11
22
12
public AbstractMapper setNamingPolicy (NamingPolicy namingPolicy ) {
23
13
this .namingPolicy = namingPolicy ;
24
- gson = null ;
25
14
mapper .namingPolicy (namingPolicy .getMapperPolicy ());
26
15
return this ;
27
16
}
@@ -32,7 +21,6 @@ public NamingPolicy getNamingPolicy() {
32
21
33
22
public AbstractMapper setExposeRequired (boolean exposeRequired ) {
34
23
this .exposeRequired = exposeRequired ;
35
- gson = null ;
36
24
mapper .requireExpose (exposeRequired );
37
25
return this ;
38
26
}
@@ -43,7 +31,6 @@ public boolean isExposeRequired() {
43
31
44
32
public AbstractMapper setDateFormat (String dateFormat ) {
45
33
this .dateFormat = dateFormat ;
46
- gson = null ;
47
34
mapper .dateFormat (dateFormat );
48
35
return this ;
49
36
}
@@ -52,48 +39,14 @@ public String getDateFormat() {
52
39
return dateFormat ;
53
40
}
54
41
55
- private Gson gson () {
56
- if (gson != null )
57
- return gson ;
58
- GsonBuilder builder = new GsonBuilder ()
59
- .registerTypeAdapter (AbstractElement .class , new GsonAbstractDataAdapter <>())
60
- .registerTypeAdapter (AbstractObject .class , new GsonAbstractDataAdapter <>())
61
- .registerTypeAdapter (AbstractArray .class , new GsonAbstractDataAdapter <>())
62
- .registerTypeAdapter (AbstractPrimitive .class , new GsonAbstractDataAdapter <>())
63
- .registerTypeAdapter (AbstractNull .class , new GsonAbstractDataAdapter <>())
64
- .setFieldNamingPolicy (namingPolicy .getGsonPolicy ())
65
- .disableHtmlEscaping ();
66
- if (dateFormat != null )
67
- builder .setDateFormat (dateFormat );
68
- if (exposeRequired ) {
69
- builder .excludeFieldsWithoutExposeAnnotation ();
70
- } else {
71
- builder .setExclusionStrategies (new ExclusionStrategy () {
72
- public boolean shouldSkipField (FieldAttributes fieldAttributes ) {
73
- return fieldAttributes .getAnnotation (Expose .class ) != null && !fieldAttributes .getAnnotation (Expose .class ).serialize ();
74
- }
75
-
76
- public boolean shouldSkipClass (Class <?> aClass ) {
77
- return false ;
78
- }
79
- });
80
- }
81
- gson = builder .create ();
82
- return gson ;
83
- }
84
-
85
42
public AbstractElement toAbstract (Object object ) {
86
- if (enableExperimentalMapper )
87
- return mapper .map (object );
88
- return AbstractElement .fromJson (gson ().toJsonTree (object ));
43
+ return mapper .map (object );
89
44
}
90
45
91
46
public <T > T fromAbstract (AbstractElement element , Class <T > type ) {
92
47
if (element == null )
93
48
return null ;
94
- if (enableExperimentalMapper )
95
- return mapper .map (element , type );
96
- return gson ().fromJson (element .toJson (), type );
49
+ return mapper .map (element , type );
97
50
}
98
51
99
52
0 commit comments