or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-features.mdannotations.mdcore-operations.mddata-structures.mdindex.mdreader-writer.md
tile.json

tessl/maven-com-alibaba-fastjson2--fastjson2

FASTJSON 2 is a high-performance and easy-to-use Java JSON processing library with extreme performance that far exceeds other popular JSON libraries.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.alibaba.fastjson2/fastjson2@2.0.x

To install, run

npx @tessl/cli install tessl/maven-com-alibaba-fastjson2--fastjson2@2.0.0

index.mddocs/

FASTJSON 2

FASTJSON 2 is a high-performance and easy-to-use Java JSON processing library with extreme performance that far exceeds other popular JSON libraries. It supports JDK 8+, includes JDK 11/17 optimizations, supports Record types, GraalVM Native-Image, Android 8+, Kotlin, JSON Schema, and introduces JSONB binary format support.

Package Information

  • Package Name: com.alibaba.fastjson2:fastjson2
  • Package Type: maven
  • Language: Java
  • Installation: Add to pom.xml:
    <dependency>
        <groupId>com.alibaba.fastjson2</groupId>
        <artifactId>fastjson2</artifactId>
        <version>2.0.57</version>
    </dependency>

Core Imports

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.TypeReference;
import com.alibaba.fastjson2.JSONValidator;

Basic Usage

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

// Parse JSON string to object
String json = "{\"name\":\"John\",\"age\":30}";
JSONObject obj = JSON.parseObject(json);
String name = obj.getString("name");
Integer age = obj.getInteger("age");

// Parse to custom Java object
User user = JSON.parseObject(json, User.class);

// Serialize object to JSON
User user = new User("John", 30);
String jsonString = JSON.toJSONString(user);

// Array handling
String arrayJson = "[{\"name\":\"John\"},{\"name\":\"Jane\"}]";
JSONArray array = JSON.parseArray(arrayJson);
List<User> users = JSON.parseArray(arrayJson, User.class);

// Generic type handling with TypeReference
String mapJson = "{\"key1\":{\"name\":\"John\"},\"key2\":{\"name\":\"Jane\"}}";
Map<String, User> userMap = JSON.parseObject(mapJson, new TypeReference<Map<String, User>>(){});

// JSON validation
boolean isValid = JSONValidator.from(json).validate();

Architecture

FASTJSON 2 is built around several key components:

  • JSON Static Interface: Primary entry point providing static methods for all JSON operations
  • JSONObject/JSONArray: Enhanced Map/List implementations with type-safe getters and path access
  • Reader/Writer Framework: Extensible deserialization/serialization system with ObjectReader/ObjectWriter interfaces
  • Annotation System: Comprehensive annotations for customizing JSON processing behavior
  • Feature Configuration: Fine-grained control through JSONReader.Feature and JSONWriter.Feature enums
  • JSONB Binary Format: High-performance binary JSON format for optimized transmission and storage
  • JSONPath Support: Advanced path-based JSON data extraction and manipulation
  • Schema Validation: JSON Schema validation capabilities for data integrity

Capabilities

Core JSON Operations

Primary JSON parsing and serialization operations using the static JSON interface. Essential for all JSON processing tasks.

public static Object parse(String text);
public static Object parse(String text, JSONReader.Feature... features);
public static JSONObject parseObject(String text);
public static <T> T parseObject(String text, Class<T> clazz);
public static JSONArray parseArray(String text);
public static <T> List<T> parseArray(String text, Class<T> clazz);
public static String toJSONString(Object object);
public static String toJSONString(Object object, JSONWriter.Feature... features);
public static byte[] toJSONBytes(Object object);

Core JSON Operations

Data Structures

Enhanced JSONObject and JSONArray classes providing type-safe access methods and advanced functionality beyond standard Map/List interfaces.

public class JSONObject extends LinkedHashMap<String, Object> {
    public String getString(String key);
    public Integer getInteger(String key);
    public JSONObject getJSONObject(String key);
    public JSONArray getJSONArray(String key);
    public Object getByPath(String jsonPath);
    public <T> T to(Class<T> clazz);
}

public class JSONArray extends ArrayList<Object> {
    public String getString(int index);
    public JSONObject getJSONObject(int index);
    public <T> T to(Class<T> clazz);
    public <T> List<T> toJavaList(Class<T> clazz);
}

Data Structures

Annotations and Configuration

Comprehensive annotation system for customizing JSON serialization and deserialization behavior at class and field levels.

@JSONField(name = "customName", format = "yyyy-MM-dd")
@JSONType(naming = PropertyNamingStrategy.SnakeCase)
@JSONCreator
@JSONBuilder
@JSONCompiled

Annotations and Configuration

Advanced Features

High-performance features including JSONB binary format, JSONPath queries, schema validation, and custom filters.

public static byte[] toBytes(Object object); // JSONB
public static Object extract(String json, String path); // JSONPath
public static boolean isValid(String text, JSONSchema schema); // Schema
public static void config(Filter... filters); // Filters

Advanced Features

Reader/Writer Framework

Extensible framework for custom serialization and deserialization logic with ObjectReader and ObjectWriter interfaces.

public interface ObjectReader<T> {
    T readObject(JSONReader jsonReader);
    Object createInstance();
}

public interface ObjectWriter<T> {
    void write(JSONWriter jsonWriter, Object object);
    boolean hasFilter();
}

Reader/Writer Framework

Type Definitions

public enum JSONReader.Feature {
    FieldBased, SupportAutoType, SupportArrayToBean, 
    UseBigDecimalForFloats, UseBigDecimalForDoubles, TrimString, 
    AllowUnQuotedFieldNames, ErrorOnNullForPrimitives, UseNativeObject,
    SupportClassForName, InitStringFieldAsEmpty, AllowComment,
    AllowSingleQuotes, AllowUnQuotedFieldNames, SupportSmartMatch
}

public enum JSONWriter.Feature {
    WriteNulls, PrettyFormat, WriteClassName, BeanToArray,
    UseSingleQuotes, ReferenceDetection, WriteEnumsUsingName,
    WriteEnumsUsingToString, NotWriteHashMapArrayListClassName,
    NotWriteRootClassName, WriteBigDecimalAsPlain, WriteNonStringKeyAsString,
    WriteByteArrayAsBase64, WriteBooleanAsNumber, OptimizedForAscii
}

public enum PropertyNamingStrategy {
    CamelCase, PascalCase, SnakeCase, UpperCase,
    UpperCamelCaseWithSpaces, UpperCamelCaseWithUnderScores,
    UpperCamelCaseWithDashes
}

public abstract class TypeReference<T> {
    protected TypeReference() {}
    public Type getType() { return null; }
}

public class JSONValidator {
    public static JSONValidator from(String json);
    public static JSONValidator from(byte[] bytes);
    public boolean validate();
    public Type getType();
}

public class JSONException extends RuntimeException {}
public class JSONValidException extends JSONException {}

public class SymbolTable {
    public static SymbolTable of(String... symbols);
    public int getId(String symbol);
    public String getName(int id);
}

public interface PathCallback {
    void callback(String path, Object value);
}

public interface PropertyFilter {
    boolean apply(Object object, String name, Object value);
}