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
  • JSONB format document
  • FASTJSON 2 performance has been greatly improved, see here for specific performance data

2. Prepare for use

in FastJSON 2.0, groupId is different from 1.x in that it is
 <version> 2.0.1version>


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.


2.2 Common classes and methods

In FastJSON 2.0, package is different from 1.x, which is If you used fastjson1 before, in most cases you can simply change the package name.


class JSON {

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

parse the string into

static JSONArray parseArray(String str);    

parse the string into a Java

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

Output Java objects as strings

static String toJSONString(Object object);    

Output Java objects as UT8 encoded

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

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


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


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(); = 1001; = "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);



Buy Me A Coffee