Class ObjectMapper

java.lang.Object
tools.jackson.databind.ObjectMapper
All Implemented Interfaces:
Serializable, TreeCodec, Versioned
Direct Known Subclasses:
JsonMapper

public class ObjectMapper extends Object implements TreeCodec, Versioned, Serializable
ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions. In addition to directly reading and writing JSON (and with different underlying TokenStreamFactory configuration, other formats), it is also the mechanism for creating ObjectReaders and ObjectWriters which offer more advancing reading/writing functionality.

Construction of mapper instances proceeds either via no-arguments constructor (producing instance with default configuration); or through one of two build methods. First build method is the static builder() on exact type and second rebuild() method on an existing mapper. Former starts with default configuration (same as one that no-arguments constructor created mapper has), and latter starts with configuration of the mapper it is called on. In both cases, after configuration (including addition of JacksonModules) is complete, instance is created by calling MapperBuilder.build() method.

Mapper (and ObjectReaders, ObjectWriters it constructs) will use instances of JsonParser and JsonGenerator for implementing actual reading/writing of JSON. Note that although most read and write methods are exposed through this class, some of the functionality is only exposed via ObjectReader and ObjectWriter: specifically, reading/writing of longer sequences of values is only available through ObjectReader.readValues(InputStream) and ObjectWriter.writeValues(OutputStream).

Simplest usage is of form:

  final ObjectMapper mapper = new ObjectMapper(); // can use static singleton, inject: just make sure to reuse!
  MyValue value = new MyValue();
  // ... and configure
  File newState = new File("my-stuff.json");
  mapper.writeValue(newState, value); // writes JSON serialization of MyValue instance
  // or, read
  MyValue older = mapper.readValue(new File("my-older-stuff.json"), MyValue.class);

  // Or if you prefer JSON Tree representation:
  JsonNode root = mapper.readTree(newState);
  // and find values by, for example, using a JsonPointer expression:
  int age = root.at("/personal/age").getValueAsInt();

Mapper instances are fully thread-safe as of Jackson 3.0.

Note on caching: root-level deserializers are always cached, and accessed using full (generics-aware) type information. This is different from caching of referenced types, which is more limited and is done only for a subset of all deserializer types. The main reason for difference is that at root-level there is no incoming reference (and hence no referencing property, no referral information or annotations to produce differing deserializers), and that the performance impact greatest at root level (since it'll essentially cache the full graph of deserializers involved).

See Also: