FASTJSON 2 is a high-performance and easy-to-use Java JSON processing library with extreme performance that far exceeds other popular JSON libraries.
—
Primary JSON parsing and serialization operations using the static JSON interface. Essential for all JSON processing tasks with comprehensive method overloads for different input types and configuration options.
Parse JSON strings, byte arrays, character arrays, and streams into Java objects with optional feature configuration.
/**
* Parse JSON text to Object
* @param text JSON string to parse
* @return Parsed object (JSONObject, JSONArray, or primitive)
*/
public static Object parse(String text);
/**
* Parse JSON text with specific features
* @param text JSON string to parse
* @param features Reader features to apply
* @return Parsed object
*/
public static Object parse(String text, JSONReader.Feature... features);
/**
* Parse from byte array
* @param bytes JSON bytes to parse
* @return Parsed object
*/
public static Object parse(byte[] bytes);
/**
* Parse from character array
* @param chars JSON characters to parse
* @return Parsed object
*/
public static Object parse(char[] chars);
/**
* Parse from InputStream
* @param inputStream JSON input stream
* @return Parsed object
*/
public static Object parse(InputStream inputStream);Parse JSON specifically to JSONObject instances with type-safe conversion to custom Java classes.
/**
* Parse JSON string to JSONObject
* @param text JSON string representing an object
* @return JSONObject instance
*/
public static JSONObject parseObject(String text);
/**
* Parse JSON string to specific Java class
* @param text JSON string to parse
* @param clazz Target class type
* @return Instance of specified class
*/
public static <T> T parseObject(String text, Class<T> clazz);
/**
* Parse JSON to generic type using TypeReference
* @param text JSON string to parse
* @param typeReference Type reference for generic types
* @return Instance of specified generic type
*/
public static <T> T parseObject(String text, TypeReference<T> typeReference);
/**
* Parse JSON to Type with features
* @param text JSON string to parse
* @param type Target type
* @return Instance of specified type
*/
public static <T> T parseObject(String text, Type type);
/**
* Parse from Reader
* @param reader JSON character reader
* @param clazz Target class type
* @return Instance of specified class
*/
public static <T> T parseObject(Reader reader, Class<T> clazz);
/**
* Parse from URL
* @param url JSON resource URL
* @param clazz Target class type
* @return Instance of specified class
*/
public static <T> T parseObject(URL url, Class<T> clazz);
/**
* Parse JSON string to Java class with features
* @param text JSON string to parse
* @param clazz Target class type
* @param features Reader features to apply
* @return Instance of specified class
*/
public static <T> T parseObject(String text, Class<T> clazz, JSONReader.Feature... features);
/**
* Parse byte array to specific Java class
* @param bytes JSON bytes to parse
* @param clazz Target class type
* @return Instance of specified class
*/
public static <T> T parseObject(byte[] bytes, Class<T> clazz);Parse JSON arrays to JSONArray instances or strongly-typed Java collections.
/**
* Parse JSON string to JSONArray
* @param text JSON string representing an array
* @return JSONArray instance
*/
public static JSONArray parseArray(String text);
/**
* Parse JSON array to List of specific type
* @param text JSON string representing an array
* @param clazz Element class type
* @return List of specified element type
*/
public static <T> List<T> parseArray(String text, Class<T> clazz);
/**
* Parse JSON array with features
* @param text JSON string representing an array
* @param clazz Element class type
* @param features Reader features to apply
* @return List of specified element type
*/
public static <T> List<T> parseArray(String text, Class<T> clazz, JSONReader.Feature... features);Convert Java objects to JSON strings or byte arrays with optional formatting and feature configuration.
/**
* Serialize object to JSON string
* @param object Object to serialize
* @return JSON string representation
*/
public static String toJSONString(Object object);
/**
* Serialize object to JSON string with features
* @param object Object to serialize
* @param features Writer features to apply
* @return JSON string representation
*/
public static String toJSONString(Object object, JSONWriter.Feature... features);
/**
* Serialize object to JSON byte array
* @param object Object to serialize
* @return JSON bytes
*/
public static byte[] toJSONBytes(Object object);
/**
* Serialize object to JSON byte array with charset
* @param object Object to serialize
* @param charset Character encoding
* @return JSON bytes in specified encoding
*/
public static byte[] toJSONBytes(Object object, Charset charset);
/**
* Write object directly to OutputStream
* @param outputStream Target output stream
* @param object Object to serialize
*/
public static void writeTo(OutputStream outputStream, Object object);Validate JSON strings for syntax correctness without full parsing.
/**
* Check if text is valid JSON
* @param text String to validate
* @return true if valid JSON
*/
public static boolean isValid(String text);
/**
* Check if text is valid JSON object
* @param text String to validate
* @return true if valid JSON object
*/
public static boolean isValidObject(String text);
/**
* Check if text is valid JSON array
* @param text String to validate
* @return true if valid JSON array
*/
public static boolean isValidArray(String text);Comprehensive JSON validation with detailed error reporting and type detection.
/**
* JSON validator for advanced validation scenarios
*/
public class JSONValidator {
/**
* Create validator from JSON string
* @param json JSON string to validate
* @return JSONValidator instance
*/
public static JSONValidator from(String json);
/**
* Create validator from byte array
* @param bytes JSON bytes to validate
* @return JSONValidator instance
*/
public static JSONValidator from(byte[] bytes);
/**
* Validate JSON structure
* @return true if JSON is valid
*/
public boolean validate();
/**
* Get detected JSON type
* @return Type of the JSON content
*/
public Type getType();
}Global configuration methods for setting default features and registering custom readers/writers.
/**
* Configure default reader features
* @param features Features to enable by default
*/
public static void config(JSONReader.Feature... features);
/**
* Configure default writer features
* @param features Features to enable by default
*/
public static void config(JSONWriter.Feature... features);
/**
* Register custom ObjectReader for specific type
* @param type Target type
* @param objectReader Custom reader implementation
*/
public static void register(Type type, ObjectReader<?> objectReader);
/**
* Register custom ObjectWriter for specific type
* @param type Target type
* @param objectWriter Custom writer implementation
*/
public static void register(Type type, ObjectWriter<?> objectWriter);
/**
* Configure mixin classes for additional annotations
* @param target Target class
* @param mixinSource Mixin class with annotations
*/
public static void mixIn(Class<?> target, Class<?> mixinSource);
/**
* Copy object with type conversion
* @param object Source object
* @param targetClass Target class type
* @return Converted object instance
*/
public static <T> T copy(Object object, Class<T> targetClass);
/**
* Copy object to existing target
* @param object Source object
* @param target Target object to populate
*/
public static void copyTo(Object object, Object target);
/**
* Parse with offset and length from string
* @param text JSON string
* @param offset Starting offset
* @param length Length to parse
* @return Parsed object
*/
public static Object parse(String text, int offset, int length);
/**
* Parse array with features and filters
* @param text JSON string
* @param clazz Element class type
* @param filter Property filter
* @param features Reader features
* @return List of specified element type
*/
public static <T> List<T> parseArray(String text, Class<T> clazz,
PropertyFilter filter, JSONReader.Feature... features);Usage Examples:
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
// Basic parsing
String json = "{\"name\":\"John\",\"age\":30}";
Object obj = JSON.parse(json);
JSONObject jsonObj = JSON.parseObject(json);
User user = JSON.parseObject(json, User.class);
// Array parsing
String arrayJson = "[{\"name\":\"John\"},{\"name\":\"Jane\"}]";
JSONArray array = JSON.parseArray(arrayJson);
List<User> users = JSON.parseArray(arrayJson, User.class);
// Generic type parsing with TypeReference
String mapJson = "{\"users\":{\"john\":{\"name\":\"John\",\"age\":30}}}";
Map<String, User> userMap = JSON.parseObject(mapJson, new TypeReference<Map<String, User>>(){});
List<String> stringList = JSON.parseObject("[\"a\",\"b\",\"c\"]", new TypeReference<List<String>>(){});
// Complex generic types
String complexJson = "{\"data\":[{\"id\":1,\"tags\":[\"tag1\",\"tag2\"]}]}";
Map<String, List<DataItem>> complexData = JSON.parseObject(complexJson,
new TypeReference<Map<String, List<DataItem>>>(){});
// Serialization
User user = new User("John", 30);
String jsonString = JSON.toJSONString(user);
String prettyJson = JSON.toJSONString(user, JSONWriter.Feature.PrettyFormat);
// Validation
boolean isValid = JSON.isValid("{\"valid\":true}");
boolean isValidObj = JSON.isValidObject("{\"key\":\"value\"}");
// Advanced validation with type detection
JSONValidator validator = JSONValidator.from("{\"name\":\"John\",\"age\":30}");
boolean valid = validator.validate();
Type detectedType = validator.getType(); // Object type detected
// Configuration
JSON.config(JSONReader.Feature.SupportArrayToBean);
JSON.config(JSONWriter.Feature.WriteNulls, JSONWriter.Feature.PrettyFormat);Install with Tessl CLI
npx tessl i tessl/maven-com-alibaba-fastjson2--fastjson2