Jackson How-To: Custom Serializers

Alternative Methods

There are two general mechanisms for enabling fully customized serialization:

First method is simpler in that no registration is needed: when values of types that implement JsonSerializable are encountered, serialize() method is called to serialize the instance.

If you choose to use the second approach, instead of directly starting with JsonSerializer, it is common to sub-class com.fasterxml.jackson.databind.ser.std.StdSerializer (2.x) (or org.codehaus.jackson.map.ser.StdSerializer for 1.x), which has commonly needed functionality that can simplify writing of custom serializers.

There are other intermediate base classes you may want to consider for specific Java types: for scalar types (things serialized as JSON number or Strings), you may want to extend StdScalarSerializer for example. Similarly there are base types for Collection and Map value serializers.

Registering external serializers

There are multiple ways to do register external serializers:

With Jackson 1.7 and above

Jackson 1.7 added ability to register serializers and deserializes via Module interface. This is the recommended way to add custom serializers -- all serializers are considered "generic", in that they are used for subtypes unless more specific binding is found.

The simplest way is to extend SimpleModule, add serializer(s), and register module with ObjectMapper:

   1         ObjectMapper mapper = new ObjectMapper();
   2         SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null));
   3         testModule.addSerializer(new MyCustomSerializer()); // assuming serializer declares correct class to bind to
   4         mapper.registerModule(testModule);

For more advanced handling of types to serializers you may need to implement Module interface directly; this will give more control over exact matching logic. This may be necessary when dealing with generic types (especially Maps and Collections).

With Jackson 1.0 - 1.6

Before Jackson 1.7, the main method method was:

Note that starting with 1.8, CustomSerializerFactory is deprecated and should not be used.

Specific use cases

Converting null values to something else

(like empty Strings)

If you want to output some other JSON value instead of null (mainly because some other processing tools prefer other constant values -- often empty String), things are bit trickier as nominal type may be anything; and while you could register serializer for Object.class, it would not be used unless there wasn't more specific serializer to use.

But there is specific concept of "null serializer" that you can use as follows:

// Configuration of ObjectMapper:
    // First: need a custom serializer provider
   StdSerializerProvider sp = new StdSerializerProvider();
   sp.setNullValueSerializer(new NullSerializer());
   // And then configure mapper to use it
   ObjectMapper m = new ObjectMapper();

// serialization as done using regular ObjectMapper.writeValue()

// and NullSerializer can be something as simple as:
public class NullSerializer extends JsonSerializer<Object>
   public void serialize(Object value, JsonGenerator jgen,
SerializerProvider provider)
       throws IOException, JsonProcessingException
       // any JSON value you want...


JacksonHowToCustomSerializers (last edited 2014-07-31 18:22:10 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC