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
public final class Ulid { private static final char[] ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".toCharArray(); private static final SecureRandom RANDOM = new SecureRandom();
How to generate a ULID in Java
base32-encoding
bit-manipulation
identifiers
Intermediate
8 steps
java
@RestController @RequestMapping("/api/reports") public class ReportController {
Header-driven endpoints in a Spring controller
rest api
request headers
dependency injection
Intermediate
7 steps
php
<?php namespace App\FeatureFlags;
How a feature flag evaluator decides
feature-flags
rollout
hashing
Intermediate
8 steps
java
public final class CaseConverter { private static final Pattern CAMEL_BOUNDARY = Pattern.compile("([a-z0-9])([A-Z])|([A-Z]+)([A-Z][a-z])");
Converting camelCase to snake_case in Java
regex
string-manipulation
utility-class
Intermediate
7 steps
java
@Component public class RegionCacheWarmer implements SmartInitializingSingleton { private static final Logger log = LoggerFactory.getLogger(RegionCacheWarmer.class);
Warming a Spring cache at startup
caching
startup-hook
dependency-injection
Intermediate
7 steps
php
<?php final class TaskProgressReporter {
Tracking task progress in a JSON file
state-persistence
atomic-writes
json
Intermediate
9 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.