fastjson2/README.md

391 lines
9.9 KiB
Markdown
Raw Permalink Normal View History

[![Java CI](https://img.shields.io/github/actions/workflow/status/alibaba/fastjson2/ci.yaml?branch=main&logo=github&logoColor=white)](https://github.com/alibaba/fastjson2/actions/workflows/ci.yaml)
2022-04-18 04:42:30 +08:00
[![Codecov](https://img.shields.io/codecov/c/github/alibaba/fastjson2/main?logo=codecov&logoColor=white)](https://codecov.io/gh/alibaba/fastjson2/branch/main)
[![Maven Central](https://img.shields.io/maven-central/v/com.alibaba.fastjson2/fastjson2?logo=apache-maven&logoColor=white)](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2)
[![GitHub release](https://img.shields.io/github/release/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/releases)
[![Java support](https://img.shields.io/badge/Java-8+-green?logo=java&logoColor=white)](https://openjdk.java.net/)
[![License](https://img.shields.io/github/license/alibaba/fastjson2?color=4D7A97&logo=apache)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-ready--to--code-green?label=gitpod&logo=gitpod&logoColor=white)](https://gitpod.io/#https://github.com/alibaba/fastjson2)
[![Last SNAPSHOT](https://img.shields.io/nexus/snapshots/https/oss.sonatype.org/com.alibaba.fastjson2/fastjson2?label=latest%20snapshot)](https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/)
[![GitHub Stars](https://img.shields.io/github/stars/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/stargazers)
[![GitHub Forks](https://img.shields.io/github/forks/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/fork)
[![user repos](https://badgen.net/github/dependents-repo/alibaba/fastjson2?label=user%20repos)](https://github.com/alibaba/fastjson2/network/dependents)
[![GitHub Contributors](https://img.shields.io/github/contributors/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/graphs/contributors)
##### [📖 English Documentation](README_EN.md) | 📖 中文文档
2023-04-18 20:27:05 +08:00
##### 本项目的Issues会被同步沉淀至[阿里云开发者社区](https://developer.aliyun.com/ask/)
2022-05-11 21:57:41 +08:00
2022-05-14 17:52:10 +08:00
# FASTJSON v2
2023-02-19 18:53:50 +08:00
`FASTJSON 2`是一个性能极致并且简单易用的Java JSON库。
2022-04-17 18:33:53 +08:00
2023-02-19 18:53:50 +08:00
- `FASTJSON 2`是`FASTJSON`项目的重要升级和FASTJSON 1相比性能有非常大的提升解决了autoType功能因为兼容和白名单的安全性问题。
- 性能极致性能远超过其他流行JSON库包括jackson/gson/org.json性能数据: [https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark](https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark)
- 支持JDK新特性包括`JDK 11`/`JDK 17`,针对`compact string`优化支持Record支持`GraalVM Native-Image`
2023-02-24 04:23:11 +08:00
- 完善的[`JSONPath`](https://alibaba.github.io/fastjson2/jsonpath_cn)支持,支持[SQL:2016](https://en.wikipedia.org/wiki/SQL:2016)的JSONPath语法
2023-02-19 18:53:50 +08:00
- 支持`Android 8+`客户端和服务器一套API
2022-05-21 23:48:40 +08:00
- 支持`Kotlin` [https://alibaba.github.io/fastjson2/kotlin_cn](https://alibaba.github.io/fastjson2/kotlin_cn)
- 支持`JSON Schema` [https://alibaba.github.io/fastjson2/json_schema_cn](https://alibaba.github.io/fastjson2/json_schema_cn)
2023-02-19 18:53:50 +08:00
- 新增加支持二进制格式JSONB [https://alibaba.github.io/fastjson2/jsonb_format_cn](https://alibaba.github.io/fastjson2/jsonb_format_cn)
2022-04-17 18:33:53 +08:00
![fastjson logo](https://user-images.githubusercontent.com/1063891/233821110-0c912009-4de3-4664-a27e-25274f2fa9c1.jpg)
2022-04-19 12:54:50 +08:00
2022-05-14 17:52:10 +08:00
# 1. 使用准备
2022-05-14 17:52:10 +08:00
## 1.1 添加依赖
在`fastjson v2`中,`groupId`和`1.x`不一样,是`com.alibaba.fastjson2`
`Maven`:
2022-04-17 15:09:14 +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-04-17 15:09:14 +08:00
</dependency>
```
`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
}
```
可以在 [maven.org](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2) 查看最新可用的版本。
## 1.2 其他模块
2022-05-14 17:52:10 +08:00
### `Fastjson v1`兼容模块
如果原来使用`fastjson 1.2.x`版本可以使用兼容包兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。
2022-04-17 15:09:14 +08:00
`Maven`:
2022-04-17 15:09:14 +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-04-17 15:09:14 +08:00
</dependency>
```
`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
}
```
### `Fastjson Kotlin`集成模块
如果项目使用`Kotlin`,可以使用`fastjson-kotlin`模块,使用方式上采用`kotlin`的特性。
2022-05-14 17:52:10 +08:00
2023-02-27 08:59:19 +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>
2023-02-27 08:59:19 +08:00
<!-- 有些场景需要依赖kotlin-reflect -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin-version}</version>
</dependency>
2022-05-14 17:52:10 +08:00
```
2023-02-27 08:59:19 +08:00
* `Kotlin Gradle`:
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
### `Fastjson Extension`扩展模块
如果项目使用`SpringFramework`等框架,可以使用`fastjson-extension`模块,使用方式参考 [SpringFramework Support](docs/spring_support_cn.md)。
`Maven`:
```xml
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
2023-04-18 22:01:46 +08:00
<artifactId>fastjson2-extension-spring5</artifactId>
2023-06-10 14:52:46 +08:00
<version>2.0.33</version>
2023-04-18 22:01:46 +08:00
</dependency>
```
```xml
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring6</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-spring5:2.0.33'
2023-04-18 22:01:46 +08:00
}
```
```groovy
dependencies {
2023-06-10 14:52:46 +08:00
implementation 'com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.33'
2022-05-22 17:12:29 +08:00
}
```
2022-05-21 22:54:52 +08:00
# 2. 简单使用
在`fastjson v2`中,`package`和`1.x`不一样,是`com.alibaba.fastjson2`。如果你之前用的是`fastjson1`,大多数情况直接更包名就即可。
2022-05-14 17:52:10 +08:00
### 2.1 将`JSON`解析为`JSONObject`
`Java`:
2022-04-17 15:09:14 +08:00
```java
2022-05-14 17:52:10 +08:00
String text = "...";
JSONObject data = JSON.parseObject(text);
2022-05-14 17:52:10 +08:00
byte[] bytes = ...;
JSONObject data = JSON.parseObject(bytes);
2022-05-14 17:52:10 +08:00
```
2022-04-17 15:09:14 +08:00
`Kotlin`:
2022-05-14 17:52:10 +08:00
```kotlin
import com.alibaba.fastjson2.*
2022-05-14 17:52:10 +08:00
val text = ... // String
val data = text.parseObject()
2022-05-14 17:52:10 +08:00
val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObject
```
2022-04-17 15:09:14 +08:00
2022-05-14 17:52:10 +08:00
### 2.2 将`JSON`解析为`JSONArray`
`Java`:
2022-05-14 17:52:10 +08:00
```java
String text = "...";
JSONArray data = JSON.parseArray(text);
2022-05-14 17:52:10 +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 将`JSON`解析为`Java`对象
`Java`:
2022-05-14 17:52:10 +08:00
```java
String text = "...";
User data = JSON.parseObject(text, User.class);
2022-05-14 17:52:10 +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 将`Java`对象序列化为`JSON`
`Java`:
2022-05-14 17:52:10 +08:00
```java
Object data = "...";
String text = JSON.toJSONString(data);
byte[] text = JSON.toJSONBytes(data);
2022-05-14 17:52:10 +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-14 20:39:09 +08:00
### 2.5 使用`JSONObject`、`JSONArray`
2022-05-14 17:52:10 +08:00
2022-05-14 20:39:09 +08:00
#### 2.5.1 获取简单属性
2022-05-14 20:39:09 +08:00
```java
String text = "{\"id\": 2,\"name\": \"fastjson2\"}";
JSONObject obj = JSON.parseObject(text);
2022-04-17 15:09:14 +08:00
2022-05-14 20:39:09 +08:00
int id = obj.getIntValue("id");
String name = obj.getString("name");
```
2022-04-17 15:09:14 +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-04-17 15:09:14 +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
```
#### 2.5.2 读取`JavaBean`
`Java`:
2022-04-17 15:09:14 +08:00
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-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);
```
`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 = array.to<User>(0)
val user = obj.to<User>("key")
2022-04-17 15:09:14 +08:00
```
#### 2.5.3 转为`JavaBean`
`Java`:
2022-04-17 15:09:14 +08:00
```java
2022-05-14 21:10:53 +08:00
JSONArray array = ...
2022-05-14 20:39:09 +08:00
JSONObject obj = ...
2022-04-17 15:09:14 +08:00
User user = obj.toJavaObject(User.class);
List<User> users = array.toJavaList(User.class);
2022-04-17 15:09:14 +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-14 20:39:09 +08:00
### 2.6 将`JavaBean`对象序列化为`JSON`
`Java`:
2022-04-17 15:09:14 +08:00
```java
2022-05-14 20:39:09 +08:00
class User {
public int id;
public String name;
2022-04-17 15:09:14 +08:00
}
2022-05-14 20:39:09 +08:00
User user = new User();
user.id = 2;
user.name = "FastJson2";
2022-05-14 20:39:09 +08:00
String text = JSON.toJSONString(user);
byte[] bytes = JSON.toJSONBytes(user);
2022-04-17 15:09:14 +08:00
```
`Kotlin`:
2022-05-14 20:39:09 +08:00
```kotlin
class User(
var id: Int,
var name: String
)
2022-05-14 20:39:09 +08:00
val user = User()
user.id = 2
user.name = "FastJson2"
2022-04-17 15:09:14 +08:00
2022-05-14 20:39:09 +08:00
val text = user.toJSONString() // String
val bytes = user.toJSONByteArray() // ByteArray
2022-04-17 15:09:14 +08:00
```
2022-05-14 20:39:09 +08:00
序列化结果:
2022-05-14 20:39:09 +08:00
```json
{
"id" : 2,
"name" : "FastJson2"
}
```
2022-05-14 20:39:09 +08:00
# 3. 进阶使用
2022-05-14 20:39:09 +08:00
### 3.1 使用`JSONB`
2022-04-17 15:09:14 +08:00
2022-05-14 20:39:09 +08:00
#### 3.1.1 将`JavaBean`对象序列化`JSONB`
2022-04-17 15:09:14 +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-04-17 15:09:14 +08:00
```
2022-05-14 20:39:09 +08:00
#### 3.1.2 将`JSONB`数据解析为`JavaBean`
2022-04-17 15:09:14 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);
2022-04-17 15:09:14 +08:00
```
2022-05-14 20:39:09 +08:00
### 3.2 使用`JSONPath`
2022-05-14 20:39:09 +08:00
#### 3.2.1 使用`JSONPath`读取部分数据
2022-04-17 15:09:14 +08:00
```java
2022-05-14 20:39:09 +08:00
String text = ...;
2022-04-17 15:09:14 +08:00
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
2022-05-14 20:39:09 +08:00
JSONReader parser = JSONReader.of(text);
2022-04-17 15:09:14 +08:00
Object result = path.extract(parser);
```
2022-05-14 20:39:09 +08:00
#### 3.2.2 使用`JSONPath`读取部分`byte[]`的数据
2022-04-17 15:09:14 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...;
2022-04-17 15:09:14 +08:00
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
2022-05-14 20:39:09 +08:00
JSONReader parser = JSONReader.of(bytes);
2022-04-17 15:09:14 +08:00
Object result = path.extract(parser);
```
2022-05-14 20:39:09 +08:00
#### 3.2.3 使用`JSONPath`读取部分`byte[]`的数据
2022-04-17 15:09:14 +08:00
```java
2022-05-14 20:39:09 +08:00
byte[] bytes = ...;
2022-04-17 15:09:14 +08:00
JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
2022-05-14 21:10:53 +08:00
JSONReader parser = JSONReader.ofJSONB(bytes); // 注意这里使用ofJSONB方法
2022-04-17 15:09:14 +08:00
Object result = path.extract(parser);
```