Which Jackson class is primarily used for converting Java objects to JSON?
A) ObjectMapper
B) JsonMapper
C) JsonNode
D) JsonFactory
Answer:
A) ObjectMapper
Explanation:
The ObjectMapper
class is the core class in Jackson used for converting Java objects to JSON and vice versa. It provides methods for serializing Java objects into JSON strings and deserializing JSON strings back into Java objects.
For example:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(someObject);
SomeClass obj = objectMapper.readValue(json, SomeClass.class);
In this example, ObjectMapper
is used to serialize a Java object to a JSON string and deserialize a JSON string back to a Java object of type SomeClass
.