I am trying simple JSON to de-serialize into a java object. However, I get empty String values ββfor the values ββof the java.lang.String property. In the rest of the properties, null values ββare converted to null values ββ(this is what I want).
My JSON and its associated Java class are listed below.
JSON string:
{ "eventId" : 1, "title" : "sample event", "location" : "" }
EventBean POJO Class:
public class EventBean { public Long eventId; public String title; public String location; }
My main code is:
ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); try { File file = new File(JsonTest.class.getClassLoader().getResource("event.txt").getFile()); JsonNode root = mapper.readTree(file);
I was expecting a print of "This is Zero." Instead, I get "This." Obviously, Jackson does not treat empty String values ββas NULL when converting to my String object type.
I read somewhere that this was expected. However, this is what I want to avoid for java.lang.String too. Is there an easy way?
java json jackson
Mayur
source share