Master FastJSON 2.0: Maven Setup, Core APIs, and Advanced JSONPath Techniques

This guide introduces FastJSON 2.0—a high‑performance JSON/JSONB library for Java—covers Maven configuration, key classes and methods, how to serialize and deserialize Java objects, and demonstrates powerful JSONPath queries for partial data extraction.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master FastJSON 2.0: Maven Setup, Core APIs, and Advanced JSONPath Techniques

FastJSON 2.0 Overview

FastJSON 2.0 is a major upgrade of the FastJSON project, delivering a high‑performance JSON library for the next decade. It supports both JSON and JSONB protocols with a unified API, treats JSONPath as a first‑class citizen, and runs on Java servers, Android clients, and big‑data scenarios.

1. Preparation

1.1 Maven Dependency

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

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

1.2 Compatibility with FastJSON 1.x

If you are upgrading from 1.2.x, you can use the compatibility artifact (groupId com.alibaba, artifactId fastjson, version 2.0.1). Compatibility is not 100 % guaranteed; thorough testing is required.

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>2.0.1</version>
</dependency>

2. Core Classes and Methods

All classes are now under the package com.alibaba.fastjson2. The most frequently used classes are shown below.

package com.alibaba.fastjson2;

class JSON {
    // Parse a JSON string into a JSONObject
    static JSONObject parseObject(String str);
    // Parse a JSON string into a JSONArray
    static JSONArray parseArray(String str);
    // Parse UTF‑8 bytes into a Java object of the given type
    static <T> T parseObject(byte[] utf8Bytes, Class<T> objectClass);
    // Serialize a Java object to a JSON string
    static String toJSONString(Object object);
    // Serialize a Java object to UTF‑8 bytes
    static byte[] toJSONBytes(Object object);
}

class JSONB {
    // Parse JSONB bytes into a Java object
    static <T> T parseObject(byte[] jsonbBytes, Class<T> objectClass);
    // Serialize a Java object to JSONB bytes
    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> T getObject(String key, Class<T> objectClass);
    <T> 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> T getObject(int index, Class<T> objectClass);
}

class JSONPath {
    // Create a JSONPath instance
    static JSONPath of(String path);
    // Partial parsing using a JSONReader
    Object extract(JSONReader jsonReader);
    // Evaluate a path against a Java object
    Object eval(Object rootObject);
}

class JSONReader {
    static JSONReader of(String str);
    static JSONReader of(byte[] utf8Bytes);
    static JSONReader of(char[] chars);
    static JSONReader ofJSONB(byte[] jsonbBytes);
}

3. Reading JSON Objects

String str = "{\"id\":123}";
JSONObject jsonObject = JSON.parseObject(str);
int id = jsonObject.getIntValue("id");

String arrStr = "[\"id\",123]";
JSONArray jsonArray = JSON.parseArray(arrStr);
String name = jsonArray.getString(0);
int id2 = jsonArray.getIntValue(1);

4. Serializing Java Beans

4.1 To JSON String

class Product {
    public int id;
    public String name;
}

Product product = new Product();
product.id = 1001;
product.name = "DataWorks";
String json = JSON.toJSONString(product);
// Result: {"id":1001,"name":"DataWorks"}

String jsonArray = JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
// Result: [1001,"DataWorks"]

4.2 To UTF‑8 Bytes

byte[] utf8JSONBytes = JSON.toJSONBytes(product);

4.3 To JSONB Bytes

byte[] jsonbBytes = JSONB.toBytes(product);
byte[] jsonbArrayBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);

5. Deserializing Java Beans

5.1 From JSON String

String str = "{\"id\":123}";
Product product = JSON.parseObject(str, Product.class);

5.2 From UTF‑8 Bytes

byte[] utf8Bytes = "{\"id\":123}".getBytes(StandardCharsets.UTF_8);
Product product = JSON.parseObject(utf8Bytes, Product.class);

5.3 From JSONB Bytes

byte[] jsonbBytes = ...;
Product product = JSONB.parseObject(jsonbBytes, Product.class);
// With bean‑array mapping support
Product product2 = JSONB.parseObject(jsonbBytes, Product.class, JSONReader.Feature.SupportBeanArrayMapping);

6. Using JSONPath

6.1 Extract from a JSON string

String str = ...;
JSONPath path = JSONPath.of("$.id"); // cache for reuse
JSONReader parser = JSONReader.of(str);
Object result = path.extract(parser);

6.2 Extract from UTF‑8 bytes

byte[] utf8Bytes = ...;
JSONPath path = JSONPath.of("$.id");
JSONReader parser = JSONReader.of(utf8Bytes);
Object result = path.extract(parser);

6.3 Extract from JSONB bytes

byte[] jsonbBytes = ...;
JSONPath path = JSONPath.of("$.id");
JSONReader parser = JSONReader.ofJSONB(jsonbBytes); // note the ofJSONB method
Object result = path.extract(parser);
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavamavenJsonPathDeserialization
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.