Jackson - ObjectMapper Class

ObjectMapper is the main actor class of Jackson library. ObjectMapper class 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. It is also highly customizable to work both with different styles of JSON content, and to support more advanced Object concepts such as polymorphism and Object identity. ObjectMapper also acts as a factory for more advanced ObjectReader and ObjectWriter classes.
Following is the declaration for com.fasterxml.jackson.databind.ObjectMapper class −
public class ObjectMapper
   extends ObjectCodec
      implements Versioned, Serializable
Customized TypeResolverBuilder that provides type resolver builders used with so-called "default typing" (see enableDefaultTyping() for details).
Enumeration used with enableDefaultTyping() to specify what kind of types (classes) default typing should be used for.
protected DeserializationConfig _deserializationConfig - Configuration object that defines basic global settings for the serialization process.
protected DeserializationConfig _deserializationConfig - Configuration object that defines basic global settings for the serialization process.
protected DefaultDeserializationContext _deserializationContext - Blueprint context object; stored here to allow custom sub-classes.
protected DefaultDeserializationContext _deserializationContext - Blueprint context object; stored here to allow custom sub-classes.
protected InjectableValues _injectableValues - Provider for values to inject in deserialized POJOs.
protected InjectableValues _injectableValues - Provider for values to inject in deserialized POJOs.
protected JsonFactory _jsonFactory - Factory used to create JsonParser and JsonGenerator instances as necessary.
protected JsonFactory _jsonFactory - Factory used to create JsonParser and JsonGenerator instances as necessary.
protected SimpleMixInResolver _mixIns - Mapping that defines how to apply mix-in annotations: key is the type to received additional annotations, and value is the type that has annotations to "mix in".
protected SimpleMixInResolver _mixIns - Mapping that defines how to apply mix-in annotations: key is the type to received additional annotations, and value is the type that has annotations to "mix in".
protected ConfigOverrides _propertyOverrides - Currently active per-type configuration overrides, accessed by declared type of property.
protected ConfigOverrides _propertyOverrides - Currently active per-type configuration overrides, accessed by declared type of property.
protected Set<Object> _registeredModuleTypes - Set of module types (as per Module.getTypeId() that have been registered; kept track of iff MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS is enabled, so that duplicate registration calls can be ignored (to avoid adding same handlers multiple times, mostly).
protected Set<Object> _registeredModuleTypes - Set of module types (as per Module.getTypeId() that have been registered; kept track of iff MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS is enabled, so that duplicate registration calls can be ignored (to avoid adding same handlers multiple times, mostly).
protected ConcurrentHashMap<JavaType,JsonDeserializer<Object>> _rootDeserializers - We will use a separate main-level Map for keeping track of root-level deserializers.
protected ConcurrentHashMap<JavaType,JsonDeserializer<Object>> _rootDeserializers - We will use a separate main-level Map for keeping track of root-level deserializers.
protected SerializationConfig _serializationConfig - Configuration object that defines basic global settings for the serialization process.
protected SerializationConfig _serializationConfig - Configuration object that defines basic global settings for the serialization process.
protected SerializerFactory _serializerFactory - Serializer factory used for constructing serializers.

protected SerializerFactory _serializerFactory - Serializer factory used for constructing serializers.

protected DefaultSerializerProvider _serializerProvider - Object that manages access to serializers used for serialization, including caching.
protected DefaultSerializerProvider _serializerProvider - Object that manages access to serializers used for serialization, including caching.
protected SubtypeResolver _subtypeResolver - Thing used for registering sub-types, resolving them to super/sub-types as needed.
protected SubtypeResolver _subtypeResolver - Thing used for registering sub-types, resolving them to super/sub-types as needed.
protected TypeFactory _typeFactory - Specific factory used for creating JavaType instances; needed to allow modules to add more custom type handling (mostly to support types of non-Java JVM languages).
protected TypeFactory _typeFactory - Specific factory used for creating JavaType instances; needed to allow modules to add more custom type handling (mostly to support types of non-Java JVM languages).
protected static AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR  
protected static AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR  
protected static BaseSettings DEFAULT_BASE - Base settings contain defaults used for all ObjectMapper instances.
protected static BaseSettings DEFAULT_BASE - Base settings contain defaults used for all ObjectMapper instances.
protected static VisibilityChecker<?> STD_VISIBILITY_CHECKER 
protected static VisibilityChecker<?> STD_VISIBILITY_CHECKER 
Default constructor, which will construct the default JsonFactory as necessary, use SerializerProvider as its SerializerProvider, and BeanSerializerFactory as its SerializerFactory.
Constructs instance that uses specified JsonFactory for constructing necessary JsonParsers and/or JsonGenerators.
Constructs instance that uses specified JsonFactory for constructing necessary JsonParsers and/or JsonGenerators, and uses given providers for accessing serializers and deserializers.
Copy-constructor, mostly used to support copy().
This class inherits methods from the following classes:
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say C:/> Jackson_WORKSPACE
File: JacksonTester.java
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTester {
   public static void main(String args[]){
   
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";
      
      //map json to student
      try{
         Student student = mapper.readValue(jsonString, Student.class);
         
         System.out.println(student);
         
         jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
         
         System.out.println(jsonString);
      }
      catch (JsonParseException e) { e.printStackTrace();}
      catch (JsonMappingException e) { e.printStackTrace(); }
      catch (IOException e) { e.printStackTrace(); }
   }
}

class Student {
   private String name;
   private int age;
   public Student(){}
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String toString(){
      return "Student [ name: "+name+", age: "+ age+ " ]";
   }
}
Verify the result
Compile the classes using javac compiler as follows:
C:\Jackson_WORKSPACE>javac JacksonTester.java
Now run the jacksonTester to see the result:
C:\Jackson_WORKSPACE>java JacksonTester
Verify the Output
Student [ name: Mahesh, age: 21 ]
{
  "name" : "Mahesh",
  "age" : 21
}

 Print
 Add Notes
 Bookmark this page