fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
Top Related Projects
Quick Overview
Fastjson is a high-performance JSON processor for Java, developed by Alibaba. It provides fast JSON parsing and generation capabilities, with features like serialization and deserialization of Java objects to and from JSON.
Pros
- Extremely fast performance compared to other JSON libraries
- Supports both serialization and deserialization of Java objects
- Lightweight and has minimal dependencies
- Extensive configuration options for customization
Cons
- Has had security vulnerabilities in the past, requiring frequent updates
- Documentation can be lacking, especially for advanced features
- Some inconsistencies in behavior between different versions
- Limited support for certain complex data structures
Code Examples
- Serializing a Java object to JSON:
Person person = new Person("John Doe", 30);
String jsonString = JSON.toJSONString(person);
System.out.println(jsonString);
- Deserializing JSON to a Java object:
String jsonString = "{\"name\":\"John Doe\",\"age\":30}";
Person person = JSON.parseObject(jsonString, Person.class);
System.out.println(person.getName() + " is " + person.getAge() + " years old");
- Working with JSON arrays:
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
String jsonArray = JSON.toJSONString(people);
List<Person> parsedPeople = JSON.parseArray(jsonArray, Person.class);
for (Person p : parsedPeople) {
System.out.println(p.getName());
}
Getting Started
To use Fastjson in your Java project, first add the dependency to your build file:
For Maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
For Gradle:
implementation 'com.alibaba:fastjson:1.2.79'
Then, you can start using Fastjson in your Java code:
import com.alibaba.fastjson.JSON;
// Your code here
String jsonString = JSON.toJSONString(yourObject);
YourClass obj = JSON.parseObject(jsonString, YourClass.class);
Competitor Comparisons
A Java serialization/deserialization library to convert Java Objects into JSON and back
Pros of Gson
- More mature and stable, with better documentation and community support
- Stricter type safety and better handling of complex object graphs
- Smaller library size, leading to reduced application footprint
Cons of Gson
- Generally slower performance compared to Fastjson
- Less flexible configuration options for customizing serialization/deserialization
Code Comparison
Gson:
Gson gson = new Gson();
String json = gson.toJson(myObject);
MyClass obj = gson.fromJson(json, MyClass.class);
Fastjson:
String json = JSON.toJSONString(myObject);
MyClass obj = JSON.parseObject(json, MyClass.class);
Key Differences
- Gson focuses on simplicity and type safety, while Fastjson prioritizes performance and flexibility
- Fastjson offers more features out-of-the-box, such as support for circular references and custom serializers
- Gson has better integration with other Google libraries and Android development
Use Cases
- Choose Gson for projects requiring strict type safety and stable, well-documented JSON processing
- Opt for Fastjson when performance is critical and you need advanced features like circular reference support
Community and Maintenance
- Gson has a larger community and more frequent updates from Google
- Fastjson has a strong presence in the Chinese developer community and is actively maintained by Alibaba
Main Portal page for the Jackson project
Pros of Jackson
- More comprehensive and feature-rich, supporting a wider range of data formats beyond JSON
- Better performance for large datasets and complex object graphs
- More active development and community support, with frequent updates and bug fixes
Cons of Jackson
- Steeper learning curve due to its extensive feature set
- Larger library size, which may impact application size and startup time
- More complex configuration for advanced use cases
Code Comparison
Jackson:
ObjectMapper mapper = new ObjectMapper();
MyObject obj = mapper.readValue(jsonString, MyObject.class);
String json = mapper.writeValueAsString(obj);
Fastjson:
MyObject obj = JSON.parseObject(jsonString, MyObject.class);
String json = JSON.toJSONString(obj);
Both libraries offer similar basic functionality for JSON serialization and deserialization. Jackson provides more options for customization and handling complex scenarios, while Fastjson focuses on simplicity and ease of use.
Jackson is generally preferred for enterprise-level applications due to its robustness and extensive features. Fastjson may be more suitable for simpler projects or when performance is the primary concern, especially for small to medium-sized datasets.
A reference implementation of a JSON package in Java.
Pros of JSON-java
- Simpler API and easier to use for basic JSON operations
- More stable and mature codebase with fewer reported vulnerabilities
- Better documentation and examples for developers
Cons of JSON-java
- Slower performance, especially for large JSON datasets
- Fewer advanced features compared to Fastjson
- Less active development and community support
Code Comparison
JSON-java:
JSONObject obj = new JSONObject();
obj.put("name", "John");
obj.put("age", 30);
String jsonString = obj.toString();
Fastjson:
JSONObject obj = new JSONObject();
obj.put("name", "John");
obj.put("age", 30);
String jsonString = JSON.toJSONString(obj);
Both libraries offer similar basic functionality for creating and manipulating JSON objects. However, Fastjson provides additional methods like toJSONString()
for more efficient serialization.
JSON-java is generally easier to use and more suitable for smaller projects or those prioritizing stability. Fastjson offers better performance and more advanced features, making it a better choice for large-scale applications or projects requiring high-speed JSON processing. However, developers should be aware of potential security issues with Fastjson and ensure they're using the latest version with all security patches applied.
A modern JSON library for Kotlin and Java.
Pros of Moshi
- Better null safety and type-safe adapters
- Smaller library size and faster performance
- More idiomatic Kotlin support
Cons of Moshi
- Less flexible configuration options
- Steeper learning curve for advanced usage
- Limited built-in support for certain data types
Code Comparison
Fastjson:
String jsonString = JSON.toJSONString(object);
MyObject myObject = JSON.parseObject(jsonString, MyObject.class);
Moshi:
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(MyObject::class.java)
val jsonString = adapter.toJson(myObject)
val parsedObject = adapter.fromJson(jsonString)
Moshi offers a more type-safe approach with its adapters, while Fastjson provides a simpler API for quick serialization and deserialization. Moshi's code is more verbose but offers better control over the process.
Fastjson is known for its high performance and ease of use, especially for simple use cases. However, Moshi provides better null safety and is generally considered more maintainable for larger projects.
Moshi's Kotlin support makes it a preferred choice for Kotlin developers, while Fastjson might be more appealing to those working primarily with Java.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
FASTJSON 2.0.x has been released, faster and more secure, we recommend you upgrade to the latest version.
Fastjson Goals
- Provide the best performance on the server-side and android client
- Provide simple toJSONString() and parseObject() methods to convert Java objects to JSON and vice-versa
- Allow pre-existing unmodifiable objects to be converted to and from JSON
- Extensive support of Java Generics
- Allow custom representations for objects
- Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
Documentation
- Documentation Home
- Contributing Code
- Frequently Asked Questions
- FASTJSON 1.x User Upgrade Guid https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Benchmark
- Eishay benchmark https://github.com/eishay/jvm-serializers/wiki
- fastjson2 benchmark https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
Download
Maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.31</version>
</dependency>
Gradle via JCenter
compile 'com.alibaba:fastjson:2.0.28'
Please see this Wiki Download Page for more repository info.
License
Fastjson is released under the Apache 2.0 license.
Copyright 1999-2020 Alibaba Group Holding Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at the following link.
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot