BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ APIs and web

I/O and data

Jackson JSON

Jackson converts between Java objects and JSON.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A translator between the program's native language (objects) and the language of the network (JSON).

What happens, in order
  1. 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.
  2. 2Annotations tune the mapping: @JsonProperty("order_id") renames, @JsonIgnore hides, @JsonInclude(NON_NULL) skips nulls, @JsonFormat sets date patterns
  3. 3Generic targets need a TypeReference: mapper.readValue(json, new TypeReference<List<Order>>() {}), since type erasure removes the element type
  4. 4JsonNode lets you read JSON as a tree when you do not have a class
Remember
Watch out
Creating an ObjectMapper per callFailing on unknown properties in a client that must tolerate new fieldsForgetting the TypeReference for genericsEnabling default typing on untrusted input
Example (highlighted lines explain it)
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);
  1. line 1 uses ObjectMapper, the core of Jackson JSON
  2. line 2 uses readValue, the core of Jackson JSON
  3. line 3 uses readValue, the core of Jackson JSON
  4. 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:

Part of a learning path

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.