1

Let's say I have an object:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Foods {

    public Foods() {}

    @Id
    @JsonProperty
    private String id;

    @JsonProperty
    private String name;

    @JsonProperty
    private Double calories;

    @JsonProperty
    private boolean sweet;

    }

Now, in my Spring REST API call, I only populate the "name" field for the food and return it.

I get this as the return JSON:

  {
    "name": "Strawberry Pies",
    "sweet": false
  }

You can see that because the calories is not populated, the JSON doesn't return it. The "sweet" field I guess gets populated because the default value for boolean is false.

I do not however want the JSON string to return the "sweet" boolean of false if it was not populated in the first place. Is there a way to do this?

6
  • 1
    Simple answer is that NON_NULL means, well, "Do not inlude null values". If String-valued fields are not initialized, they will be nulls. But boolean is primitive type, for which nulls are never used: false is not null. If you do want it to be excluded, you can make "sweet" to be of type Boolean (that is, java.lang.Boolean), wrapper that does allow nulls as well. Or, like another answer suggested, just use NON_DEFAULT. Commented Oct 15, 2015 at 23:48
  • @StaxMan Does Jackson instantiate the class once (with parameterless constructor) to check the DEFAULT value and compare against the instance you pass for serialization? What is the motivation for this? (As opposed to just default initialization values.) Commented Oct 16, 2015 at 0:18
  • @SotiriosDelimanolis It does not mean "whatever defaults are for primitives". It literally means "default values for this class when instantiated (with no-args ctor)" -- and to know what that would be, one needs an instance to check what those default values are. Commented Oct 16, 2015 at 16:46
  • @StaxMan Nono, what you're saying is what I mean. I'm curious as to why it was done that way. Is that a common scenario (as opposed to not serializing default initialization values, 0, false, null)? Commented Oct 16, 2015 at 16:53
  • @StaxMan I'm worried about that unexpected instance creation. Commented Oct 16, 2015 at 16:54

1 Answer 1

3

Annotate your field (or property getter/setter) with

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean sweet;

The javadoc of NON_DEFAULT states

Value that indicates that only properties that have values that differ from default settings (meaning values they have when Bean is constructed with its no-arguments constructor) are to be included.

With

private boolean sweet;

that default value would've been false. If that's the value that field had for the object you serialized, the JSON would not contain it.

Similarly, if you had

private boolean sweet = true;

and the field for the corresponding object had a value of true, the generated JSON would also not contain it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.