Why use ObjectMapper ?

Sanjay Singh
Apr 3, 2022

--

Reading and Writing Object Using ObjectMapper

We can use it to parse or deserialize JSON content into a Java object.

Java Object to JSON

ObjectMapper objectMapper = new ObjectMapper(); Car car = new Car(“yellow”, “renault”); objectMapper.writeValue(new File(“target/car.json”), car);

JSON to Java Object

String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
Car car = objectMapper.readValue(json, Car.class);

The readValue() function also accepts other forms of input, such as a file containing JSON string:

Car car = objectMapper.readValue(new File("src/test/resources/json_car.json"), Car.class);

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

No responses yet