JSON Small and Fast Parser - A lightweight, high-performance JSON processing library for Java
npx @tessl/cli install tessl/maven-net-minidev--json-smart@2.6.0JSON-Smart is a lightweight, high-performance JSON processing library for Java that provides fast parsing, serialization, and dynamic navigation of JSON data. It offers both strict RFC4627 compliance and permissive parsing modes, along with extensive customization options.
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency>import net.minidev.json.JSONValue;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONNavi;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONStyle;import net.minidev.json.JSONValue;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;
// Parse JSON string to Object
Object obj = JSONValue.parse("{\"name\": \"John\", \"age\": 30}");
// Parse to specific type
JSONObject person = JSONValue.parse("{\"name\": \"John\", \"age\": 30}", JSONObject.class);
// Create JSON objects programmatically
JSONObject user = new JSONObject()
.appendField("name", "Alice")
.appendField("age", 25)
.appendField("active", true);
// Create JSON arrays
JSONArray numbers = new JSONArray()
.appendElement(1)
.appendElement(2)
.appendElement(3);
// Serialize to JSON
String json = JSONValue.toJSONString(user);
System.out.println(json); // {"name":"Alice","age":25,"active":true}
// Validate JSON
boolean isValid = JSONValue.isValidJson("{\"test\": true}"); // trueJSON-Smart uses a multi-layered architecture:
JSONValue provides static utility methods for common operationsJSONObject and JSONArray extend standard Java collections with JSON-specific functionalityJSONParser handles configurable parsing with multiple modes (permissive, strict, streaming)JSONNavi provides jQuery-like dynamic traversal and manipulationJsonReader/JsonWriter framework enables custom serialization/deserializationJSONStyle and parser flags control output formatting and parsing behaviorEssential JSON parsing, creation, and serialization functionality using JSONValue, JSONObject, and JSONArray.
// JSONValue - Main entry point
public static Object parse(String s);
public static <T> T parse(String s, Class<T> mapTo);
public static String toJSONString(Object value);
public static boolean isValidJson(String s);
// JSONObject - Object manipulation
public JSONObject appendField(String fieldName, Object fieldValue);
public String getAsString(String key);
public Number getAsNumber(String key);
public void merge(Object o2);
// JSONArray - Array manipulation
public JSONArray appendElement(Object element);
public void merge(Object o2);jQuery-like API for traversing and manipulating JSON structures without predefined types.
// JSONNavi factory methods
public static JSONNavi<JSONAwareEx> newInstance();
public static JSONNavi<JSONObject> newInstanceObject();
public static JSONNavi<JSONArray> newInstanceArray();
// Navigation methods
public JSONNavi<?> at(String key);
public JSONNavi<?> at(int index);
public JSONNavi<T> root();
public JSONNavi<?> up();
// Value access and mutation
public String asString();
public JSONNavi<T> set(String key, Object value);
public JSONNavi<T> add(Object... values);Configurable parsing with multiple modes, streaming support, and detailed error handling.
// JSONParser configuration
public static final int MODE_PERMISSIVE = 1;
public static final int MODE_RFC4627 = 2;
public static final int ACCEPT_SIMPLE_QUOTE = 1;
public static final int ACCEPT_NAN = 4;
public static final int ACCEPT_INCOMPLETE = 8192;
// Parsing methods
public Object parse(String in) throws ParseException;
public <T> T parse(String in, JsonReaderI<T> mapper) throws ParseException;
// MultipleJsonParser for streaming
public Object parseNext() throws ParseException;
public boolean hasNext();Extensible framework for implementing custom JSON serialization and deserialization logic.
// Registration methods
public static <T> void registerWriter(Class<?> cls, JsonWriterI<T> writer);
public static <T> void registerReader(Class<T> type, JsonReaderI<T> mapper);
public static <T> void remapField(Class<T> type, String jsonFieldName, String javaFieldName);
// Custom interfaces
public interface JsonWriterI<T> {
<E extends T> void writeJSONString(E value, Appendable out, JSONStyle compression) throws IOException;
}
public abstract class JsonReaderI<T> {
public abstract T convert(Object current);
public void setValue(Object current, String key, Object value) throws ParseException, IOException;
}Custom Serialization Framework
Control JSON output formatting, compression levels, and parser behavior through comprehensive configuration options.
// JSONStyle constants
public static final JSONStyle NO_COMPRESS;
public static final JSONStyle MAX_COMPRESS;
public static final JSONStyle LT_COMPRESS;
// Formatting flags
public static final int FLAG_PROTECT_KEYS = 1;
public static final int FLAG_PROTECT_VALUES = 4;
public static final int FLAG_IGNORE_NULL = 16;
// JSONStyle methods
public JSONStyle(int FLAG);
public boolean protectKeys();
public boolean ignoreNull();Output Formatting and Configuration
// Core exception type
class ParseException extends Exception {
public int getErrorType();
public int getPosition();
public Object getUnexpectedObject();
}
// JSON interfaces
interface JSONAware {
String toJSONString();
}
interface JSONAwareEx extends JSONAware {
String toJSONString(JSONStyle compression);
}
interface JSONStreamAware {
void writeJSONString(Appendable out) throws IOException;
}
interface JSONStreamAwareEx extends JSONStreamAware {
void writeJSONString(Appendable out, JSONStyle compression) throws IOException;
}
// Annotation
@interface JsonIgnore {
boolean value() default true;
}