44import com .google .gson .FieldNamingStrategy ;
55import com .google .gson .annotations .Expose ;
66import com .google .gson .annotations .SerializedName ;
7+ import com .google .gson .annotations .Since ;
8+ import com .google .gson .annotations .Until ;
79import com .jsoniter .JsonIterator ;
810import com .jsoniter .annotation .JsonIgnore ;
911import com .jsoniter .annotation .JsonProperty ;
@@ -66,6 +68,7 @@ protected DateFormat initialValue() {
6668 }
6769 };
6870 private FieldNamingStrategy fieldNamingStrategy ;
71+ private Double version ;
6972
7073 public Builder excludeFieldsWithoutExposeAnnotation () {
7174 excludeFieldsWithoutExposeAnnotation = true ;
@@ -122,6 +125,11 @@ public Builder disableHtmlEscaping() {
122125 return this ;
123126 }
124127
128+ public Builder setVersion (double version ) {
129+ this .version = version ;
130+ return this ;
131+ }
132+
125133 public GsonCompatibilityMode build () {
126134 escapeUnicode (false );
127135 return (GsonCompatibilityMode ) super .build ();
@@ -144,7 +152,9 @@ public boolean equals(Object o) {
144152 if (serializeNulls != builder .serializeNulls ) return false ;
145153 if (disableHtmlEscaping != builder .disableHtmlEscaping ) return false ;
146154 if (!dateFormat .get ().equals (builder .dateFormat .get ())) return false ;
147- return fieldNamingStrategy != null ? fieldNamingStrategy .equals (builder .fieldNamingStrategy ) : builder .fieldNamingStrategy == null ;
155+ if (fieldNamingStrategy != null ? !fieldNamingStrategy .equals (builder .fieldNamingStrategy ) : builder .fieldNamingStrategy != null )
156+ return false ;
157+ return version != null ? version .equals (builder .version ) : builder .version == null ;
148158 }
149159
150160 @ Override
@@ -155,6 +165,7 @@ public int hashCode() {
155165 result = 31 * result + (disableHtmlEscaping ? 1 : 0 );
156166 result = 31 * result + dateFormat .get ().hashCode ();
157167 result = 31 * result + (fieldNamingStrategy != null ? fieldNamingStrategy .hashCode () : 0 );
168+ result = 31 * result + (version != null ? version .hashCode () : 0 );
158169 return result ;
159170 }
160171
@@ -166,6 +177,7 @@ public Config.Builder copy() {
166177 copied .disableHtmlEscaping = disableHtmlEscaping ;
167178 copied .dateFormat = dateFormat ;
168179 copied .fieldNamingStrategy = fieldNamingStrategy ;
180+ copied .version = version ;
169181 return copied ;
170182 }
171183 }
@@ -294,6 +306,18 @@ public void updateClassDescriptor(ClassDescriptor desc) {
294306 if (builder ().serializeNulls ) {
295307 binding .shouldOmitNull = false ;
296308 }
309+ if (builder ().version != null ) {
310+ Since since = binding .getAnnotation (Since .class );
311+ if (since != null && builder ().version < since .value ()) {
312+ binding .toNames = new String [0 ];
313+ binding .fromNames = new String [0 ];
314+ }
315+ Until until = binding .getAnnotation (Until .class );
316+ if (until != null && builder ().version >= until .value ()) {
317+ binding .toNames = new String [0 ];
318+ binding .fromNames = new String [0 ];
319+ }
320+ }
297321 }
298322 super .updateClassDescriptor (desc );
299323 }
0 commit comments