java
33 lines · 7 steps
Deep-copying objects via Jackson serialization
A utility that clones any object by serializing it to JSON bytes and reading it back into a fresh instance.
Explained by
highlit
1public final class DeepCopier {
2
3 private static final ObjectMapper MAPPER = new ObjectMapper()
4 .registerModule(new JavaTimeModule())
5 .findAndRegisterModules();
6
7 private DeepCopier() {
8 }
9
10 public static <T> T copy(T source, Class<T> type) {
11 if (source == null) {
12 return null;
13 }
14 try {
15 byte[] bytes = MAPPER.writeValueAsBytes(source);
16 return MAPPER.readValue(bytes, type);
17 } catch (IOException e) {
18 throw new IllegalStateException("Failed to deep-copy " + type.getSimpleName(), e);
19 }
20 }
21
22 public static <T> T copy(T source, TypeReference<T> typeRef) {
23 if (source == null) {
24 return null;
25 }
26 try {
27 byte[] bytes = MAPPER.writeValueAsBytes(source);
28 return MAPPER.readValue(bytes, typeRef);
29 } catch (IOException e) {
30 throw new IllegalStateException("Failed to deep-copy value", e);
31 }
32 }
33}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Round-tripping through a serializer is a generic way to deep-copy without writing per-class clone logic.
- 2A shared, immutable ObjectMapper is thread-safe and worth reusing as a static singleton.
- 3TypeReference overloads let you preserve generic type information that a plain Class token would erase.
Related explainers
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
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 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/deep-copying-objects-via-jackson-serialization-explained-java-46db/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.