Posted In: Jackson 2X, JSON

Create custom serializer using Jackson 2.X

Use Case – While creating JSON from POJO I want to add additional logic or fields to the JSON. In the following example let us add full name which is concatenation of first name and last name. In such cases we could use Jackson custom serializer class.

Maven – Jackson 2X

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.9.7</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.9.7</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.9.7</version>
</dependency>

Solution – com.fasterxml.jackson.databind.ser.std.StdSerializer

Following code will register new custom serializer with ObjectMapper
SimpleModule module = new SimpleModule();
module.addSerializer(User.class, new UserSerializer());
mapper.registerModule(module);

package com.example.json;

import java.io.IOException;
import java.io.Serializable;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class JacksonSerialize {

	public static void main(String[] args) throws Exception {
		JacksonSerialize jacksonSerialize = new JacksonSerialize();
		jacksonSerialize.serialize();
	}

	public void serialize() throws Exception {
		User user = new User();
		user.setFirstName("Java");
		user.setLastName("Usecase");
		ObjectMapper mapper = new ObjectMapper();

		SimpleModule module = new SimpleModule();
		module.addSerializer(User.class, new UserSerializer());
		mapper.registerModule(module);

		String json = mapper.writeValueAsString(user);
		System.out.println(json);
	}
}

class User implements Serializable {
	private static final long serialVersionUID = 5590115870809984678L;
	private String firstName;
	private String lastName;
	private String fullName;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
}

class UserSerializer extends StdSerializer<User> {
	private static final long serialVersionUID = -4267023683673333834L;

	public UserSerializer() {
		this(null);
	}

	public UserSerializer(Class<User> t) {
		super(t);
	}

	@Override
	public void serialize(User value, JsonGenerator jgen, SerializerProvider provider)
			throws IOException, JsonProcessingException {

		jgen.writeStartObject();
		jgen.writeStringField("firstName", value.getFirstName());
		jgen.writeStringField("lastName", value.getLastName());
		jgen.writeStringField("fullName", value.getFirstName() + " " + value.getLastName());
		jgen.writeEndObject();
	}
}

Tags:

by , on November 21st, 2018