Optimizing JSON Serialization and Deserialization in Java
Fast recall
Quick revision
Core answer
Whether you know that Jackson has significant configuration options that affect performance, and that alternative libraries like jsoniter or DSL-Json exist for high-throughput scenarios.
Assuming Jackson's defaults are optimal, or using JSON for large internal service calls where a binary format like Protobuf would be much faster.
Mention AfterburnerModule for bytecode generation, streaming API for large payloads, pre-registering types with ObjectMapper to avoid reflection overhead, and when to prefer binary protocols.
Interview-ready guide
Interview answer
JSON serialization and deserialization can become a performance bottleneck when high-volume APIs process large payloads or when ObjectMapper is misused. The highest-impact fix is usually the simplest: use a singleton ObjectMapper.
Singleton ObjectMapper
ObjectMapperis expensive to instantiate — it scans annotations and initializes hundreds of serializer and deserializer mappings; creating one per request adds 1-5ms overhead- Spring Boot auto-configures a shared
ObjectMapperbean — inject it with@Autowiredand reuse it; it's thread-safe for concurrent use;new ObjectMapper()in a request handler is a common and costly mistake - Measure the impact: add a
Timeraround your serialization calls; if it shows 2-5ms per call and you're handling 1,000 req/s, that's 2-5 seconds of CPU wasted per second on ObjectMapper construction
Disabling unused features
- Disable
FAIL_ON_UNKNOWN_PROPERTIES— by default, Jackson throws an exception if the JSON contains a field not in the target class; disabling it makes deserialization forward-compatible and avoids defensive@JsonIgnorePropertiesannotations - Disable
WRITE_DATES_AS_TIMESTAMPSif you don't need timestamp format; use ISO 8601 strings for interoperability objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)prevents exceptions on plain Java objects without any annotations
For large payloads — streaming API
- Jackson's streaming API (
JsonParser,JsonGenerator) processes JSON token by token without building a full object tree — which means a 100MB JSON file can be parsed without loading all 100MB into heap - Use
objectMapper.createParser(inputStream)and iterate withparser.nextToken()for large exports or imports that would otherwise cause OutOfMemoryError
Jackson modules for speed
jackson-module-blackbird(successor toAfterburner) replaces reflection-based property access with bytecode-generated accessors — typically 10-20% faster serialization at the cost of a dependency and JVM startup warmup
Optimizing JSON Serialization and Deserialization in Java
Direct Answer
Reuse ObjectMapper as a singleton, disable unused features, use streaming API for large payloads, pre-register types, and consider binary protocols like Protobuf for high-throughput internal APIs.
Common Mistakes & Pitfalls
Assuming Jackson's defaults are optimal, or using JSON for large internal service calls where a binary format like Protobuf would be much faster.
Check your understanding
Answer these without looking back at the article.
- When would you use Protobuf instead of JSON for API communication?
- How does the Jackson AfterburnerModule improve performance?
- What is the difference between Jackson's tree model and streaming API?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.