📖 English Documentation | 📖 䏿–‡æ–‡æ¡£
本项目的Issues会被�?ï¿½æ¥æ²‰æ·€è‡³é˜¿é‡Œäº‘开�?�者社区
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 - 支�?
Kotlinhttps://alibaba.github.io/fastjson2/kotlin_cn - 支�?
JSON Schemahttps://alibaba.github.io/fastjson2/json_schema_cn - æ–°å¢žåŠ æ”¯ï¿½?äºŒè¿›åˆ¶æ ¼ï¿½?JSONB https://alibaba.github.io/fastjson2/jsonb_format_cn
在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 查看最新�?�用的版本。
如果原�?�使用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'
}如果项目使用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")
}如果项目使用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'
}在fastjson v2ä¸ï¼Œpackageå’Œ1.x�?ä¸€æ ·ï¼Œæ˜¯com.alibaba.fastjson2ã€‚å¦‚æžœä½ ä¹‹ï¿½?用的是fastjson1,大多数情况直接更包�??就�?��?�。
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() // JSONObjectJava:
String text = "...";
JSONArray data = JSON.parseArray(text);Kotlin:
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.parseArray() // JSONArrayJava:
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>() // UserJava:
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() // ByteArrayString 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);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")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>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"
}User user = ...;
byte[] bytes = JSONB.toBytes(user);
byte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);byte[] bytes = ...
User user = JSONB.parseObject(bytes, User.class);
User user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);String text = ...;
JSONPath path = JSONPath.of("$.id"); // 缓å˜èµ·ï¿½?��?�?使用能�??�?�性能
JSONReader parser = JSONReader.of(text);
Object result = path.extract(parser);byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓å˜èµ·ï¿½?��?�?使用能�??�?�性能
JSONReader parser = JSONReader.of(bytes);
Object result = path.extract(parser);byte[] bytes = ...;
JSONPath path = JSONPath.of("$.id"); // 缓å˜èµ·ï¿½?��?�?使用能�??�?�性能
JSONReader parser = JSONReader.ofJSONB(bytes); // 注�?这里使用ofJSONB方法
Object result = path.extract(parser);
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

