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
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
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.