I started using Jackson as a JSON generator as an alternative to google GSON. I ran into a problem when Jackson generates an object: null if the object is really zero. GSON, on the other hand, generates a NO entry in JSON, which is the behavior I want. Is there a way to stop Jackson from creating a null object / value when the object is missing?
Jackson
ObjectMapper mapper = new ObjectMapper(); StringWriter sw = new StringWriter(); mapper.writeValue(sw, some_complex_object); String jackson = sw.getBuffer().toString(); System.out.println("********************* START JACKSON JSON ****************************"); System.out.println(jackson); System.out.println("********************* END JACKSON JSON ****************************");
generates this:
{"eatwithrustyspoon": { "urlList": null , "device": "iPad", "os": "iPhone OS", "peer_id":
and GSON is as follows:
Gson gson = new Gson(); String json = gson.toJson(some_complex_object); System.out.println("********************* START GSON JSON ****************************"); System.out.println(json); System.out.println("********************* END GSON JSON ****************************");
and it generates this (this is what I want - note that "urlList": null was not generated):
{"eatwithrustyspoon": {"device": "iPad", "os": "iPhone OS", "peer_id"
java json jackson gson
geekyaleks
source share