Jackson ObjectMapper
Table of Contents
TODO
https://www.baeldung.com/jackson-object-mapper-tutorial
https://stackoverflow.com/questions/10113512/readvalue-and-readtree-in-jackson-when-to-use-which
You only need one ObjectMapper bean per application
- One mistake people make is create many instances of ObjectMapper in many classes across the application.
- This is just a waste of memory. There will be so many ObjectMappers in the memory.
- Their behavior will not be consistent. Using only one ObjectMapper across the application will make sure that the behavior is consistent across the application.
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ObjectMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true); // Example customization
// Add other configurations as needed
return mapper;
}
}
Sample Implementations
https://github.com/explorer436/programming-playground/tree/main/java-playground/json-processing