The Wayback Machine - https://web.archive.org/web/20250120035955/https://github.com/alibaba/fastjson2
Skip to content

🚄 FASTJSON2 is a Java JSON library with excellent performance.

License

Notifications You must be signed in to change notification settings

alibaba/fastjson2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Java CI Codecov Maven Central GitHub release Java support License Gitpod Ready-to-Code Last SNAPSHOT GitHub Stars GitHub Forks user repos GitHub Contributors

📖 English Documentation | 📖 中文文档
本项目的Issues会被�?�步沉淀至阿里云开�?�者社区

FASTJSON v2

FASTJSON 2是一个性能�?致并且简�?�易用的Java JSON库。

  • FASTJSON 2是FASTJSON项目的�?�?�?�级,和FASTJSON 1相比,性能有�?�常大的�??�?�,解决了autoType功能因为兼容和白�??�?�的安全性问题。
  • 性能�?致,性能远超过其他�?行JSON库,包括jackson/gson/org.json,性能数�?�: https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
  • 支�?JDK新特性,包括JDK 11/JDK 17,针对compact string优化,支�?Record,支�?GraalVM Native-Image
  • 完善的JSONPath支�?,支�?SQL:2016çš„JSONPath语法
  • 支�?Android 8+,客户端和�?务器一套API
  • 支�?Kotlin https://alibaba.github.io/fastjson2/kotlin_cn
  • 支�?JSON Schema https://alibaba.github.io/fastjson2/json_schema_cn
  • 新增加支�?二进制格�?JSONB https://alibaba.github.io/fastjson2/jsonb_format_cn

fastjson logo

1. 使用准备

1.1 添加�?赖

在fastjson v2中,groupId和1.x�?一样,是com.alibaba.fastjson2:

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>2.0.54</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.54'
}

�?�以在 maven.org 查看最新�?�用的版本。

1.2 其他模�?�

Fastjson v1兼容模�?�

如果原�?�使用fastjson 1.2.x版本,�?�以使用兼容包,兼容包�?能�?�?100%兼容,请仔细测试验�?,�?�现问题请�?�时�??馈。

Maven:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.54</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba:fastjson:2.0.54'
}

Fastjson Kotlin集�?模�?�

如果项目使用Kotlin,�?�以使用fastjson-kotlin模�?�,使用方�?上采用kotlin的特性。

  • Maven:
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-kotlin</artifactId>
    <version>2.0.54</version>
</dependency>

酌情添加标准库(kotlin-stdlib)�?�??射库(kotlin-reflect), 其中若使用数�?�类(data class)�?通过构造函数传入�?�数则添加�??射库。

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>${kotlin-version}</version>
</dependency>

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
    <version>${kotlin-version}</version>
</dependency>
  • Kotlin Gradle:
dependencies {
    implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.54")
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
    implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")
}

Fastjson Extension扩展模�?�

如果项目使用SpringFramework等框架,�?�以使用fastjson-extension模�?�,使用方�?�?�考 SpringFramework Support。

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring5</artifactId>
    <version>2.0.54</version>
</dependency>
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring6</artifactId>
    <version>2.0.54</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.54'
}
dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.54'
}

2. 简�?�使用

在fastjson v2中,package和1.x�?一样,是com.alibaba.fastjson2。如果你之�?用的是fastjson1,大多数情况直接更包�??就�?��?�。

2.1 将JSON解�?为JSONObject

Java:

String text = "...";
JSONObject data = JSON.parseObject(text);

byte[] bytes = ...;
JSONObject data = JSON.parseObject(bytes);

Kotlin:

import com.alibaba.fastjson2.*

val text = ... // String
val data = text.parseObject()

val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObject

2.2 将JSON解�?为JSONArray

Java:

String text = "...";
JSONArray data = JSON.parseArray(text);

Kotlin:

import com.alibaba.fastjson2.*

val text = ... // String
val data = text.parseArray() // JSONArray

2.3 将JSON解�?为Java对象

Java:

String text = "...";
User data = JSON.parseObject(text, User.class);

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:

Object data = "...";
String text = JSON.toJSONString(data);
byte[] text = JSON.toJSONBytes(data);

Kotlin:

import com.alibaba.fastjson2.*

val data = ... // Any
val text = data.toJSONString() // String
val bytes = data.toJSONByteArray() // ByteArray

2.5 使用JSONObject�?JSONArray

2.5.1 获�?�简�?�属性

String text = "{\"id\": 2,\"name\": \"fastjson2\"}";
JSONObject obj = JSON.parseObject(text);

int id = obj.getIntValue("id");
String name = obj.getString("name");
String text = "[2, \"fastjson2\"]";
JSONArray array = JSON.parseArray(text);

int id = array.getIntValue(0);
String name = array.getString(1);

2.5.2 读�?�JavaBean

Java:

JSONArray array = ...
JSONObject obj = ...

User user = array.getObject(0, User.class);
User user = obj.getObject("key", User.class);

Kotlin:

val array = ... // JSONArray
val obj = ... // JSONObject

val user = array.to<User>(0)
val user = obj.to<User>("key")

2.5.3 转为JavaBean

Java:

JSONArray array = ...
JSONObject obj = ...

User user = obj.toJavaObject(User.class);
List<User> users = array.toJavaList(User.class);

Kotlin:

val array = ... // JSONArray
val obj = ... // JSONObject

val user = obj.to<User>() // User
val users = array.toList<User>() // List<User>

2.6 将JavaBean对象�?列化为JSON

Java:

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);

Kotlin:

class User(
    var id: Int,
    var name: String
)

val user = User()
user.id = 2
user.name = "FastJson2"

val text = user.toJSONString() // String
val bytes = user.toJSONByteArray() // ByteArray

�?列化结果:

{
    "id"   : 2,
    "name" : "FastJson2"
}

3. 进阶使用

3.1 使用JSONB

3.1.1 将JavaBean对象�?列化JSONB

User user = ...;
byte[] bytes = JSONB.toBytes(user);
byte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);

3.1.2 将JSONB数�?�解�?为JavaBean

byte[] bytes = ...
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);

3.2 使用JSONPath

3.2.1 使用JSONPath读�?�部分数�?�

String text = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起�?��?�?使用能�??�?�性能

JSONReader parser = JSONReader.of(text);
Object result = path.extract(parser);

3.2.2 使用JSONPath读�?�部分byte[]的数�?�

byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起�?��?�?使用能�??�?�性能

JSONReader parser = JSONReader.of(bytes);
Object result = path.extract(parser);

3.2.3 使用JSONPath读�?�部分byte[]的数�?�

byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓存起�?��?�?使用能�??�?�性能

JSONReader parser = JSONReader.ofJSONB(bytes); // 注�?这里使用ofJSONB方法
Object result = path.extract(parser);

Star History

Star History Chart