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.
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
:
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.2</version>
</dependency>
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.2</version>
</dependency>
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.
package com.alibaba.fastjson2;
class JSON {
// Parse String into JSONObject
static JSONObject parseObject(String str);
// Parse a String into JSONArray
static JSONArray parseArray(String str);
// Parse a String into a Java Object
static T parseObject(byte[] utf8Bytes, Class<T> objectClass);
// Output a Java Object into a string
static String toJSONString(Object object);
// Output a Java Object into a JSON Byte Array
static byte[] toJSONBytes(Object object);
}
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);