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 public class OrderRecoveryHandler { private final RabbitTemplate rabbitTemplate;
Dead-letter recovery with Spring AMQP
dead-letter-queue
retry
exponential-backoff
Advanced
9 steps
php
final class Route { private string $regex; private array $paramNames = [];
Compiling route patterns into regex in PHP
routing
regex
parsing
Intermediate
8 steps
java
@Component public class CorrelationIdWebFilter implements WebFilter, Ordered { public static final String CORRELATION_ID_HEADER = "X-Correlation-Id";
Correlation IDs in a Spring WebFlux filter
reactive
web filter
correlation id
Advanced
7 steps
python
from flask import Blueprint, request, url_for, jsonify, abort from flask.views import MethodView from .models import Article, db
Building a paginated JSON API with Flask MethodView
rest-api
pagination
class-based-views
Intermediate
10 steps
ruby
module DeepImmutable module_function def deep_freeze(obj)
Deep-freezing config with recursive immutability
immutability
recursion
deep-copy
Intermediate
8 steps
java
package com.example.time; import java.time.Duration; import java.time.Instant;
Adding ISO-8601 durations across time zones in Java
date-time
iso-8601
parsing
Intermediate
7 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.