44import com .google .gson .annotations .SerializedName ;
55import com .jsoniter .annotation .JsonIgnore ;
66import com .jsoniter .annotation .JsonProperty ;
7+ import com .jsoniter .any .Any ;
8+ import com .jsoniter .output .JsonStream ;
79import com .jsoniter .spi .Config ;
810import com .jsoniter .spi .*;
911
12+ import java .io .IOException ;
1013import java .lang .annotation .Annotation ;
14+ import java .lang .reflect .Type ;
15+ import java .text .DateFormat ;
16+ import java .text .SimpleDateFormat ;
17+ import java .util .Date ;
18+ import java .util .Locale ;
1119
1220public class GsonCompatibilityMode extends Config {
1321
@@ -22,6 +30,12 @@ protected Builder builder() {
2230 public static class Builder extends Config .Builder {
2331 private boolean excludeFieldsWithoutExposeAnnotation = false ;
2432 private boolean serializeNulls = false ;
33+ private ThreadLocal <DateFormat > dateFormat = new ThreadLocal <DateFormat >() {
34+ @ Override
35+ protected DateFormat initialValue () {
36+ return DateFormat .getDateTimeInstance (DateFormat .DEFAULT , DateFormat .DEFAULT , Locale .US );
37+ }
38+ };
2539
2640 public Builder excludeFieldsWithoutExposeAnnotation () {
2741 excludeFieldsWithoutExposeAnnotation = true ;
@@ -33,6 +47,31 @@ public Builder serializeNulls() {
3347 return this ;
3448 }
3549
50+ public Builder setDateFormat (int dateStyle ) {
51+ // no op, same as gson
52+ return this ;
53+ }
54+
55+ public Builder setDateFormat (final int dateStyle , final int timeStyle ) {
56+ dateFormat = new ThreadLocal <DateFormat >() {
57+ @ Override
58+ protected DateFormat initialValue () {
59+ return DateFormat .getDateTimeInstance (dateStyle , timeStyle , Locale .US );
60+ }
61+ };
62+ return this ;
63+ }
64+
65+ public Builder setDateFormat (final String pattern ) {
66+ dateFormat = new ThreadLocal <DateFormat >() {
67+ @ Override
68+ protected DateFormat initialValue () {
69+ return new SimpleDateFormat (pattern , Locale .US );
70+ }
71+ };
72+ return this ;
73+ }
74+
3675 public GsonCompatibilityMode build () {
3776 return (GsonCompatibilityMode ) super .build ();
3877 }
@@ -51,18 +90,34 @@ public boolean equals(Object o) {
5190 Builder builder = (Builder ) o ;
5291
5392 if (excludeFieldsWithoutExposeAnnotation != builder .excludeFieldsWithoutExposeAnnotation ) return false ;
54- return serializeNulls == builder .serializeNulls ;
93+ if (serializeNulls != builder .serializeNulls ) return false ;
94+ return dateFormat .get ().equals (builder .dateFormat .get ());
5595 }
5696
5797 @ Override
5898 public int hashCode () {
5999 int result = super .hashCode ();
60100 result = 31 * result + (excludeFieldsWithoutExposeAnnotation ? 1 : 0 );
61101 result = 31 * result + (serializeNulls ? 1 : 0 );
102+ result = 31 * result + dateFormat .get ().hashCode ();
62103 return result ;
63104 }
64105 }
65106
107+ @ Override
108+ public Encoder createEncoder (String cacheKey , Type type ) {
109+ if (Date .class == type ) {
110+ return new EmptyEncoder () {
111+ @ Override
112+ public void encode (Object obj , JsonStream stream ) throws IOException {
113+ DateFormat dateFormat = builder ().dateFormat .get ();
114+ stream .writeVal (dateFormat .format (obj ));
115+ }
116+ };
117+ }
118+ return super .createEncoder (cacheKey , type );
119+ }
120+
66121 @ Override
67122 public void updateClassDescriptor (ClassDescriptor desc ) {
68123 removeGetterAndSetter (desc );
0 commit comments