Skip to content
forked from alibaba/fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.

License

Notifications You must be signed in to change notification settings

cnscoo/fastjson2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 logo

1. 使用准备

1.1 添加依赖

fastjson v2中,groupId1.x不一样,是com.alibaba.fastjson2

Maven:

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

Gradle:

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

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

1.2 其他模块

Fastjson v1兼容模块

如果原来使用fastjson 1.2.x版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。

Maven:

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

Gradle:

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

Fastjson Kotlin集成模块

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

  • Maven:
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-kotlin</artifactId>
    <version>2.0.53</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.53")
}
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.53</version>
</dependency>
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring6</artifactId>
    <version>2.0.53</version>
</dependency>

Gradle:

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

2. 简单使用

fastjson v2中,package1.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 = text.toJSONString() // String
val bytes = text.toJSONByteArray() // ByteArray

2.5 使用JSONObjectJSONArray

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

About

🚄 FASTJSON2 is a Java JSON library with excellent performance.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 99.6%
  • Other 0.4%