JSON with Jackson
37 questions in 11 subtopics.
ObjectMapper configuration
How does Spring Boot configure Jackson'sObjectMapper, and how do you change a setting like the naming strategy?Is ObjectMapper thread-safe? How should it be used in a service?Spring Boot 4 moves to Jackson 3. What changes would you check when upgrading a service?Design a shared Jackson configuration for all services and explain how teams can extend it without replacing Boot's ObjectMapper.The move to Jackson 3 in Spring Boot 4 touches every service. How would you plan and sequence this change across a large fleet?Custom serializers and deserializers
Write a custom serializer that writes aMoney(amount, currency) object as "12.50 EUR" and register it.When would you use @JsonCreator, @JsonValue and mix-ins instead of a custom serializer?Jackson has been the default JSON library in Spring for over a decade and now has a major version change. How should an organisation think about dependence on such foundational libraries?Date and time handling
ALocalDateTime field is returned as an array like [2024,5,1,10,30] in one service and as a string in another. Why?A client sends dates as dd.MM.yyyy. Show how to accept this for one field and when you should avoid doing it.Which Java type would you use for timestamps in an API: LocalDateTime, OffsetDateTime or Instant? Explain the JSON format.Mobile clients in different time zones see wrong dates because services use LocalDateTime and different formats. How would you fix this across 30 services?Polymorphic types
Deserialize a list of payment methods where each JSON object has a type field (card or bank) into the correct subclass.Why is usingJsonTypeInfo.Id.CLASS or enabling default typing dangerous on data from clients?Events between services use polymorphic JSON with type information. How would you design this so producers and consumers can evolve independently?Unknown properties
What happens when a JSON request contains a field that the target class does not have, in plain Jackson and in Spring Boot?Should your API reject unknown fields in requests or ignore them? Discuss for public APIs and for internal events.Some teams want to replace JSON with Protobuf or Avro for internal traffic. How would you decide on wire formats for the company over the next decade?Jackson performance and security
Which Jackson protections exist against malicious JSON such as deeply nested arrays or very long numbers?A JSON endpoint is slow and uses a lot of memory for large responses of 100,000 rows. How would you improve it?How would you set standards for JSON serialization across your services, so that the same types look the same everywhere?Jackson has had many security advisories over the years, mostly around polymorphic deserialization. What does this history teach about long-term security posture for serialization libraries?XML with Jackson
Using Jackson'sXmlMapper (jackson-dataformat-xml), map a Book class so that id is an XML attribute and authors is written as <authors><author>..</author></authors>. Then show the code to read and write.After a dependency upgrade added jackson-dataformat-xml to the classpath, a Spring MVC JSON API started returning XML to some clients, including browsers. Explain and fix.An API must offer JSON and XML representations to partners. Would you use Jackson for both, or JAXB for XML and Jackson for JSON? Discuss for a long-lived public API.Jackson 3 migration
What are the most visible changes developers meet when moving code from Jackson 2 to Jackson 3?Your Spring Boot 3 service has custom serializers, mix-ins and aJackson2ObjectMapperBuilderCustomizer. Plan the upgrade to Spring Boot 4 with Jackson 3.Shared libraries in your company expose Jackson 2 types (ObjectMapper parameters, JsonNode return values) in their public APIs. How do you manage the Jackson 3 transition across 120 services?Tree model and map conversion
Convert aMap<String, Object> received from a message queue into a typed Customer object and back, without writing JSON strings in between.Read this JSON with Jackson's tree model and print the email of the second user, safely handling missing fields: {"users": [{"email": "a@x.com"}, {"name": "b"}]}A gateway service must forward JSON payloads while changing only two fields. Would you bind to POJOs, use the tree model, or use streaming? Discuss memory, correctness and schema evolution.Creators and constructor binding
Deserialize{"id": 7, "name": "Ann"} into an immutable class with final fields and no setters, and into a Java record. What is needed in each case?A class has two constructors, and Jackson fails with 'Cannot construct instance of Price (no Creators, like default constructor, exist): cannot deserialize from Number value'. The JSON is just 12.5. Explain and fix.Teams write API DTOs as mutable beans, Lombok builders, and records in the same code base, and deserialization bugs keep appearing. What standard would you set?Generic collections with TypeReference
This code throwsClassCastException: java.util.LinkedHashMap cannot be cast to Order:List<Order> orders = mapper.readValue(json, List.class);
orders.get(0).getId()
Page<T> readPage(String json, Class<T> itemType) that deserializes {"items": [...], "total": 3} into Page<T>. Why does a TypeReference not work here?A shared event library deserializes Envelope<T> payloads with generic types, and consumers often get maps instead of their types. How would you redesign the library API?No question matches these choices.