Go to file
yanxutao89 fc5e31062e
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, macos-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, ubuntu-24.04) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, windows-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, macos-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, ubuntu-24.04) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, windows-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, macos-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, ubuntu-24.04) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, windows-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, macos-latest) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, ubuntu-24.04) (push) Has been cancelled Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, windows-latest) (push) Has been cancelled Details
fix improve feature of NullAsDefaultValue, for issue #3518
2025-04-25 10:56:09 +08:00
.github ubuntu-24.04 2025-04-18 08:57:40 +08:00
.mvn/wrapper chore: upgrade maven wrapper to `3.9.9` 🪶 2024-12-10 02:08:32 +08:00
android-test fix android support 2023-10-06 05:30:46 +08:00
benchmark Bump org.apache.fury:fury-core from 0.10.0 to 0.10.1 2025-04-19 16:27:26 +08:00
benchmark_21 Bump org.apache.fury:fury-core from 0.10.0 to 0.10.1 2025-04-19 16:27:26 +08:00
codegen 2.0.58 init 2025-03-31 15:32:20 +08:00
codegen-test 2.0.58 init 2025-03-31 15:32:20 +08:00
core fix improve feature of NullAsDefaultValue, for issue #3518 2025-04-25 10:56:09 +08:00
docs fix doc benchmark_2.0.12.md 2025-04-18 08:51:33 +08:00
example-graalvm-native Bump org.junit.platform:junit-platform-launcher from 1.12.1 to 1.12.2 2025-04-18 08:56:29 +08:00
example-solon-test 2.0.58 init 2025-03-31 15:32:20 +08:00
example-spring-test 2.0.58 init 2025-03-31 15:32:20 +08:00
example-spring6-test 2.0.58 init 2025-03-31 15:32:20 +08:00
extension Bump com.clickhouse:clickhouse-jdbc from 0.8.2 to 0.8.3 2025-04-10 10:09:54 +08:00
extension-jaxrs 2.0.58 init 2025-03-31 15:32:20 +08:00
extension-solon solon upgraded to v3.2.0 2025-04-19 12:54:38 +08:00
extension-spring5 2.0.58 init 2025-03-31 15:32:20 +08:00
extension-spring6 2.0.58 init 2025-03-31 15:32:20 +08:00
fastjson1-compatible 2.0.58 init 2025-03-31 15:32:20 +08:00
kotlin 2.0.58 init 2025-03-31 15:32:20 +08:00
safemode-test 2.0.58 init 2025-03-31 15:32:20 +08:00
scripts add script `bump_fastjson2_version`: 2022-04-25 23:52:23 +08:00
src add back `violations.xml` 2023-02-12 18:31:29 +08:00
test-jdk17 2.0.58 init 2025-03-31 15:32:20 +08:00
.editorconfig test: update testcase of Android 2023-02-15 13:27:01 +08:00
.gitattributes Improve some code details, fix some bugs (#3161) 2024-11-17 20:18:23 +08:00
.gitignore add git ignore 2025-03-07 19:46:20 +08:00
CODE_OF_CONDUCT.md upgrade contributor-covenant version 2.1. 2022-05-09 19:59:50 +08:00
CONTRIBUTING.md 英语句子语法修复 2023-11-06 09:20:49 +08:00
LICENSE Initial commit 2022-04-17 12:27:24 +08:00
README.md 2.0.57 release 2025-03-31 12:58:31 +08:00
README_EN.md 2.0.57 release 2025-03-31 12:58:31 +08:00
SECURITY.md Create SECURITY.md 2022-05-07 19:40:39 +08:00
mvnw chore: upgrade maven wrapper to `3.9.9` 🪶 2024-12-10 02:08:32 +08:00
mvnw.cmd chore: upgrade maven wrapper to `3.9.9` 🪶 2024-12-10 02:08:32 +08:00
pom.xml Bump commons-io:commons-io from 2.18.0 to 2.19.0 2025-04-19 16:27:01 +08:00

README_EN.md

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 | 📖 中文文档
The issues of fastjson will be also posted on Alibaba Cloud Developer Community

FASTJSON v2

FASTJSON v2 is an upgrade of the FASTJSON, with the goal of providing a highly optimized JSON library for the next ten years.

fastjson logo

Related Documents:

1. Prepare

1.1 Download

FASTJSONv2's groupId is different from versions 1.x, it is instead com.alibaba.fastjson2:

Maven:

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

Gradle:

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

Find the latest version of FASTJSONv2 at maven.org.

1.2 Other modules

Compatible dependence of fastjson-v1

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.

Maven:

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

Gradle:

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

Kotlin integration module fastjson-kotlin

If your project uses kotlin, you can use the Fastjson-Kotlin module, and use the characteristics of kotlin.

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-kotlin</artifactId>
    <version>2.0.57</version>
</dependency>

Add standard library(kotlin-stdlib) and reflection library(kotlin-reflect) as appropriate. If the data class is used or the parameters are passed in through constructor, then add reflection library.

<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.57")
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
    implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")
}

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.

Maven:

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension</artifactId>
    <version>2.0.57</version>
</dependency>

Gradle:

dependencies {
    implementation 'com.alibaba.fastjson2:fastjson2-extension:2.0.57'
}

2. Usage

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.

2.1 Parse JSON into 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 Parse JSON into JSONArray

Java:

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

Kotlin:

import com.alibaba.fastjson2.*

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

2.3 Parse JSON into a Java Object

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 Serialization Java Object to 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 Use JSONObject, JSONArray

2.5.1 Get simple property

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 Get 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 Convert to 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 Serialize JavaBean to 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

Serialization result:

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

3. Advanced usage

3.1 Use JSONB

3.1.1 Serialize JavaBean to JSONB

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

3.1.2 Parse JSONB to JavaBean

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

3.2 Use JSONPath

3.2.1 Use JSONPath to read partial data

String text = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse

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

3.2.2 Read part of byte[] data using JSONPath

byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse

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

3.2.3 Read part of byte[] data using JSONPath

byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // Cached for reuse

JSONReader parser = JSONReader.ofJSONB(bytes); // Use ofJSONB method
Object result = path.extract(parser);