FASTJSON 2 is a high-performance and easy-to-use Java JSON processing library with extreme performance that far exceeds other popular JSON libraries.
—
Enhanced JSONObject and JSONArray classes providing type-safe access methods and advanced functionality beyond standard Map/List interfaces. These classes offer optimized JSON-specific operations and seamless conversion capabilities.
Enhanced LinkedHashMap implementation with type-safe getters, JSONPath support, and conversion utilities.
/**
* JSON object implementation extending LinkedHashMap
*/
public class JSONObject extends LinkedHashMap<String, Object> implements InvocationHandler {
/**
* Get string value by key
* @param key Property key
* @return String value or null
*/
public String getString(String key);
/**
* Get integer value by key
* @param key Property key
* @return Integer value or null
*/
public Integer getInteger(String key);
/**
* Get long value by key
* @param key Property key
* @return Long value or null
*/
public Long getLong(String key);
/**
* Get double value by key
* @param key Property key
* @return Double value or null
*/
public Double getDouble(String key);
/**
* Get primitive double value by key
* @param key Property key
* @return double value or 0.0 if null
*/
public double getDoubleValue(String key);
/**
* Get primitive long value by key
* @param key Property key
* @return long value or 0L if null
*/
public long getLongValue(String key);
/**
* Get primitive int value by key
* @param key Property key
* @return int value or 0 if null
*/
public int getIntValue(String key);
/**
* Get primitive boolean value by key
* @param key Property key
* @return boolean value or false if null
*/
public boolean getBooleanValue(String key);
/**
* Get boolean value by key
* @param key Property key
* @return Boolean value or null
*/
public Boolean getBoolean(String key);
/**
* Get nested JSONObject by key
* @param key Property key
* @return JSONObject or null
*/
public JSONObject getJSONObject(String key);
/**
* Get JSONArray by key
* @param key Property key
* @return JSONArray or null
*/
public JSONArray getJSONArray(String key);
/**
* Get List value by key
* @param key Property key
* @return List or null
*/
public List getList(String key);
/**
* Access nested values using JSONPath expression
* @param jsonPath Path expression (e.g., "$.user.name")
* @return Value at specified path
*/
public Object getByPath(String jsonPath);
/**
* Convert to specific Java class
* @param clazz Target class type
* @return Instance of specified class
*/
public <T> T to(Class<T> clazz);
/**
* Convert to Java object (alias for to())
* @param clazz Target class type
* @return Instance of specified class
*/
public <T> T toJavaObject(Class<T> clazz);
/**
* Iterate over array objects in this JSONObject
*/
public void forEachArrayObject();
}Enhanced ArrayList implementation with type-safe getters and conversion utilities for JSON arrays.
/**
* JSON array implementation extending ArrayList
*/
public class JSONArray extends ArrayList<Object> {
/**
* Get string value by index
* @param index Array index
* @return String value or null
*/
public String getString(int index);
/**
* Get integer value by index
* @param index Array index
* @return Integer value or null
*/
public Integer getInteger(int index);
/**
* Get long value by index
* @param index Array index
* @return Long value or null
*/
public Long getLong(int index);
/**
* Get double value by index
* @param index Array index
* @return Double value or null
*/
public Double getDouble(int index);
/**
* Get primitive double value by index
* @param index Array index
* @return double value or 0.0 if null
*/
public double getDoubleValue(int index);
/**
* Get primitive long value by index
* @param index Array index
* @return long value or 0L if null
*/
public long getLongValue(int index);
/**
* Get primitive int value by index
* @param index Array index
* @return int value or 0 if null
*/
public int getIntValue(int index);
/**
* Get primitive boolean value by index
* @param index Array index
* @return boolean value or false if null
*/
public boolean getBooleanValue(int index);
/**
* Get boolean value by index
* @param index Array index
* @return Boolean value or null
*/
public Boolean getBoolean(int index);
/**
* Get nested JSONObject by index
* @param index Array index
* @return JSONObject or null
*/
public JSONObject getJSONObject(int index);
/**
* Get nested JSONArray by index
* @param index Array index
* @return JSONArray or null
*/
public JSONArray getJSONArray(int index);
/**
* Convert to specific Java class
* @param clazz Target class type
* @return Instance of specified class
*/
public <T> T to(Class<T> clazz);
/**
* Convert to Java array
* @return Object array
*/
public Object[] toArray();
/**
* Convert to typed Java List
* @param clazz Element class type
* @return List of specified element type
*/
public <T> List<T> toJavaList(Class<T> clazz);
/**
* Enhanced set method with negative indexing and auto-expansion
* @param index Array index (supports negative indexing)
* @param value Value to set
* @return Previous value at index
*/
public Object set(int index, Object value);
}Usage Examples:
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
// JSONObject usage
String json = "{\"name\":\"John\",\"age\":30,\"active\":true,\"scores\":[95,87,92]}";
JSONObject obj = JSON.parseObject(json);
// Type-safe getters
String name = obj.getString("name"); // "John"
Integer age = obj.getInteger("age"); // 30
Boolean active = obj.getBoolean("active"); // true
JSONArray scores = obj.getJSONArray("scores");
// JSONPath access
String nestedJson = "{\"user\":{\"profile\":{\"name\":\"John\"}}}";
JSONObject nested = JSON.parseObject(nestedJson);
String userName = (String) nested.getByPath("$.user.profile.name"); // "John"
// Convert to custom object
User user = obj.to(User.class);
// JSONArray usage
String arrayJson = "[{\"name\":\"John\",\"age\":30},{\"name\":\"Jane\",\"age\":25}]";
JSONArray array = JSON.parseArray(arrayJson);
// Access by index
JSONObject firstUser = array.getJSONObject(0);
String firstName = firstUser.getString("name"); // "John"
// Convert to typed list
List<User> users = array.toJavaList(User.class);
// Enhanced set with negative indexing
JSONArray numbers = JSON.parseArray("[1,2,3,4,5]");
numbers.set(-1, 10); // Sets last element to 10: [1,2,3,4,10]
// Auto-expansion
JSONArray expandable = new JSONArray();
expandable.set(5, "value"); // Automatically expands to index 5// Complex nested structure access
String complexJson = """
{
"users": [
{"name": "John", "addresses": [{"city": "NYC"}, {"city": "LA"}]},
{"name": "Jane", "addresses": [{"city": "SF"}]}
]
}
""";
JSONObject complex = JSON.parseObject(complexJson);
// Access nested arrays and objects
JSONArray users = complex.getJSONArray("users");
JSONObject firstUser = users.getJSONObject(0);
JSONArray addresses = firstUser.getJSONArray("addresses");
String firstCity = addresses.getJSONObject(0).getString("city"); // "NYC"
// Using JSONPath for complex access
String firstUserCity = (String) complex.getByPath("$.users[0].addresses[0].city"); // "NYC"
// Convert specific nested objects
User firstUserObj = firstUser.to(User.class);
List<Address> userAddresses = addresses.toJavaList(Address.class);// JSONObject conversions
JSONObject obj = JSON.parseObject("{\"id\":1,\"name\":\"Product\",\"price\":99.99}");
// Safe type conversion with null handling
Integer id = obj.getInteger("id"); // 1
String name = obj.getString("name"); // "Product"
Double price = obj.getDouble("price"); // 99.99
Integer missing = obj.getInteger("missing"); // null
// Convert entire object
Product product = obj.to(Product.class);
// JSONArray conversions
JSONArray numbers = JSON.parseArray("[1,2,3,4,5]");
JSONArray mixed = JSON.parseArray("[\"hello\",42,true,null]");
String first = mixed.getString(0); // "hello"
Integer second = mixed.getInteger(1); // 42
Boolean third = mixed.getBoolean(2); // true
Object fourth = mixed.get(3); // null
// Convert to standard Java collections
List<Integer> numberList = numbers.toJavaList(Integer.class);
Object[] mixedArray = mixed.toArray();Install with Tessl CLI
npx tessl i tessl/maven-com-alibaba-fastjson2--fastjson2