8

I've started using Jackson as a JSON generator, as an alternative to google GSON. I've run into an issue where Jackson is generating object: null if the object is indeed null. GSON on the other hand generates NO entry in JSON, which is the behavior I want. Is there a way to stop Jackson from generating null object/value when an 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 looks like this:

        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 (which is what I want - note that "urlList":null was not generated) :

{"eatwithrustyspoon":{"device":"iPad","os":"iPhone OS","peer_id"

geekyaleks
  • 1,281
  • 3
  • 18
  • 29

3 Answers3

12

From the Jackson FAQ:

Can I omit writing of Bean properties with null value? ("how to prevent writing of null properties", "how to suppress null values")

Yes. As per JacksonAnnotationSerializeNulls, you can use:

  objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
  // or (for older versions):
  objectMapper.configure(SerializationConfig.WRITE_NULL_PROPERTIES, false);

and voila, no more null values. Note that you MUST configure mapper before beans are serialized, since this setting may be cached along with serializers. So setting it too late might prevent change from taking effect.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • At this did not work for me (an undecipherable compile time error), but it gave me a good hint where to go - if I add this: @JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL) On top of my serializable classes, all works well. – geekyaleks Feb 13 '13 at 19:16
  • Gah, sorry - a link to that was supposed to be in there as well (edited), though one of those should have worked (you shouldn't have to annotate your POJOs). – Brian Roach Feb 13 '13 at 19:48
  • This helped for me at a global ObjectMapper level. If I needed to configure it at a bean/class or field/property level, using the JsonInclude would seem the way to go. – Matt Feb 28 '14 at 18:13
2

my issue was bit different actually i was getting Null values for the properties of POJO class.

however i solved the problem by giving mapping to properties in my pojo class like this :

@JsonProperty("PROPERTY_NAME")

thought it may help someone :)

Prateek S
  • 81
  • 1
  • 9
1

The following solution saved me.

  objectMapper.setSerializationInclusion(Include.NON_NULL);
Ghost Rider
  • 161
  • 1
  • 1
  • 10