FASTJSON 2 is a high-performance and easy-to-use Java JSON processing library with extreme performance that far exceeds other popular JSON libraries.
npx @tessl/cli install tessl/maven-com-alibaba-fastjson2--fastjson2@2.0.0FASTJSON 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.
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.57</version>
</dependency>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;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();FASTJSON 2 is built around several key components:
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);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);
}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
@JSONCompiledHigh-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); // FiltersExtensible 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();
}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);
}