FASTJSON 2.0 is an important upgrade of the FASTJSON project. The goal is to provide a high-performance JSON library for the next decade. The same API supports both JSON/JSONB protocols. JSONPath is a first-class citizen, supporting full parsing and partial parsing, and supporting Java Server, client Android, big data scenarios.
1 Introduction
- FASJTONS2 code https://github.com/alibaba/fastjson2/releases/tag/2.0.1
- JSONB format documentation https://github.com/alibaba/fastjson2/wiki/jsonb_format_cn
- The performance of FASTJSON 2 has been greatly improved. For specific performance data, see here https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark
2. Preparation before use
2.1 Maven dependencies
In fastjson 2.0, groupId is different from 1.x, it is com.alibaba.fastjson2
<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.1</version> </dependency>
https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/
2.2
If the fastjson 1.2.x version is used originally, the compatibility package can be used. The compatibility package cannot be guaranteed to be 100% compatible. Please test and verify it carefully. If you find any problems, please feedback in time.
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.1</version> </dependency>
2.2 Common classes and methods
In fastjson 2.0, the package is different from 1.x, it is com.alibaba.fastjson2. If you used fastjson1 before, you can just change the package name in most cases.
package com.alibaba.fastjson2; class JSON { // Parse string into JSONObject static JSONObject parseObject ( String str ); // Parse string into JSONArray static JSONArray parseArray ( String str ); // Parse string into Java object static T parseObject ( byte [] utf8Bytes , Class < T > objectClass ); // Output the Java object as a string static String toJSONString ( Object object ); // Output the Java object as UT8 encoded byte[] static byte [] toJSONBytes ( Object object ); } class JSONB { // Parse byte[] in jsonb format into Java object static T parseObject ( byte [] jsonbBytes , Class < T > objectClass ); // Output the Java object as byte[] in jsonb format 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 to 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 { // 构造JSONPath static JSONPath of(String path); // Directly parse the input according to the path, which will partially parse and optimize, but will not fully parse the Object extract ( JSONReader jsonReader ); // Evaluate the object according to path Object eval ( Object rootObject ); } class JSONReader { // Construct a JSONReader based on String input static JSONReader of ( String str ); // Construct a JSONReader based on ut8 encoded byte array input static JSONReader of ( byte [] utf8Bytes ); // Construct a JSONReader based on char[] input static JSONReader of ( char [] chars ); // Construct JSONReader based on json format byte array input static JSONReader ofJSONB ( byte [] jsonbBytes ) }
3. Read JSON object
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);
4. Generate JSON from JavaBean object
4.1 Generate a JSON-formatted string from a JavaBean object
class Product { public int id; public String name; } Product product = new Product(); product.id = 1001; product.name = "DataWorks"; JSON.toJSONString(product); // generate the following result { "id" : 1001, "name" : "DataWorks" } JSON . toJSONString ( product , JSONWriter . Feature . BeanToArray ); // produces the following result [ 123 , "DataWorks" ]
4.2 Generate UTF8 encoded byte[] from JavaBean object
Product product = ...; byte[] utf8JSONBytes = JSON.toJSONBytes(product);
4.3 Generate a JavaBean object into a byte[] in JSONB format
Product product = ...; byte[] jsonbBytes = JSONB.toBytes(product); byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
5. Read JavaBeans
5.1 Read string into JavaBean
String str = "{\"id\":123}"; Product product = JSON.parseObject(str, Product.class);
5.2 Read UTF8 encoded byte[] into JavaBean
byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8); Product product = JSON.parseObject(utf8Bytes, Product.class);
5.3 Read JSONB data into JavaBean
byte[] jsonbBytes = ... Product product = JSONB.parseObject(jsonbBytes, Product.class); Product product = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);
6. Using JSONPath
6.1 Partially read data using JSONPath
String str = ...; JSONPath path = JSONPath . of ( "$.id" ); // Cached and reused can improve performance JSONReader parser = JSONReader.of(str); Object result = path.extract(parser);
6.2 Use JSONPath to read some utf8Bytes data
byte[] utf8Bytes = ...; JSONPath path = JSONPath . of ( "$.id" ); // Cached and reused can improve performance JSONReader parser = JSONReader.of(utf8Bytes); Object result = path.extract(parser);
6.3 Use JSONPath to read some jsonbBytes data
byte[] jsonbBytes = ...; JSONPath path = JSONPath . of ( "$.id" ); // Cached and reused can improve performance JSONReader parser = JSONReader . ofJSONB ( jsonbBytes ); // Note, this is using the ofJSONB method Object result = path . extract ( parser );
For details, please check: https://github.com/alibaba/fastjson2/releases