Unravel โบ Java concepts โบ APIs and web
At a glance
A translator between the program's native language (objects) and the language of the network (JSON).
- 1mapper.readValue(json, Order.class) fills an Order by matching JSON property names to fields, setters or constructor parametersUnknown properties cause an error unless you configure it to ignore them.
- 2Annotations tune the mapping: @JsonProperty("order_id") renames, @JsonIgnore hides, @JsonInclude(NON_NULL) skips nulls, @JsonFormat sets date patterns
- 3Generic targets need a TypeReference: mapper.readValue(json, new TypeReference<List<Order>>() {}), since type erasure removes the element type
- 4JsonNode lets you read JSON as a tree when you do not have a class
- ObjectMapper is thread safe and expensive to create: build one and reuse it (Spring Boot already provides one)
- Jackson inspects classes by reflection, builds and caches a serializer or deserializer per type, and streams tokens with a low-level JsonParser
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());Order order = mapper.readValue(json, Order.class);List<Order> list = mapper.readValue(json, new TypeReference<List<Order>>() {});String back = mapper.writeValueAsString(order);- line 1 uses ObjectMapper, the core of Jackson JSON
- line 2 uses readValue, the core of Jackson JSON
- line 3 uses readValue, the core of Jackson JSON
- line 4 uses writeValueAsString, the core of Jackson JSON
Read the full explanation (about 1 min)
Jackson converts between Java objects and JSON. ObjectMapper.readValue parses JSON into an object, and writeValueAsString turns an object into JSON text.
A mental model
A translator between the program's native language (objects) and the language of the network (JSON).
How it works
mapper.readValue(json, Order.class) fills an Order by matching JSON property names to fields, setters or constructor parameters. Unknown properties cause an error unless you configure it to ignore them.
Annotations tune the mapping: @JsonProperty("order_id") renames, @JsonIgnore hides, @JsonInclude(NON_NULL) skips nulls, @JsonFormat sets date patterns.
Generic targets need a TypeReference: mapper.readValue(json, new TypeReference<List<Order>>() {}), since type erasure removes the element type.
JsonNode lets you read JSON as a tree when you do not have a class.
Going deeper
ObjectMapper is thread safe and expensive to create: build one and reuse it (Spring Boot already provides one). Register the JavaTimeModule for java.time types. Be careful deserialising untrusted JSON into polymorphic types, which has been a source of remote-code-execution bugs; do not enable default typing for outside input.
Under the hood
Jackson inspects classes by reflection, builds and caches a serializer or deserializer per type, and streams tokens with a low-level JsonParser. The first use of a type is slower than later ones.
Common mistakes
- Creating an ObjectMapper per call.
- Failing on unknown properties in a client that must tolerate new fields.
- Forgetting the TypeReference for generics.
- Enabling default typing on untrusted input.
How Unravel recognises it
When you paste code, Unravel looks for these signs of jackson json. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@JsonProperty@JsonIgnore@JsonCreator@JsonInclude@JsonFormat@JsonIgnoreProperties@JsonTypeInfo@JsonSubTypes - Types:
ObjectMapperJsonNodeTypeReference - Method calls:
readValue()writeValueAsString()
Part of a learning path
- How a Spring request really works ยท previous: Spring proxies (how annotations work) ยท next: Spring REST controllers
Related concepts
Builds on this
Frequently asked questions
What is Jackson JSON?
Jackson converts between Java objects and JSON. ObjectMapper.readValue parses JSON into an object, and writeValueAsString turns an object into JSON text.
What are the most common mistakes with Jackson JSON?
Creating an ObjectMapper per call. Failing on unknown properties in a client that must tolerate new fields. Forgetting the TypeReference for generics. Enabling default typing on untrusted input.
How can I see Jackson JSON in real code?
Paste your own Java into Unravel and it will point out where jackson json appears, explain how it works at the detail level you choose, and draw the flow of the code around it.