I'm using Jackson objectMapper to convert a JSON to an object and do some transformations and converting it back to JSON.
The JSON is like { "id":"abc1243", "val":1, "isDel":true}
In the above val is int and isDel is boolean.
My object is defined as below
public class MyObj{
private String id;
private String val;
private String isDel;
//getters and setters
}
When I convert the JSON to the MyObj, the String id and Int val are bound into the String but the boolean is not binded and hence I get null.
Im using the below approach
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
MyObj myObj = mapper.readValue(new File("/Users/sample.json"), MyObj.class);
}
May I know why the boolean is not binded to the String member.
You can have a setter that accepts boolean and does the necessary conversion
@JsonProperty("isDel")
public void setIsDel(boolean isDel) {
this.isDel = String.valueOf(isDel);
}
note: this does not interfere with a setter that accepts String (the annotation tells Jackson to use this particular method)