Create Jackson custom serializers and deserializers

Jackson is a popular library to handle JSON in Java. It has built-in serializers and deserializers to handle common data types. If you want to serialize and deserialize custom types, you can add custom serializers and deserializers. The code in this post is tested using Jackson 2.7.2 and should work with Jackson after 1.7.

The class we want to serialize and deserialize is com.github.zafarkhaja.semver.Version from the jsemver library. We want objects of this class to be serialized as simple strings, e.g. 1.0.0.

Serializer

The class VersionSerializer extends Jackson’s com.fasterxml.jackson.databind.ser.std.StdSerializer and overrides the method serialize(). The first parameter is the object to serialize, the second parameter is the JsonGenerator object that can be used to write out the JSON structure for this Version object. Here we use JsonGenerator.writeString() to write the string format of the Version object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.github.zafarkhaja.semver.Version;
import java.io.IOException;
public class VersionSerializer extends StdSerializer<Version> {
public VersionSerializer() {
super(Version.class);
}
@Override
public void serialize(final Version value, final JsonGenerator gen, final SerializerProvider provider) throws IOException {
gen.writeString(value.toString());
}
}

Deserializer

The class VersionDeserializer extends Jackson’s com.fasterxml.jackson.databind.deser.std.StdDeserializer and overrides the method deserialize() to convert the string format to a Version object. The JsonParser object can be used to parse the JSON structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.github.zafarkhaja.semver.Version;
import java.io.IOException;
public class VersionDeserializer extends StdDeserializer<Version> {
public VersionDeserializer() {
super(Version.class);
}
@Override
public Version deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
return Version.valueOf(p.getText());
}
}

Module

When creating a Jackson’s ObjectMapper object, we need to create a new module with the VersionSerializer and VersionDeserializer and register the module to the ObjectMapper object.

1
2
3
4
5
6
7
8
9
public ObjectMapper configObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
final SimpleModule module = new SimpleModule("configModule", com.fasterxml.jackson.core.Version.unknownVersion());
module.addSerializer(new VersionSerializer());
module.addDeserializer(Version.class, new VersionDeserializer());
objectMapper.registerModule(module);
return objectMapper;
}

Then you can use ObjectMapper to correctly serialize and deserialize objects with Version as the property type.

Comments