Jackson custom serializer

In this post we will provide an example of how to configure a Jackson custom serializer when using the Jackson library.


You will need to add the following dependency

Add the following classes if you wish to follow the example below


Jackson custom serializer

The first example shows the default behaviour. It uses the ObjectMapper to convert a User object to JSON.

Running the above code produces the following JSON

Now lets say we do not want to have an address object in the JSON and instead we want to have line1 and postcode as top level fields. In this scenario we will need to provide a custom serializer as shown below.

Below is our custom UserSerializer, it ensures that line1 and postcode are top level fields and the address object is removed.

The code snippet below, registers a module which has a custom serializer for the User.class.

Running the above code produces the following JSON.

I hope this post has provided some insight on how to use custom serializers with Jackson.

Similar posts

  1. Jackson Custom date serializer
  2. Convert a List to a CSV file
  3. Java WatchService example
  4. Java 8 Lambda Expressions


  1. Jackson API Documentation

Leave a Reply