2022-12-17 19:04:57 +08:00
[](https://github.com/alibaba/fastjson2/actions/workflows/ci.yaml)
2022-05-11 21:57:41 +08:00
[](https://codecov.io/gh/alibaba/fastjson2/branch/main)
[](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2)
[](https://github.com/alibaba/fastjson2/releases)
[](https://openjdk.java.net/)
[](https://www.apache.org/licenses/LICENSE-2.0.html)
[](https://gitpod.io/#https://github.com/alibaba/fastjson2)
[](https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/)
[](https://github.com/alibaba/fastjson2/stargazers)
[](https://github.com/alibaba/fastjson2/fork)
[](https://github.com/alibaba/fastjson2/network/dependents)
[](https://github.com/alibaba/fastjson2/graphs/contributors)
2023-04-23 13:24:19 +08:00
##### 📖 English Documentation | 📖 [中文文档](README.md)
2023-06-10 14:52:46 +08:00
##### The issues of fastjson will be also posted on [Alibaba Cloud Developer Community](https://developer.aliyun.com/ask/)
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
# FASTJSON v2
2022-05-11 21:57:41 +08:00
2023-06-10 14:52:46 +08:00
`FASTJSON v2` is an upgrade of the `FASTJSON` , with the goal of providing a highly optimized `JSON` library for the next ten years.
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
- Supports the JSON and JSONB Protocols.
- Supports full parsing and partial parsing.
2022-05-11 21:57:41 +08:00
- Supports Java servers and Android Clients, and has big data applications.
2022-05-21 23:48:40 +08:00
- Supports Kotlin [https://alibaba.github.io/fastjson2/kotlin_en ](https://alibaba.github.io/fastjson2/kotlin_en )
2022-12-31 07:49:01 +08:00
- Supports Android 8+
- Supports `Graal Native-Image`
2022-05-21 17:53:19 +08:00
- Supports `JSON Schema` [https://alibaba.github.io/fastjson2/json_schema_cn ](https://alibaba.github.io/fastjson2/json_schema_cn )
2022-05-11 21:57:41 +08:00
2023-04-23 13:24:19 +08:00

2022-05-11 21:57:41 +08:00
Related Documents:
- `JSONB` format documentation:
2022-05-21 17:53:19 +08:00
[https://alibaba.github.io/fastjson2/jsonb_format_cn ](https://alibaba.github.io/fastjson2/jsonb_format_cn )
2022-05-11 21:57:41 +08:00
- `FASTJSON v2` 's performance has been significantly improved. For the benchmark, see here:
2022-11-06 10:45:50 +08:00
[https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark ](https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark )
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
# 1. Prepare
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
## 1.1 Download
2022-05-11 21:57:41 +08:00
`FASTJSONv2` 's groupId is different from versions `1.x` , it is instead `com.alibaba.fastjson2` :
2022-05-20 03:27:35 +08:00
`Maven` :
2022-05-11 21:57:41 +08:00
```xml
< dependency >
< groupId > com.alibaba.fastjson2< / groupId >
< artifactId > fastjson2< / artifactId >
2023-06-10 14:52:46 +08:00
< version > 2.0.33< / version >
2022-05-11 21:57:41 +08:00
< / dependency >
```
2022-05-20 03:27:35 +08:00
`Gradle` :
2022-05-14 17:52:10 +08:00
```groovy
dependencies {
2023-06-10 14:52:46 +08:00
implementation 'com.alibaba.fastjson2:fastjson2:2.0.33'
2022-05-14 17:52:10 +08:00
}
```
Find the latest version of `FASTJSONv2` at [maven.org ](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2 ).
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
## 1.2 Other modules
2022-05-14 17:52:10 +08:00
### Compatible dependence of fastjson-v1
2022-05-11 21:57:41 +08:00
If you are using `fastjson 1.2.x` , you can use the compatibility package. The compatibility package cannot guarantee 100% compatibility. Please test it yourself and report any problems.
2022-05-20 03:27:35 +08:00
`Maven` :
2022-05-11 21:57:41 +08:00
```xml
< dependency >
< groupId > com.alibaba< / groupId >
< artifactId > fastjson< / artifactId >
2023-06-10 14:52:46 +08:00
< version > 2.0.33< / version >
2022-05-11 21:57:41 +08:00
< / dependency >
```
2022-05-20 03:27:35 +08:00
`Gradle` :
2022-05-14 18:12:57 +08:00
```groovy
dependencies {
2023-06-10 14:52:46 +08:00
implementation 'com.alibaba:fastjson:2.0.33'
2022-05-14 18:12:57 +08:00
}
```
2022-05-20 03:27:35 +08:00
### `Kotlin` integration module `fastjson-kotlin`
2022-05-14 17:52:10 +08:00
2022-05-22 17:12:29 +08:00
If your project uses `kotlin` , you can use the `Fastjson-Kotlin` module, and use the characteristics of `kotlin` .
2022-05-14 17:52:10 +08:00
2022-05-20 03:27:35 +08:00
`Maven` :
2022-05-14 17:52:10 +08:00
```xml
< dependency >
2022-05-14 18:12:57 +08:00
< groupId > com.alibaba.fastjson2< / groupId >
< artifactId > fastjson2-kotlin< / artifactId >
2023-06-10 14:52:46 +08:00
< version > 2.0.33< / version >
2022-05-14 17:52:10 +08:00
< / dependency >
```
2022-05-22 17:36:08 +08:00
`Kotlin Gradle` :
2022-05-20 03:27:35 +08:00
2022-05-14 18:12:57 +08:00
```kotlin
dependencies {
2023-06-10 14:52:46 +08:00
implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.33")
2022-05-14 18:12:57 +08:00
}
```
2022-05-22 17:12:29 +08:00
### `Extension` integration module `fastjson-extension`
If your project uses a framework such as `SpringFramework` , you can use the `fastjson-extension` module, please refer to the usage [SpringFramework Support ](docs/spring_support_en.md ).
`Maven` :
```xml
< dependency >
< groupId > com.alibaba.fastjson2< / groupId >
< artifactId > fastjson2-extension< / artifactId >
2023-06-10 14:52:46 +08:00
< version > 2.0.33< / version >
2022-05-22 17:12:29 +08:00
< / dependency >
```
`Gradle` :
2022-05-22 17:36:08 +08:00
```groovy
2022-05-22 17:12:29 +08:00
dependencies {
2023-06-10 14:52:46 +08:00
implementation 'com.alibaba.fastjson2:fastjson2-extension:2.0.33'
2022-05-22 17:12:29 +08:00
}
```
2022-05-21 22:54:52 +08:00
# 2. Usage
2022-05-11 21:57:41 +08:00
The package name of `fastjson v2` is different from `fastjson v1` . It is `com.alibaba.fastjson2` . If you used `fastjson v1` before, simply change the package name.
2022-05-14 17:52:10 +08:00
### 2.1 Parse `JSON` into `JSONObject`
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-11 21:57:41 +08:00
```java
2022-05-14 17:52:10 +08:00
String text = "...";
2022-05-15 11:04:05 +08:00
JSONObject data = JSON.parseObject(text);
2022-05-14 17:52:10 +08:00
byte[] bytes = ...;
2022-05-15 11:04:05 +08:00
JSONObject data = JSON.parseObject(bytes);
2022-05-14 17:52:10 +08:00
```
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 17:52:10 +08:00
```kotlin
import com.alibaba.fastjson2.*
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
val text = ... // String
val data = text.parseObject()
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObject
```
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
### 2.2 Parse `JSON` into `JSONArray`
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-14 17:52:10 +08:00
```java
String text = "...";
2022-05-15 11:07:47 +08:00
JSONArray data = JSON.parseArray(text);
2022-05-14 17:52:10 +08:00
```
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 17:52:10 +08:00
```kotlin
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.parseArray() // JSONArray
```
### 2.3 Parse `JSON` into a Java Object
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-14 17:52:10 +08:00
```java
String text = "...";
2022-05-15 11:07:47 +08:00
User data = JSON.parseObject(text, User.class);
2022-05-14 17:52:10 +08:00
```
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 17:52:10 +08:00
```kotlin
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.to< User > () // User
val data = text.parseObject< User > () // User
```
### 2.4 Serialization Java Object to `JSON`
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-14 17:52:10 +08:00
```java
Object data = "...";
2022-05-15 11:07:47 +08:00
String text = JSON.toJSONString(data);
byte[] text = JSON.toJSONBytes(data);
2022-05-14 17:52:10 +08:00
```
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 17:52:10 +08:00
```kotlin
import com.alibaba.fastjson2.*
val data = ... // Any
val text = text.toJSONString() // String
val bytes = text.toJSONByteArray() // ByteArray
```
2022-05-20 03:27:35 +08:00
### 2.5 Use `JSONObject`, `JSONArray`
2022-05-14 17:52:10 +08:00
2022-05-14 20:39:09 +08:00
#### 2.5.1 Get simple property
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
```java
String text = "{\"id\": 2,\"name\": \"fastjson2\"}";
JSONObject obj = JSON.parseObject(text);
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
int id = obj.getIntValue("id");
String name = obj.getString("name");
```
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
```java
String text = "[2, \"fastjson2\"]";
2022-05-14 21:10:53 +08:00
JSONArray array = JSON.parseArray(text);
2022-05-11 21:57:41 +08:00
2022-05-14 21:10:53 +08:00
int id = array.getIntValue(0);
String name = array.getString(1);
2022-05-14 20:39:09 +08:00
```
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
#### 2.5.2 Get JavaBean
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-14 20:39:09 +08:00
```java
2022-05-14 21:10:53 +08:00
JSONArray array = ...
2022-05-14 20:39:09 +08:00
JSONObject obj = ...
2022-05-11 21:57:41 +08:00
2022-05-14 21:10:53 +08:00
User user = array.getObject(0, User.class);
2022-05-14 20:39:09 +08:00
User user = obj.getObject("key", User.class);
```
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 20:39:09 +08:00
```kotlin
2022-05-14 21:10:53 +08:00
val array = ... // JSONArray
2022-05-14 20:39:09 +08:00
val obj = ... // JSONObject
2022-05-11 21:57:41 +08:00
2022-05-21 22:54:52 +08:00
val user = array.to< User > (0)
val user = obj.to< User > ("key")
2022-05-11 21:57:41 +08:00
```
2022-05-14 20:39:09 +08:00
#### 2.5.3 Convert to JavaBean
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-11 21:57:41 +08:00
```java
2022-05-14 21:10:53 +08:00
JSONArray array = ...
2022-05-14 20:39:09 +08:00
JSONObject obj = ...
2022-05-11 21:57:41 +08:00
2022-05-17 03:50:48 +08:00
User user = obj.toJavaObject(User.class);
List< User > users = array.toJavaList(User.class);
2022-05-11 21:57:41 +08:00
```
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 20:39:09 +08:00
```kotlin
2022-05-14 21:10:53 +08:00
val array = ... // JSONArray
2022-05-14 20:39:09 +08:00
val obj = ... // JSONObject
2022-05-21 22:54:52 +08:00
val user = obj.to< User > () // User
2022-05-14 21:10:53 +08:00
val users = array.toList< User > () // List< User >
2022-05-14 20:39:09 +08:00
```
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
### 2.6 Serialize `JavaBean` to `JSON`
2022-05-11 21:57:41 +08:00
2022-05-20 03:27:35 +08:00
`Java` :
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
class User {
2022-05-11 21:57:41 +08:00
public int id;
public String name;
}
2022-05-14 20:39:09 +08:00
User user = new User();
user.id = 2;
user.name = "FastJson2";
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
String text = JSON.toJSONString(user);
byte[] bytes = JSON.toJSONBytes(user);
2022-05-11 21:57:41 +08:00
```
2022-05-20 03:27:35 +08:00
`Kotlin` :
2022-05-14 20:39:09 +08:00
```kotlin
class User(
var id: Int,
var name: String
)
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
val user = User()
user.id = 2
user.name = "FastJson2"
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
val text = user.toJSONString() // String
val bytes = user.toJSONByteArray() // ByteArray
2022-05-11 21:57:41 +08:00
```
2022-05-14 20:39:09 +08:00
Serialization result:
```json
{
"id" : 2,
"name" : "FastJson2"
}
```
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
# 3. Advanced usage
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
### 3.1 Use `JSONB`
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
#### 3.1.1 Serialize `JavaBean` to `JSONB`
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
User user = ...;
byte[] bytes = JSONB.toBytes(user);
byte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);
```
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
#### 3.1.2 Parse `JSONB` to `JavaBean`
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...
2022-05-15 21:04:41 +08:00
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);
2022-05-11 21:57:41 +08:00
```
2022-05-14 20:39:09 +08:00
### 3.2 Use `JSONPath`
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
#### 3.2.1 Use `JSONPath` to read partial data
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
String text = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
JSONReader parser = JSONReader.of(text);
2022-05-11 21:57:41 +08:00
Object result = path.extract(parser);
```
2022-05-14 20:39:09 +08:00
#### 3.2.2 Read part of `byte[]` data using `JSONPath`
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
JSONReader parser = JSONReader.of(bytes);
2022-05-11 21:57:41 +08:00
Object result = path.extract(parser);
```
2022-05-14 20:39:09 +08:00
#### 3.2.3 Read part of `byte[]` data using `JSONPath`
2022-05-11 21:57:41 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse
2022-05-11 21:57:41 +08:00
2022-05-14 20:39:09 +08:00
JSONReader parser = JSONReader.ofJSONB(bytes); // Use ofJSONB method
2022-05-11 21:57:41 +08:00
Object result = path.extract(parser);
```