Non default value skipped in serialized json with serialization inclusion NON_DEFAULT
See original GitHub issueRunning following code:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
@org.junit.Test
public void name() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
Pojo pojo = new Pojo();
pojo.setValue(false);
System.out.println(mapper.writeValueAsString(pojo));
}
public static class Pojo {
private Boolean value;
public Boolean getValue() {
return value;
}
public void setValue(Boolean value) {
this.value = value;
}
}
}
with Jackson v 2.8.2 outputs {"value":false}
(as I expected). This however changes starting with 2.8.3: it outputs {}
(tested with 2.8.3, 2.8.11.2 and 2.9.6). I guess it was affected by #1351. Was it intended? Imo that’s bug: default value for Boolean
is null
, not false
.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top Results From Across the Web
How do I make jackson not serialize primitives with default value
If you're using a recent version of Jackson you can use JsonInclude.Include.NON_DEFAULT which should work for primitives.
Read more >JsonSerializerOptions.DefaultIgnoreCondition Property
Gets or sets a value that determines when properties with default values are ignored during serialization or deserialization. The default value is Never....
Read more >Jackson JSON - @JsonInclude NON_DEFAULT Example
This is done by creating an instance of POJO using zero-argument constructor, and comparing the property values, excluding the ones having ...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, ... 0 and length - 1 (inclusive) are serialized; other properties are...
Read more >Reducing Serialized JSON Size - Json.NET
Setting a value of DefaultValueHandling.Ignore will make the JsonSerializer skip writing any properties that have a default value to the JSON result. For...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This issue also burned us when upgrading from 2.6.6 to 2.9.6. It was extra confusing because of the breaking changes with
NON_EMPTY
andNON_DEFAULT
going from 2.6.x to 2.7.x+.This also changed with String. Previously
""
would not be omitted with `NON_DEFAULT. After 2.8.3 it is omitted.I’m reading a bunch of other tickets relating #1351 and I’m sympathetic because it’s hard to know what the right answer is. But it would be great to know if this is a bug that’s going to be fixed (and thus we might upgrade to 2.8.2) or if this is future functionality and we need to fix this in our application.
@Rolly992 what you think is “default value” is not actually specification of what Jackson means by default PROPERTY value for deserialization purposes. Before sharing your personal interpretation of a term it makes sense to try to learn its meaning in context.
But to recap: idea of default value for, say
is that default value for
x
is3
, andy
is 7 (in context of “property value ofMyValue
”). This because default instance ofMyValue
would have those values. But implementing this can get tricky because to find real default values – and not just what JDK initializes properties to, in absence of assignment – one needs to have containing POJO type.