Language: 中文 | English
FASTJSONv2
is an upgrade of the FASTJSON
, with the goal of providing a highly optimized JSON
library for the next ten years.
- Supports the JSON and JSONB Protocols.
- Supports full parsing and partial parsing.
- Supports Java servers and Android Clients, and has big data applications.
- Supports Kotlin
Related Documents:
JSONB
format documentation:
https://github.com/alibaba/fastjson2/wiki/jsonb_format_cnFASTJSON v2
's performance has been significantly improved. For the benchmark, see here:
https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
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.3</version>
</dependency>
Gradle:
dependencies {
implementation 'com.alibaba.fastjson2:fastjson2:2.0.3'
}
Find the latest version of FASTJSONv2
at maven.org.
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.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.3</version>
</dependency>
Gradle:
dependencies {
implementation 'com.alibaba:fastjson:2.0.3'
}
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.3</version>
</dependency>
Kotlin Gradle:
dependencies {
implementation("com.alibaba.fastjson2:fastjson2-kotlin:2.0.3")
}
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.
Java:
String text = "...";
JSONObject data = JSONObject.parseObject(text);
byte[] bytes = ...;
JSONObject data = JSONObject.parseObject(bytes);
Kotlin:
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.parseObject()
val bytes = ... // ByteArray
val data = bytes.parseObject() // JSONObject
Java:
String text = "...";
JSONArray data = JSONObject.parseArray(text);
Kotlin:
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.parseArray() // JSONArray
Java:
String text = "...";
User data = JSONObject.parseObject(text, User.class);
Kotlin:
import com.alibaba.fastjson2.*
val text = ... // String
val data = text.to<User>() // User
val data = text.parseObject<User>() // User
Java:
Object data = "...";
String text = JSONObject.toJSONString(data);
byte[] text = JSONObject.toJSONBytes(data);
Kotlin:
import com.alibaba.fastjson2.*
val data = ... // Any
val text = text.toJSONString() // String
val bytes = text.toJSONByteArray() // ByteArray
package com.alibaba.fastjson2;
class JSONB {
// Parse a JSONB byte array into a Java Object
static T parseObject(byte[] jsonbBytes, Class<T> objectClass);
// Convert a Java Object into a JSONB Byte Array
static byte[] toBytes(Object object);
}
class JSONObject {
Object get(String key);
int getIntValue(String key);
Integer getInteger(String key);
long getLongValue(String key);
Long getLong(String key);
T getObject(String key, Class<T> objectClass);
// Convert JSONObject into a Java Object
T toJavaObject(Class<T> objectClass);
}
class JSONArray {
Object get(int index);
int getIntValue(int index);
Integer getInteger(int index);
long getLongValue(int index);
Long getLong(int index);
T getObject(int index, Class<T> objectClass);
}
class JSONPath {
// Construct a JSONPath
static JSONPath of(String path);
// The input is directly parsed according to the path,
// which will be parsed and optimized but not fully parsed.
Object extract(JSONReader jsonReader);
// Evaluate object based on the path
Object eval(Object rootObject);
}
class JSONReader {
// Constructs a JSONReader given a JSON String
static JSONReader of(String str);
// Constructs a JSONReader given a UTF-8 encoded byte array
static JSONReader of(byte[] utf8Bytes);
// Construct a JSONReader given a char array
static JSONReader of(char[] chars);
// Construct a JSONReader given a JSONB-formatted byte array
static JSONReader ofJSONB(byte[] jsonbBytes);
}
String str = "{\"id\":123}";
JSONObject jsonObject = JSON.parseObject(str);
int id = jsonObject.getIntValue("id");
String str = "[\"id\", 123]";
JSONArray jsonArray = JSON.parseArray(str);
String name = jsonArray.getString(0);
int id = jsonArray.getIntValue(1);
class Product {
public int id;
public String name;
}
Product product = new Product();
product.id = 1001;
product.name = "DataWorks";
JSON.toJSONString(product);
// Produces the following result
{
"id" : 1001,
"name" : "DataWorks"
}
JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// Produces the following result
[1001, "DataWorks"]
Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product);
Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product);
byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);
byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8);
Product product = JSON.parseObject(utf8Bytes, Product.class);
byte[] jsonbBytes = ...
Product product = JSONB.parseObject(jsonbBytes, Product.class);
Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);
String str = ...;
// Caching and Reusing can improve performance
JSONPath path = JSONPath.of("$.id");
JSONReader parser = JSONReader.of(str);
Object result = path.extract(parser);
byte[] utf8Bytes = ...;
// Caching and Reusing can improve perforamance
JSONPath path = JSONPath.of("$.id");
JSONReader parser = JSONReader.of(utf8Bytes);
Object result = path.extract(parser);
byte[] jsonbBytes = ...;
// Caching and Reusing can improve performance
JSONPath path = JSONPath.of("$.id");
// Note that this is using the ofJSONB method
JSONReader parser = JSONReader.ofJSONB(jsonbBytes);
Object result = path.extract(parser);