@@ -14,6 +14,16 @@ public class Config extends EmptyExtension {
1414 private static volatile Map <String , Config > configs = new HashMap <String , Config >();
1515 private volatile Map <Type , String > decoderCacheKeys = new HashMap <Type , String >();
1616 private volatile Map <Type , String > encoderCacheKeys = new HashMap <Type , String >();
17+ private final static Map <Class , OmitValue > primitiveOmitValues = new HashMap <Class , OmitValue >() {{
18+ put (boolean .class , new OmitValue .False ());
19+ put (char .class , new OmitValue .ZeroChar ());
20+ put (byte .class , new OmitValue .ZeroByte ());
21+ put (short .class , new OmitValue .ZeroShort ());
22+ put (int .class , new OmitValue .ZeroInt ());
23+ put (long .class , new OmitValue .ZeroLong ());
24+ put (float .class , new OmitValue .ZeroFloat ());
25+ put (double .class , new OmitValue .ZeroDouble ());
26+ }};
1727
1828 protected Config (String configName , Builder builder ) {
1929 this .configName = configName ;
@@ -76,6 +86,10 @@ public int indentionStep() {
7686 return builder .indentionStep ;
7787 }
7888
89+ public boolean omitDefaultValue () {
90+ return builder .omitDefaultValue ;
91+ }
92+
7993 public boolean escapeUnicode () {
8094 return builder .escapeUnicode ;
8195 }
@@ -90,6 +104,7 @@ public static class Builder {
90104 private EncodingMode encodingMode ;
91105 private int indentionStep ;
92106 private boolean escapeUnicode = true ;
107+ private boolean omitDefaultValue = false ;
93108
94109 public Builder () {
95110 String envMode = System .getenv ("JSONITER_DECODING_MODE" );
@@ -121,6 +136,11 @@ public Builder indentionStep(int indentionStep) {
121136 return this ;
122137 }
123138
139+ public Builder omitDefaultValue (boolean omitDefaultValue ) {
140+ this .omitDefaultValue = omitDefaultValue ;
141+ return this ;
142+ }
143+
124144 public Builder escapeUnicode (boolean escapeUnicode ) {
125145 this .escapeUnicode = escapeUnicode ;
126146 return this ;
@@ -159,6 +179,7 @@ public boolean equals(Object o) {
159179 if (indentionStep != builder .indentionStep ) return false ;
160180 if (escapeUnicode != builder .escapeUnicode ) return false ;
161181 if (decodingMode != builder .decodingMode ) return false ;
182+ if (omitDefaultValue != builder .omitDefaultValue ) return false ;
162183 return encodingMode == builder .encodingMode ;
163184 }
164185
@@ -168,6 +189,7 @@ public int hashCode() {
168189 result = 31 * result + (encodingMode != null ? encodingMode .hashCode () : 0 );
169190 result = 31 * result + indentionStep ;
170191 result = 31 * result + (escapeUnicode ? 1 : 0 );
192+ result = 31 * result + (omitDefaultValue ? 1 : 0 );
171193 return result ;
172194 }
173195
@@ -177,6 +199,7 @@ public Builder copy() {
177199 builder .decodingMode = decodingMode ;
178200 builder .indentionStep = indentionStep ;
179201 builder .escapeUnicode = escapeUnicode ;
202+ builder .omitDefaultValue = omitDefaultValue ;
180203 return builder ;
181204 }
182205 }
@@ -349,6 +372,7 @@ private void detectCtor(ClassDescriptor desc) {
349372 }
350373
351374 private void updateBindings (ClassDescriptor desc ) {
375+ boolean globalOmitDefault = JsoniterSpi .getCurrentConfig ().omitDefaultValue ();
352376 for (Binding binding : desc .allBindings ()) {
353377 JsonIgnore jsonIgnore = getJsonIgnore (binding .annotations );
354378 if (jsonIgnore != null ) {
@@ -365,6 +389,9 @@ private void updateBindings(ClassDescriptor desc) {
365389 binding .fromNames = new String [0 ];
366390 binding .toNames = new String [0 ];
367391 }
392+ if (globalOmitDefault ) {
393+ binding .defaultValueToOmit = createOmitValue (binding .valueType );
394+ }
368395 JsonProperty jsonProperty = getJsonProperty (binding .annotations );
369396 if (jsonProperty != null ) {
370397 updateBindingWithJsonProperty (binding , jsonProperty );
@@ -388,7 +415,10 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
388415 binding .asMissingWhenNotPresent = jsonProperty .required ();
389416 binding .isNullable = jsonProperty .nullable ();
390417 binding .isCollectionValueNullable = jsonProperty .collectionValueNullable ();
391- binding .shouldOmitNull = jsonProperty .omitNull ();
418+ String defaultValueToOmit = jsonProperty .defaultValueToOmit ();
419+ if (!defaultValueToOmit .isEmpty ()) {
420+ binding .defaultValueToOmit = OmitValue .Parsed .parse (binding .valueType , defaultValueToOmit );
421+ }
392422 String altName = jsonProperty .value ();
393423 if (!altName .isEmpty ()) {
394424 binding .name = altName ;
@@ -419,6 +449,14 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
419449 }
420450 }
421451
452+ protected OmitValue createOmitValue (Type valueType ) {
453+ OmitValue omitValue = primitiveOmitValues .get (valueType );
454+ if (omitValue != null ) {
455+ return omitValue ;
456+ }
457+ return new OmitValue .Null ();
458+ }
459+
422460 protected JsonWrapper getJsonWrapper (Annotation [] annotations ) {
423461 return getAnnotation (annotations , JsonWrapper .class );
424462 }
0 commit comments