Jackson allows you to specify serializers through annotations. For example, see the Trivial example below:
@JsonSerialize(using FooToStringSerializer) public class Foo implements Serializable { private String bar; public Foo(String bar) { this.bar = bar; }
Then, if all I wanted to see when the object was serialized was bar , I would create a serializer as follows:
public class FooToStringSerializer extends JsonSerializer<Foo> { @Override public void serialize(final Foo value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException { jgen.writeObject(value.getBar()); }
For deserialization, you can create a deserializer and register it using the ObjectMapper , which will perform the deserialization.
To register a deserializer with an object mapper, follow these steps:
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Item.class, new FooDeserializer()); mapper.registerModule(module);
For a very simple example of custom deserialization see this link: http://www.baeldung.com/jackson-deserialization
source share