@@ -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,8 +86,8 @@ public int indentionStep() {
7686 return builder .indentionStep ;
7787 }
7888
79- public boolean omitZero () {
80- return builder .omitZero ;
89+ public boolean omitDefaultValue () {
90+ return builder .omitDefaultValue ;
8191 }
8292
8393 public boolean escapeUnicode () {
@@ -94,7 +104,7 @@ public static class Builder {
94104 private EncodingMode encodingMode ;
95105 private int indentionStep ;
96106 private boolean escapeUnicode = true ;
97- private boolean omitZero ;
107+ private boolean omitDefaultValue = false ;
98108
99109 public Builder () {
100110 String envMode = System .getenv ("JSONITER_DECODING_MODE" );
@@ -126,8 +136,8 @@ public Builder indentionStep(int indentionStep) {
126136 return this ;
127137 }
128138
129- public Builder omitZero (boolean b ) {
130- omitZero = b ;
139+ public Builder omitDefaultValue (boolean omitDefaultValue ) {
140+ this . omitDefaultValue = omitDefaultValue ;
131141 return this ;
132142 }
133143
@@ -169,7 +179,7 @@ public boolean equals(Object o) {
169179 if (indentionStep != builder .indentionStep ) return false ;
170180 if (escapeUnicode != builder .escapeUnicode ) return false ;
171181 if (decodingMode != builder .decodingMode ) return false ;
172- if (omitZero != builder .omitZero ) return false ;
182+ if (omitDefaultValue != builder .omitDefaultValue ) return false ;
173183 return encodingMode == builder .encodingMode ;
174184 }
175185
@@ -179,7 +189,7 @@ public int hashCode() {
179189 result = 31 * result + (encodingMode != null ? encodingMode .hashCode () : 0 );
180190 result = 31 * result + indentionStep ;
181191 result = 31 * result + (escapeUnicode ? 1 : 0 );
182- result = 31 * result + (omitZero ? 1 : 0 );
192+ result = 31 * result + (omitDefaultValue ? 1 : 0 );
183193 return result ;
184194 }
185195
@@ -189,7 +199,7 @@ public Builder copy() {
189199 builder .decodingMode = decodingMode ;
190200 builder .indentionStep = indentionStep ;
191201 builder .escapeUnicode = escapeUnicode ;
192- builder .omitZero = omitZero ;
202+ builder .omitDefaultValue = omitDefaultValue ;
193203 return builder ;
194204 }
195205 }
@@ -362,6 +372,7 @@ private void detectCtor(ClassDescriptor desc) {
362372 }
363373
364374 private void updateBindings (ClassDescriptor desc ) {
375+ boolean globalOmitDefault = JsoniterSpi .getCurrentConfig ().omitDefaultValue ();
365376 for (Binding binding : desc .allBindings ()) {
366377 JsonIgnore jsonIgnore = getJsonIgnore (binding .annotations );
367378 if (jsonIgnore != null ) {
@@ -378,6 +389,9 @@ private void updateBindings(ClassDescriptor desc) {
378389 binding .fromNames = new String [0 ];
379390 binding .toNames = new String [0 ];
380391 }
392+ if (globalOmitDefault ) {
393+ binding .defaultValueToOmit = createOmitValue (binding .valueType );
394+ }
381395 JsonProperty jsonProperty = getJsonProperty (binding .annotations );
382396 if (jsonProperty != null ) {
383397 updateBindingWithJsonProperty (binding , jsonProperty );
@@ -401,7 +415,10 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
401415 binding .asMissingWhenNotPresent = jsonProperty .required ();
402416 binding .isNullable = jsonProperty .nullable ();
403417 binding .isCollectionValueNullable = jsonProperty .collectionValueNullable ();
404- binding .shouldOmitNull = jsonProperty .omitNull ();
418+ String defaultValueToOmit = jsonProperty .defaultValueToOmit ();
419+ if (!defaultValueToOmit .isEmpty ()) {
420+ binding .defaultValueToOmit = OmitValue .Parsed .parse (binding .valueType , defaultValueToOmit );
421+ }
405422 String altName = jsonProperty .value ();
406423 if (!altName .isEmpty ()) {
407424 binding .name = altName ;
@@ -432,6 +449,14 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
432449 }
433450 }
434451
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+
435460 protected JsonWrapper getJsonWrapper (Annotation [] annotations ) {
436461 return getAnnotation (annotations , JsonWrapper .class );
437462 }
0 commit comments