question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non default value skipped in serialized json with serialization inclusion NON_DEFAULT

See original GitHub issue

Running 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:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
workmanwcommented, Aug 24, 2018

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 and NON_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.

0reactions
cowtowncodercommented, May 29, 2020

@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

public class MyValue {
   public int x, y;

   public MyValue() {
        x = 3;
        y = 7;
   }

   // getters and setters

is that default value for x is 3, and y is 7 (in context of “property value of MyValue”). This because default instance of MyValue 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found