I am using Jackson 1.9.2 (org.codehaus.jackson) to combine the Java object according to the 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 would get the result that I want.
ColorLight light = new ColorLight(); light.setType("red"); light.setIsOn("true"); light.setValue("255"); objectMapper mapper = new ObjectMapper(); jsonString = mapper.writeValueAsString();
jsonString will look like this:
{"type":"red","isOn":"true", "value":"255"}
But sometimes I donβt have the value of the isOn property
ColorLight light = new ColorLight(); light.setType("red"); light.setValue("255");
But jsonString still looks like this:
{"type":"red","isOn":"false", "value":"255"}
Where "isOn: false" is the default for the Java Boolean type, which I do not want it to be there. How to remove isOn property in final json construct?
{"type":"red","value":"255"}
source share