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.

  • FASJTONS2 code
  • https://github.com/alibaba/fastjson2/releases/tag/2.0.1 JSONB format document https://github.com/alibaba/fastjson2/wiki/jsonb_format_cn
  • FASTJSON 2 performance has been greatly improved, see here for specific performance data https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark

2. Prepare for use

in FastJSON 2.0, groupId is different from 1.x in that it is com.alibaba.fastjson2

 com.alibaba.fastjson2
groupId>
 <artifactId>fastjson2artifactId>
 <version> 2.0.1version>
dependency>

https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/

2.2

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.

<dependency>
 <groupId>com.alibabagroupId>
  <artifactId>fastjsonartifactId>
 <version>2.0.1version>
dependency>

2.2 Common classes and methods

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.

package com.alibaba.fastjson2; 

class JSON {

parses the string into JSONObject
static JSONObject
parseObject( String str);    

parse the string into

JSONArray
static JSONArray parseArray(String str);    

parse the string into a Java

object
static T parseObject(byte[] utf8Bytes, Class objectClass);

Output Java objects as strings


static String toJSONString(Object object);    

Output Java objects as UT8 encoded

byte[]
static byte[] toJSONBytes(Object object); }

class JSONB {


parses jsonb-formatted byte[] into Java objects
static T parseObject( byte[] jsonbBytes, Class objectClass);    Output Java objects to byte[] static

byte[] toBytes


(Object object) in jsonb format; }

class JSONObject {


    Object get(String key);
    int getIntValue(String key);
    Integer getInteger(String key);
    long getLongValue(String key);
    Long getLong(String key);
    getObject(String key, Class objectClass);    

Convert a JSONObject object to a Java

object
T toJavaObject(Class objectClass); }

class JSONArray {


    Object get(int index);
    int getIntValue(int index);
    Integer getInteger(int index);
    long getLongValue(int index);
    Long getLong(int index);
    getObject(int index, Class objectClass); }class JSONPath { constructJSONPath



static JSONPath of(String path) ;

Directly parse the input according to path, and will partially parse optimization, not all


of Object extract(JSONReader jsonReader);    Object eval(Object

rootObject) is evaluated according to path


; }class JSONReader { constructs JSONReader static

JSONReader



of( String str);    Construct JSONReader static

JSONReader


of(byte[] utf8Bytes) based on ut8 encoding byte array input;    Construct JSONReader static

JSONReader


of(char[] chars) based on char[] input;    Constructs JSONReader static

JSONReader


ofJSONB(byte[] jsonbBytes)}
based on json-formatted byte array input

3. Read the 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);
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


[123, "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. classJSONReader. Feature. SupportBeanArrayMapping);


String str = ...; 

JSONPath path = JSONPath.of("$.id");  Caching for reuse improves performance

JSONReader parser = JSONReader.of(str);Object result = path.extract(parser);
byte[] utf8Bytes = ...; 

JSONPath path = JSONPath.of("$.id");  Caching for reuse improves performance

JSONReader parser = JSONReader.of(utf8Bytes);Object result = path.extract(parser);
byte[] jsonbBytes = ...; 

JSONPath path = JSONPath.of("$.id");  Caching for reuse improves performance

JSONReader parser = JSONReader.ofJSONB(jsonbBytes);  Note that this is using the ofJSONB method

Object result = path.extract(parser);

Source: https://github.com/alibaba/fastjson2/releases

——

Buy Me A Coffee