5

I'm using Jackson for JSON serialization, and I would like to override the null serializer -- specifically, so that null values are serialized as empty strings in JSON rather than the string "null".

All of the documentation and examples I've found on how to set null serializers refers to Jackson 1.x -- for example, the code at the bottom of http://wiki.fasterxml.com/JacksonHowToCustomSerializers no longer compiles with Jackson 2.0 because StdSerializerProvider no longer exists in the library. That web page describes Jackson 2.0's module interface, but the module interface has no obvious way to override the null serializer.

Can anyone provide a pointer on how to override the null serializer in Jackson 2.0?

uscjeremy
  • 348
  • 1
  • 5
  • 12

2 Answers2

16

Override the JsonSerializer serialize method as below.

public class NullSerializer extends JsonSerializer<Object> {
  public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    // any JSON value you want...  
    jgen.writeString("");
  }
}

then you can set NullSerializer as default for custom object mapper:

public class CustomJacksonObjectMapper extends ObjectMapper {

public CustomJacksonObjectMapper() {
    super();
    DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
    sp.setNullValueSerializer(new NullSerializer());
    this.setSerializerProvider(sp);
  }
}

or specify it for some property using @JsonSerialize annotation, e.g:

public class MyClass {

  @JsonSerialize(nullsUsing = NullSerializer.class)
  private String property;
}
Nolequen
  • 3,032
  • 6
  • 36
  • 55
Narendra Reddy
  • 358
  • 2
  • 6
  • 1
    Thanks - this is what I was looking for. Two notes: 1) It's not necessary to extend ObjectMapper; its public API allows setting the null value serializer so it's sufficient to just create an instance. 2) There's apparently a bug in Jackson 2.2.2 that prevents the null serializer from being called on null nodes using the Tree Model; it only gets called for POJOs. The maintainers tell me this is fixed in 2.3. – uscjeremy Aug 08 '13 at 15:47
  • Anyone using `StdSerializerProvider` in version 1.x can change this to `DefaultSerializerProvider` for version 2+ of Jackson jaxrs. – Navigatron Apr 20 '22 at 10:59
0

I was not able to get the accepted answer to work for me. Perhaps because my ObjectMapper is a Spring Bean in my environment.

I reverted by to using a SimpleModule. Same serializer:

  public class NullSerializer extends JsonSerializer<Object> {
      public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        // any JSON value you want...  
        jgen.writeString("");
      }
    }

The annotation is located in a Mixin as I don't have access to modifying MyClass:

public abstract class MyClassMixin {
    @JsonSerialize(nullsUsing = NullSerializer.class)
    public String property;
}

To attach the serializer to my mapper, I use a module in my Spring component:

@AutoWired
ObjectMapper objectMapper;

@PostConstruct
public void onPostConstruct() {
    SimpleModule module = new SimpleModule();
    module.setMixInAnnotation(MyClass.class, MyClassMixin.class);
    objectMapper.registerModule(module);
}
Patrice Gagnon
  • 1,276
  • 14
  • 14