Posted In: Jackson 2X, JSON

Is Jackson ObjectMapper threadsafe?

Use Case – I want to make only one/few ObjectMapper objects and share them in various classes. In this case we could use immutable ObjectReader and ObjectWriter classes

Comment from ObjectWriter class

/**
 * Builder object that can be used for per-serialization configuration of
 * serialization parameters, such as JSON View and root type to use.
 * (and thus fully thread-safe with no external synchronization);
 * new instances are constructed for different configurations.
 * Instances are initially constructed by {@link ObjectMapper} and can be
 * reused in completely thread-safe manner with no explicit synchronization
 */

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.ObjectWriter

ObjectWriter objectWriter = mapper.writer();
ObjectReader objectReader = mapper.reader().forType(Employee.class);

package com.example.json;

import java.io.Serializable;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;

public class ObjectMapperThreadSafe {

	// though ObjectMapper is threadsafe ObjectMapper configuration can be changed
	final static ObjectMapper mapper = new ObjectMapper();

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

	public void serialize() throws Exception {
		Employee employee = new Employee();
		employee.setFirstName("Java");
		employee.setLastName("Usecase");

		String json = mapper.writeValueAsString(employee);

		// threadsafe immutable ObjectWriter object
		ObjectWriter objectWriter = mapper.writer();
		json = objectWriter.writeValueAsString(employee);
		System.out.println(json);

		// threadsafe immutable ObjectReader object
		ObjectReader objectReader = mapper.reader().forType(Employee.class);
		Employee employee2 = objectReader.readValue(json);
		System.out.println(employee2.getFirstName());
	}
}

class Employee 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;
	}
}

Tags: Tags: ,

by , on November 21st, 2018