11package org .javawebstack .abstractdata ;
22
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 ;
83import org .javawebstack .abstractdata .mapper .Mapper ;
9- import org .javawebstack .abstractdata .util .GsonAbstractDataAdapter ;
104
115public class AbstractMapper {
126
13- // This allows
14- public static boolean enableExperimentalMapper = false ;
15-
16- private Gson gson ;
177 private final Mapper mapper = new Mapper ();
188 private NamingPolicy namingPolicy = NamingPolicy .NONE ;
199 private String dateFormat = "yyyy-MM-dd HH:mm:ss" ;
2010 private boolean exposeRequired = false ;
2111
2212 public AbstractMapper setNamingPolicy (NamingPolicy namingPolicy ) {
2313 this .namingPolicy = namingPolicy ;
24- gson = null ;
2514 mapper .namingPolicy (namingPolicy .getMapperPolicy ());
2615 return this ;
2716 }
@@ -32,7 +21,6 @@ public NamingPolicy getNamingPolicy() {
3221
3322 public AbstractMapper setExposeRequired (boolean exposeRequired ) {
3423 this .exposeRequired = exposeRequired ;
35- gson = null ;
3624 mapper .requireExpose (exposeRequired );
3725 return this ;
3826 }
@@ -43,7 +31,6 @@ public boolean isExposeRequired() {
4331
4432 public AbstractMapper setDateFormat (String dateFormat ) {
4533 this .dateFormat = dateFormat ;
46- gson = null ;
4734 mapper .dateFormat (dateFormat );
4835 return this ;
4936 }
@@ -52,48 +39,14 @@ public String getDateFormat() {
5239 return dateFormat ;
5340 }
5441
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-
8542 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 );
8944 }
9045
9146 public <T > T fromAbstract (AbstractElement element , Class <T > type ) {
9247 if (element == null )
9348 return null ;
94- if (enableExperimentalMapper )
95- return mapper .map (element , type );
96- return gson ().fromJson (element .toJson (), type );
49+ return mapper .map (element , type );
9750 }
9851
9952
0 commit comments