java
36 lines · 8 steps
Configuring a reusable Jackson ObjectMapper
One tuned ObjectMapper drives several serialization variants for Order objects.
Explained by
highlit
1public class OrderSerializer {
2
3 private final ObjectMapper mapper;
4
5 public OrderSerializer() {
6 this.mapper = JsonMapper.builder()
7 .addModule(new JavaTimeModule())
8 .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
9 .serializationInclusion(JsonInclude.Include.NON_NULL)
10 .build();
11 this.mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
12 }
13
14 public String toJson(Order order) throws JsonProcessingException {
15 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(order);
16 }
17
18 public byte[] toBytes(Order order) throws JsonProcessingException {
19 return mapper.writeValueAsBytes(order);
20 }
21
22 public void writeTo(Path target, Order order) throws IOException {
23 try (OutputStream out = Files.newOutputStream(target)) {
24 mapper.writeValue(out, order);
25 }
26 }
27
28 public String toJsonView(Order order) throws JsonProcessingException {
29 return mapper.writerWithView(Views.Public.class).writeValueAsString(order);
30 }
31
32 public String toJsonArray(List<Order> orders) throws JsonProcessingException {
33 ObjectWriter writer = mapper.writerFor(new TypeReference<List<Order>>() {});
34 return writer.writeValueAsString(orders);
35 }
36}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Build and configure a single ObjectMapper once, then reuse it for every serialization call.
- 2ObjectMapper's writer methods return specialized writers without mutating the shared mapper.
- 3TypeReference preserves generic type information that Java erasure would otherwise lose.
Related explainers
java
package com.example.payments.config; import com.stripe.StripeClient; import org.springframework.beans.factory.annotation.Value;
Swapping payment gateways by profile in Spring
dependency-injection
profiles
configuration
Intermediate
6 steps
rust
use std::env; use std::time::Duration; #[derive(Debug, Clone)]
Loading typed config from environment variables in Rust
configuration
error-handling
generics
Intermediate
7 steps
java
public class EventBus { private final Map<Class<?>, List<Subscriber>> subscribers = new ConcurrentHashMap<>(); private final Executor executor;
Building an annotation-driven EventBus in Java
publish-subscribe
reflection
annotations
Intermediate
7 steps
ruby
module Api module V2 class ArticlesController < Api::BaseController before_action :set_article, only: %i[show update destroy]
Building a versioned JSON API controller in Rails
rest-api
serialization
pagination
Intermediate
10 steps
typescript
import { Injectable, Scope, Inject } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express';
Request-scoped context service in NestJS
dependency-injection
request-scope
immutability
Intermediate
8 steps
java
package com.example.maintenance; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
How a scheduled cleanup job runs in Spring
scheduling
cron
transactions
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/configuring-a-reusable-jackson-objectmapper-explained-java-5e11/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.