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 ;
8- import org .javawebstack .abstractdata .util .GsonAbstractDataAdapter ;
3+ import org .javawebstack .abstractdata .mapper .Mapper ;
94
105public class AbstractMapper {
116
12- private Gson gson ;
7+ private final Mapper mapper = new Mapper () ;
138 private NamingPolicy namingPolicy = NamingPolicy .NONE ;
149 private String dateFormat = "yyyy-MM-dd HH:mm:ss" ;
1510 private boolean exposeRequired = false ;
1611
1712 public AbstractMapper setNamingPolicy (NamingPolicy namingPolicy ) {
1813 this .namingPolicy = namingPolicy ;
19- gson = null ;
14+ mapper . namingPolicy ( namingPolicy . getMapperPolicy ()) ;
2015 return this ;
2116 }
2217
@@ -26,7 +21,7 @@ public NamingPolicy getNamingPolicy() {
2621
2722 public AbstractMapper setExposeRequired (boolean exposeRequired ) {
2823 this .exposeRequired = exposeRequired ;
29- gson = null ;
24+ mapper . requireExpose ( exposeRequired ) ;
3025 return this ;
3126 }
3227
@@ -36,52 +31,22 @@ public boolean isExposeRequired() {
3631
3732 public AbstractMapper setDateFormat (String dateFormat ) {
3833 this .dateFormat = dateFormat ;
39- gson = null ;
34+ mapper . dateFormat ( dateFormat ) ;
4035 return this ;
4136 }
4237
4338 public String getDateFormat () {
4439 return dateFormat ;
4540 }
4641
47- private Gson gson () {
48- if (gson != null )
49- return gson ;
50- GsonBuilder builder = new GsonBuilder ()
51- .registerTypeAdapter (AbstractElement .class , new GsonAbstractDataAdapter <>())
52- .registerTypeAdapter (AbstractObject .class , new GsonAbstractDataAdapter <>())
53- .registerTypeAdapter (AbstractArray .class , new GsonAbstractDataAdapter <>())
54- .registerTypeAdapter (AbstractPrimitive .class , new GsonAbstractDataAdapter <>())
55- .registerTypeAdapter (AbstractNull .class , new GsonAbstractDataAdapter <>())
56- .setFieldNamingPolicy (namingPolicy .getGsonPolicy ())
57- .disableHtmlEscaping ();
58- if (dateFormat != null )
59- builder .setDateFormat (dateFormat );
60- if (exposeRequired ) {
61- builder .excludeFieldsWithoutExposeAnnotation ();
62- } else {
63- builder .setExclusionStrategies (new ExclusionStrategy () {
64- public boolean shouldSkipField (FieldAttributes fieldAttributes ) {
65- return fieldAttributes .getAnnotation (Expose .class ) != null && !fieldAttributes .getAnnotation (Expose .class ).serialize ();
66- }
67-
68- public boolean shouldSkipClass (Class <?> aClass ) {
69- return false ;
70- }
71- });
72- }
73- gson = builder .create ();
74- return gson ;
75- }
76-
7742 public AbstractElement toAbstract (Object object ) {
78- return AbstractElement . fromJson ( gson (). toJsonTree ( object ) );
43+ return mapper . map ( object );
7944 }
8045
8146 public <T > T fromAbstract (AbstractElement element , Class <T > type ) {
8247 if (element == null )
8348 return null ;
84- return gson (). fromJson (element . toJson () , type );
49+ return mapper . map (element , type );
8550 }
8651
8752
0 commit comments