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 set of APIs support JSON/JSONB two protocols, JSONPath is a first-class citizen, supports full parsing and partial parsing, supports Java server, client Android, big data scenarios. in FastJSON 2.0, groupId is different from 1.x in that it is com.alibaba.fastjson2 https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/ If you originally use the fastjson 1.2.x version, you can use the compatibility package, the compatibility package cannot guarantee 100% compatibility, please test and verify carefully, If you find a problem, please give feedback in time. In FastJSON 2.0, package is different from 1.x, which is com.alibaba.fastjson2. If you used fastjson1 before, in most cases you can simply change the package name. class JSON { parse the string into parse the string into a Java Output Java objects as strings Output Java objects as UT8 encoded class JSONB { byte[] toBytes class JSONObject { Convert a JSONObject object to a Java class JSONArray { Directly parse the input according to path, and will partially parse optimization, not all rootObject) is evaluated according to path JSONReader JSONReader JSONReader JSONReader int id = jsonObject.getIntValue("id"); String name = jsonArray.getString(0); Product product = new Product(); Produces the following result id" : 1001 Produces the following result byte[] jsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray); Product product = JSONB.parseObject(jsonbBytes, Product. class, JSONReader. Feature. SupportBeanArrayMapping); JSONPath path = JSONPath.of("$.id"); Caching for reuse improves performance JSONPath path = JSONPath.of("$.id"); Caching for reuse improves performance JSONPath path = JSONPath.of("$.id"); Caching for reuse improves performance JSONReader parser = JSONReader.ofJSONB(jsonbBytes); Note that this is using the ofJSONB method
2. Prepare for use
groupId>
<artifactId>fastjson2artifactId>
<version> 2.0.1version>
dependency>2.2
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.1version>
dependency>2.2 Common classes and methods
package com.alibaba.fastjson2;
static JSONObject
parseObject( String str);
static JSONArray parseArray(String str);
static T parseObject(byte[] utf8Bytes, Class objectClass
static String toJSONString(Object object);
static byte[] toJSONBytes(Object object); }
parses jsonb-formatted byte[] into Java objects
static T parseObject( byte[] jsonbBytes, Class
(Object object) in jsonb format; }
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 toJavaObject(Class
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
static JSONPath of(String path) ;
of Object extract(JSONReader jsonReader); Object eval(Object
; }class JSONReader { constructs JSONReader static
of( String str); Construct JSONReader static
of(byte[] utf8Bytes) based on ut8 encoding byte array input; Construct JSONReader static
of(char[] chars) based on char[] input; Constructs JSONReader static
3. Read the JSON object
String str = "{\"id\":123}"; JSONObject jsonObject = JSON.parseObject(str);
String str = "[\"id\", 123]"; JSONArray jsonArray = JSON.parseArray(str);
int id = jsonArray.getIntValue(1); class Product {
public int id;
public String name; }
product.id = 1001;
product.name = "DataWorks"; JSON.toJSONString(product);
name" : "DataWorks"}JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
[123, "DataWorks"]Product product = ...;
byte[] utf8JSONBytes = JSON.toJSONBytes(product); Product product = ...;
byte[] jsonbBytes = JSONB.toBytes(product); 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); String str = ...;
byte[] utf8Bytes = ...;
byte[] jsonbBytes = ...;
ofJSONB(byte[] jsonbBytes)}
based on json-formatted byte array input