Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How an optional property can be ignored when using jackson to convert Java object

Tags:

java

json

jackson

I'm using Jackson 1.9.2(org.codehaus.jackson) to convent from Java object to matching JSON construct. Here is my java object:

Class ColorLight {
    String type;
    boolean isOn;
    String value;

    public String getType(){
        return type;
    }

    public setType(String type) {
        this.type = type;
    }

    public boolean getIsOn(){
        return isOn;
    }

    public setIsOn(boolean isOn) {
        this.isOn = isOn;
    }

    public String getValue(){
        return value;
    }

    public setValue(String value) {
        this.value = value;
    }
}

If I did the following conversion, I'd get the result I want.

ColorLight light = new ColorLight();
light.setType("red");
light.setIsOn("true");
light.setValue("255");
objectMapper mapper = new ObjectMapper();
jsonString = mapper.writeValueAsString();

jsonString would be like:

{"type":"red","isOn":"true", "value":"255"}

But sometimes I don't have the value of isOn property

ColorLight light = new ColorLight();
light.setType("red");
light.setValue("255");

But the jsonString is still like:

{"type":"red","isOn":"false", "value":"255"}

Where "isOn:false" is default value of Java boolean type which I don't want it be there. How can I remove the isOn property in the final json construct like this?

{"type":"red","value":"255"}
like image 975
taoxiaopang Avatar asked Jul 08 '14 02:07

taoxiaopang


People also ask

How do I ignore properties in Jackson?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

Does Jackson ignore unknown properties?

Jackson API provides two ways to ignore unknown fields, first at the class level using @JsonIgnoreProperties annotation and second at the ObjectMapper level using configure() method.


2 Answers

To skip the value if it's not present:

  • Use Boolean instead of the boolean primitive (boolean values are always set to true or false).
  • Configure Jackson not to serialize nulls by using @JsonInclude(Include.NON_NULL) or @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL), depending on the version.
like image 76
chrylis -cautiouslyoptimistic- Avatar answered Oct 26 '22 12:10

chrylis -cautiouslyoptimistic-


You can mark your class with the @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) in 1.x annotation 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.

The @JsonInclude(JsonInclude.Include.NON_DEFAULT) annotation is used for version 2.x.

Here is an example:

public class JacksonInclusion {

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
    public static class ColorLight {
        public String type;
        public boolean isOn;

        public ColorLight() {
        }

        public ColorLight(String type, boolean isOn) {
            this.type = type;
            this.isOn = isOn;
        }
    }

    public static void main(String[] args) throws IOException {
        ColorLight light = new ColorLight("value", false);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(light));
    }
}

Output:

{"type":"value"}
like image 7
Alexey Gavrilov Avatar answered Oct 26 '22 10:10

Alexey Gavrilov