Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson. Deserialize missing properties as empty Optional<T>

Let's say I have a class like this:

public static class Test {

    private Optional<String> something;

    public Optional<String> getSomething() {
        return something;
    }

    public void setSomething(Optional<String> something) {
        this.something = something;
    }
    
}

If I deserialize this JSON, I get an empty Optional:

{"something":null}

But if property is missing (in this case just empty JSON), I get null instead of Optional<T>. I could initialize fields by myself of course, but I think it would be better to have one mechanism for null and missing properties. So is there a way to make jackson deserialize missing properties as empty Optional<T>?

like image 245
Artem Malinko Avatar asked Jan 27 '16 09:01

Artem Malinko


People also ask

How do I make JSON field optional?

Just declare any field in your POJO. If a field is not present in your JSON structure then Jackson will not call the setter. You may keep track of wether a setter has been called with a flag in the POJO.

What is @JsonDeserialize?

@JsonDeserialize is used to specify custom deserializer to unmarshall the json object.

Does Jackson require default constructor?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.

How to include properties that are not empty in JSON?

Include.NON_EMPTY: Indicates that only properties that are not empty will be included in JSON. We can configure Include.NON_NULL and Include.NON_EMPTY at property level as well as at class level using @JsonInclude annotation. 2. Using Include.NON_NULL and Include.NON_EMPTY at the Property level

How to ignore null and empty fields while writing JSON in Jackson?

Jackson provides Include.NON_NULL to ignore fields with Null values and Include.NON_EMPTY to ignore fields with Empty values. By default, Jackson does not ignore Null and Empty fields while writing JSON. We can configure Include.NON_NULL and Include.NON_EMPTY at property level as well as at class level using @JsonInclude annotation.

How to ignore null or empty fields when serializing a Java class?

In this quick tutorial, I show you how to set up Jackson to ignore null or empty fields when serializing a Java class. Jackson provides Include.NON_NULL to ignore fields with Null values and Include.NON_EMPTY to ignore fields with Empty values. By default, Jackson does not ignore Null and Empty fields while writing JSON.

Can Jackson unmarshall an incomplete JSON?

The unknown fields are simply ignored, and only known fields are mapped: 3. Unmarshall an Incomplete JSON Similar to additional unknown fields, unmarshalling an incomplete JSON, a JSON that doesn't contain all the fields in the Java class, isn't a problem with Jackson: 4. Conclusion


2 Answers

Optional is not really meant to be used as a field but more as a return value. Why not have:

public static class Test {
  private String something;
  public Optional<String> getSomething() {
    return Optional.ofNullable(something);
  }
  public void setSomething(String something) {
    this.something = something;
  }
}
like image 50
assylias Avatar answered Oct 19 '22 01:10

assylias


For a solution without getters/setters, make sure something get initialized like this:

public Optional<String> something = Optional.empty();
like image 5
gamliela Avatar answered Oct 19 '22 00:10

gamliela