This issue recommends a Ali open source high-performance JSON library – FASTJSON2.
FASTJSON2 is an important upgrade to the FASTJSON project, with the goal of providing a high-performance JSON library for the next decade. Through the same API,
- Supports both JSON/JSONB protocols
- Support full and partial parsing
- Support Java server, client Android, big data scenarios
- Kotlin
- Support for Android (2.0.3.android)
- Support Graal Native Image (2.0.3.graal)
Preparation
Add dependencies
Maven:
< dependency>
< groupId> com.alibaba.fastjson2< /groupId>
< artifactId> fastjson2< /artifactId>
< version> 2.0.3< /version>
< /dependency>
Gradle:
dependencies {
implementation 'com. Alibaba. Fastjson2: fastjson2:2.0.3' < / span >
}
Other modules
- Fastjson v1 compatible module
If you used fastjson 1.2.x version, you can use the compatibility package, the compatibility package cannot guarantee 100% compatibility, please carefully test and verify, and please timely feedback if you find any problems.
Maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.3</version>
</dependency>
Gradle:
dependencies {
implementation 'com.alibaba:fastjson:2.0.3'
}
- Kotlin Integration module
If the project uses Kotlin, you can use the fastjson-kotlin module, which uses the characteristics of kotlin.
Maven:
< dependency>
< groupId> com.alibaba.fastjson2< /groupId>
< artifactId> fastjson2-kotlin< /artifactId>
< version> 2.0.3< /version>
< /dependency>
Gradle:
dependencies {
implementation(<span ">" class = "HLJS - string com. Alibaba. Fastjson2: fastjson2 - kotlin: 2.0.3" < / span >)
}
Simple use
In fastjson v2, the package is not the same as 1.x, it is com.alibaba.fastjson2. If you were using fastjson1, in most cases you can simply change the package name.
Parse JSON to JSONObject
String text = "..." ;
JSONObject data = JSON.parseObject(text);
byte[] bytes = ... ;
JSONObject data = JSON.parseObject(bytes);
Parse JSON to JSONArray
String text = "..." ;
JSONArray data = JSON.parseArray(text);
Parsing JSON into Java objects
String text = "..." ;
User data = JSON.parseObject(text, User.class);
Serializes Java objects to JSON
Object data = "..." ;
String text = JSON.toJSONString(data);
byte[] text = JSON.toJSONBytes(data);
Use JSONObject, JSONArray
Get simple properties
String text = "{\"id\": 2,\"name\": \"fastjson2\"}";
JSONObject obj = JSON.parseObject(text);
int id = obj.getIntValue("id");
String name = obj.getString("name");
Read JavaBean
JSONArray array = ...
JSONObject obj = ...
User user = array.getObject(0, User.class);
User user = obj.getObject("key", User.class);
转为JavaBean
JSONArray array = ...
JSONObject obj = ...
User user = obj.toJavaObject(User.class);
List<User> users = array.toJavaList(User.class);
Serializes JavaBean objects to JSON
class User {
public int id;
public String name;
}
User user = new User();
user.id = 2;
user.name = "FastJson2";
String text = JSON.toJSONString(user);
byte[] bytes = JSON.toJSONBytes(user);
Serialization result
{
"id" : 2,
"name" : "FastJson2"
}
Advanced use
Use JSONB
Serializing a JavaBean object JSONB
User user = ... ;
byte[] bytes = JSONB.toBytes(user);
byte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);
Parsing JSONB data to JavaBean
byte[] bytes = ...
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);
Use JSONPath
Read some data using JSONPath
String text = ... ;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse improves performance
JSONReader parser = JSONReader.of(text);
Object result = path.extract(parser);
Read part of byte[] using JSONPath
byte[] bytes = ... ;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse can improve performance
JSONReader parser = JSONReader.of(bytes);
Object result = path.extract(parser);
Read part of byte[] data using JSONPath
byte[] bytes = ... ;
JSONPath path = JSONPath.of("$.id"); // Caching and reuse improves performance
JSONReader parser = JSONReader.ofJSONB(bytes); // Note that the ofJSONB method is used here
Object result = path.extract(parser);
—END—
Open source protocol: Apache2.0