Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring not set primitive type while converting an object to json using jackson

Tags:

java

json

jackson

I have a class which looks like below:-

@JsonSerialize(include = Inclusion.NON_EMPTY)
public class JsonPojo {
    private int intVal;
    private String strVal;

    public int getIntVal() {
    return intVal;
    }

    public void setIntVal(int intVal) {
    this.intVal = intVal;
    }

    public String getStrVal() {
    return strVal;
    }

    public void setStrVal(String strVal) {
    this.strVal = strVal;
    }
}

If I am having an instance of JsonPojo in which the int field is not set then while converting it to json jackson is assigning it a default 0 as shown below:-

public static void main(String[] args) {
    JsonPojo jsonPojo = new JsonPojo();
    jsonPojo.setStrVal("Hello World");
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
        String json = mapper.writeValueAsString(jsonPojo);
        System.out.println(json);
    } catch (Exception e) {

    }

 }

This will output:- {"intVal":0,"strVal":"Hello World"}

Is there a way I can ignore intVal in Json output when intVal is not set without converting the datatype of intVal to Integer? If I convert intVal to Integer then when this value is not set it will be null & doing JsonSerialize(include = Inclusion.NON_EMPTY) will skip intVal during the conversion to json.

But I want to know if it is possible to achieve the same behavior without converting intVal to Integer from int?

I am using jackson 1.9.

like image 836
tuk Avatar asked Oct 17 '25 09:10

tuk


2 Answers

Inclusion.NON_EMPTY should do what you expect. From the documentation:

Value that indicates that only properties that have values that values that are null or what is considered empty are not to be included.

Java primitive int is by default 0 (this is considered empty). So if you never set it or you set it to 0 the output should be {"strVal":"Hello World"} I tried this on jackson 2.6.3 and seems to work so I assume there is an issue on 1.9

You could try the following and maybe works on 1.9:

@JsonInclude(Include.NON_DEFAULT)
private int intVal;

and remove include = Inclusion.NON_EMPTY

If 0 is a valid value you could add a flag to your pojo. In you ever call the setter the flag should turn to true:

    @JsonIgnore
    public boolean set = false;

    public Integer getIntVal() {
        if (!set) {
            return null;
        }
        return Integer.valueOf(intVal);
    }

    public void setIntVal(int intVal) {
        set = true;
        this.intVal = intVal;
    }
like image 163
Liviu Stirb Avatar answered Oct 18 '25 23:10

Liviu Stirb


You could try @JsonInclude and @JsonProperty annotations from jackson.annotations package. Here's the sample code :

JsonPojo class:

public class JsonPojo {
@JsonInclude(Include.NON_DEFAULT)
private int intVal;
@JsonProperty
private String strVal;

public int getIntVal() {
    return intVal;
}

public void setIntVal(int intVal) {
    this.intVal = intVal;
}

public String getStrVal() {
    return strVal;
}

public void setStrVal(String strVal) {
    this.strVal = strVal;
}}
like image 43
can lekili Avatar answered Oct 18 '25 23:10

can lekili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!