Introduction
Fastjson is one of the most widely adopted JSON libraries in the Java ecosystem, developed by Alibaba. The latest generation (fastjson2) was rewritten from scratch for better security and performance while maintaining API compatibility. It provides a concise API for serialization, deserialization, and JSON path queries.
What Fastjson Does
- Serializes Java objects to JSON strings with automatic field discovery
- Deserializes JSON into typed Java objects, collections, and generic types
- Supports JSONPath expressions for querying and extracting nested values
- Provides streaming readers and writers for processing large JSON documents
- Integrates with Spring MVC and Spring Boot as a drop-in message converter
Architecture Overview
Fastjson2 uses a bytecode-generation approach (ASM) to create optimized serializers and deserializers at runtime, avoiding reflection overhead on hot paths. The parser operates directly on UTF-8 byte arrays when possible, skipping intermediate String allocation. A modular design separates the core engine from framework integrations (Spring, Kotlin, Android), letting projects pull only what they need.
Self-Hosting & Configuration
- Add the fastjson2 Maven or Gradle dependency — zero external services required
- Use @JSONField annotations to customize field names, formats, and ordering
- Register custom serializers and deserializers via SerializeConfig and ParserConfig
- Enable autoType with an explicit accept-list for safe polymorphic deserialization
- Swap in FastJsonHttpMessageConverter for Spring Boot JSON handling
Key Features
- Consistently ranks among the fastest Java JSON libraries in independent benchmarks
- Direct UTF-8 byte array parsing avoids unnecessary String allocations
- JSONPath support for querying deeply nested structures without full deserialization
- Kotlin extension module with data class and null-safety support
- Android-optimized build for mobile applications
Comparison with Similar Tools
- Jackson — the default JSON library in Spring Boot with broad ecosystem integration; Fastjson2 trades Jackson's plugin ecosystem for raw speed
- Gson — Google's simple and lenient JSON library; Fastjson2 is significantly faster but Gson is more forgiving with malformed input
- Moshi — Kotlin-first JSON library by Square; ideal for Kotlin-native projects while Fastjson2 targets the broader Java ecosystem
- org.json — reference implementation with a basic API; lacks the performance optimizations and annotation support of Fastjson2
- DSL-JSON — another high-performance option that compiles schemas at build time; Fastjson2 offers a more conventional runtime API
FAQ
Q: Should I use fastjson or fastjson2? A: Use fastjson2. It is the actively maintained successor with improved security, performance, and API design.
Q: Is Fastjson safe from the autoType deserialization vulnerabilities? A: Fastjson2 disabled autoType by default and requires an explicit allow-list, addressing the security issues found in the original library.
Q: Can I use Fastjson with Spring Boot? A: Yes. Add the fastjson2-extension-spring6 module and register FastJsonHttpMessageConverter as a bean.
Q: How does Fastjson2 compare to Jackson in speed? A: In most benchmarks fastjson2 serializes and deserializes faster than Jackson, though the margin depends on payload shape and JDK version.