Class OpenJson
java.lang.Object
cloud.opencode.base.json.OpenJson
OpenJson - Unified JSON Processing Facade
OpenJson - 统一 JSON 处理门面
This is the main entry point for all JSON operations in the OpenCode JSON library. It provides a unified API that delegates to pluggable JSON provider implementations (Jackson, Gson, Fastjson2, etc.).
这是 OpenCode JSON 库中所有 JSON 操作的主入口点。 它提供统一的 API,委托给可插拔的 JSON 提供者实现(Jackson、Gson、Fastjson2 等)。
Features | 特性:
- Serialization/Deserialization - 序列化/反序列化
- Tree Model (JsonNode) - 树模型
- Streaming API - 流式 API
- JSONPath & JSON Pointer - JSONPath 和 JSON Pointer
- JSON Patch & Merge Patch - JSON Patch 和 Merge Patch
- JSON Schema Validation - JSON Schema 验证
- Custom Type Adapters - 自定义类型适配器
- Security Features - 安全特性
Basic Usage | 基本用法:
// Serialize to JSON
String json = OpenJson.toJson(user);
// Deserialize from JSON
User user = OpenJson.fromJson(json, User.class);
// Parse to tree
JsonNode node = OpenJson.parse(json);
// Query with JSONPath
List<JsonNode> results = OpenJson.select(node, "$.users[*].name");
// Custom configuration
OpenJson json = OpenJson.withConfig(config);
Features | 主要功能:
- Unified facade for all JSON operations - 所有JSON操作的统一门面
- Pluggable provider architecture (Jackson, Gson, Fastjson2) - 可插拔提供者架构
- Static and instance API support - 支持静态和实例API
Usage Examples | 使用示例:
// See class-level documentation for usage
// 参见类级文档了解用法
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionAdds a mixin annotation class for a target type.static JsonNodeEvaluates a JSON Pointer against a node.static Stringcanonicalize(JsonNode node) Produces RFC 8785 canonical JSON output for the given node.static JsonReadercreateReader(InputStream input) Creates a JsonReader for an InputStream.static JsonReadercreateReader(Reader reader) Creates a JsonReader for a Reader.static JsonWritercreateWriter(OutputStream output) Creates a JsonWriter for an OutputStream.static JsonWritercreateWriter(Writer writer) Creates a JsonWriter for a Writer.<T> Tdeserialize(byte[] json, TypeReference<T> typeReference) Deserializes JSON bytes using TypeReference.<T> Tdeserialize(byte[] json, Class<T> clazz) Deserializes JSON bytes.<T> Tdeserialize(InputStream input, Class<T> clazz) Deserializes from InputStream.<T> Tdeserialize(Reader reader, Class<T> clazz) Deserializes from Reader.<T> Tdeserialize(String json, TypeReference<T> typeReference) Deserializes JSON using TypeReference.<T> Tdeserialize(String json, Class<T> clazz) Deserializes JSON to an object.<T> List<T> deserializeArray(String json, Class<T> elementType) Deserializes JSON array to List.<K,V> Map <K, V> deserializeMap(String json, Class<K> keyType, Class<V> valueType) Deserializes JSON object to Map.static JsonDiff.DiffResultComputes diff between two JSON documents.Flattens a nested JSON node into a flat key-value map using dot notation.static <T> TfromJson(byte[] json, TypeReference<T> typeReference) Deserializes JSON bytes using TypeReference.static <T> TDeserializes JSON bytes to an object.static <T> TfromJson(InputStream input, Class<T> clazz) Deserializes from InputStream.static <T> TDeserializes from Reader.static <T> TfromJson(String json, TypeReference<T> typeReference) Deserializes JSON string using TypeReference.static <T> TDeserializes JSON string to an object.static <T> List<T> fromJsonArray(String json, Class<T> elementType) Deserializes JSON array to List.static <K,V> Map <K, V> fromJsonMap(String json, Class<K> keyType, Class<V> valueType) Deserializes JSON object to Map.Returns the configuration.Returns the mixin source.Returns the registered modules.getPropertyFilter(String filterId) Returns the property filter for the given ID.Returns the provider.static booleanChecks whether the given string is valid JSON.static JsonNodemergePatch(JsonNode target, JsonNode patch) Applies a JSON Merge Patch to a document.static StringMinifies a JSON string by removing unnecessary whitespace.static JsonNodeparse(byte[] json) Parses JSON bytes to JsonNode tree.static JsonNodeParses JSON string to JsonNode tree.parseTree(byte[] json) Parses JSON bytes to tree.Parses JSON to tree.static JsonNodeApplies a JSON Patch to a document.static StringprettyPrint(String json) Pretty-prints a JSON string with standard 2-space indentation.registerModule(JsonModule module) Registers a JSON module with this instance.Selects nodes using JSONPath.static JsonNodeselectFirst(JsonNode node, String path) Selects first matching node using JSONPath.Serializes an object to JSON string.voidserialize(Object obj, OutputStream output) Serializes an object to an OutputStream.voidSerializes an object to a Writer.byte[]serializeToBytes(Object obj) Serializes an object to JSON bytes.setPropertyFilter(String filterId, PropertyFilter filter) Registers a named property filter.static booleanstructuralEquals(JsonNode a, JsonNode b) Compares two JSON nodes for structural equality (ignoring object key order).static StringSerializes an object to JSON string.static voidtoJson(Object obj, OutputStream output) Serializes an object to an OutputStream.static voidSerializes an object to a Writer.static byte[]toJsonBytes(Object obj) Serializes an object to JSON bytes.static StringtoPrettyJson(Object obj) Serializes an object to pretty-printed JSON.static JsonNodeConverts object to JsonNode tree.<T> TtreeToObject(JsonNode node, Class<T> clazz) Converts tree to object.static <T> TtreeToValue(JsonNode node, Class<T> clazz) Converts JsonNode to object.static StringTruncates a JSON string to the specified maximum length for logging.static JsonNodeRestores a flattened map back to a nested JSON node.Validates JSON against a schema.static voidvalidateOrThrow(JsonNode data, JsonNode schema) Validates and throws if invalid.valueToTree(Object obj) Converts object to tree.static OpenJsonwithConfig(JsonConfig config) Creates a new OpenJson instance with the specified configuration.static OpenJsonwithConfigAndProvider(JsonConfig config, String providerName) Creates a new OpenJson instance with configuration and provider.static OpenJsonwithProvider(String providerName) Creates a new OpenJson instance with the specified provider.
-
Method Details
-
withConfig
Creates a new OpenJson instance with the specified configuration. 使用指定配置创建新的 OpenJson 实例。- Parameters:
config- the configuration - 配置- Returns:
- the OpenJson instance - OpenJson 实例
-
withProvider
-
withConfigAndProvider
Creates a new OpenJson instance with configuration and provider. 使用配置和提供者创建新的 OpenJson 实例。- Parameters:
config- the configuration - 配置providerName- the provider name - 提供者名称- Returns:
- the OpenJson instance - OpenJson 实例
-
toJson
-
toJsonBytes
Serializes an object to JSON bytes. 将对象序列化为 JSON 字节数组。- Parameters:
obj- the object to serialize - 要序列化的对象- Returns:
- the JSON bytes - JSON 字节数组
-
toJson
Serializes an object to an OutputStream. 将对象序列化到输出流。- Parameters:
obj- the object to serialize - 要序列化的对象output- the output stream - 输出流
-
toJson
-
toPrettyJson
-
fromJson
-
fromJson
Deserializes JSON string using TypeReference. 使用 TypeReference 反序列化 JSON 字符串。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON string - JSON 字符串typeReference- the type reference - 类型引用- Returns:
- the deserialized object - 反序列化的对象
-
fromJson
Deserializes JSON bytes to an object. 将 JSON 字节数组反序列化为对象。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON bytes - JSON 字节数组clazz- the target class - 目标类- Returns:
- the deserialized object - 反序列化的对象
-
fromJson
Deserializes JSON bytes using TypeReference. 使用 TypeReference 反序列化 JSON 字节数组。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON bytes - JSON 字节数组typeReference- the type reference - 类型引用- Returns:
- the deserialized object - 反序列化的对象
-
fromJson
Deserializes from InputStream. 从输入流反序列化。- Type Parameters:
T- the target type - 目标类型- Parameters:
input- the input stream - 输入流clazz- the target class - 目标类- Returns:
- the deserialized object - 反序列化的对象
-
fromJson
-
fromJsonArray
-
fromJsonMap
Deserializes JSON object to Map. 将 JSON 对象反序列化为 Map。- Type Parameters:
K- the key type - 键类型V- the value type - 值类型- Parameters:
json- the JSON string - JSON 字符串keyType- the key type - 键类型valueType- the value type - 值类型- Returns:
- the map - Map
-
parse
-
parse
Parses JSON bytes to JsonNode tree. 将 JSON 字节数组解析为 JsonNode 树。- Parameters:
json- the JSON bytes - JSON 字节数组- Returns:
- the root node - 根节点
-
toTree
-
treeToValue
-
at
-
select
-
selectFirst
-
diff
Computes diff between two JSON documents. 计算两个 JSON 文档之间的差异。- Parameters:
source- the source document - 源文档target- the target document - 目标文档- Returns:
- the diff result - 差异结果
-
patch
-
mergePatch
-
validate
Validates JSON against a schema. 根据 schema 验证 JSON。- Parameters:
data- the data to validate - 要验证的数据schema- the JSON Schema - JSON Schema- Returns:
- the validation result - 验证结果
-
validateOrThrow
-
isValid
Checks whether the given string is valid JSON. 检查给定字符串是否为合法 JSON。- Parameters:
json- the string to check - 要检查的字符串- Returns:
- true if valid JSON - 如果是合法 JSON 则返回 true
-
minify
-
prettyPrint
-
structuralEquals
-
flatten
-
unflatten
-
canonicalize
-
truncate
-
createReader
Creates a JsonReader for an InputStream. 为输入流创建 JsonReader。- Parameters:
input- the input stream - 输入流- Returns:
- the JSON reader - JSON 读取器
-
createReader
Creates a JsonReader for a Reader. 为 Reader 创建 JsonReader。- Parameters:
reader- the reader - Reader- Returns:
- the JSON reader - JSON 读取器
-
createWriter
Creates a JsonWriter for an OutputStream. 为输出流创建 JsonWriter。- Parameters:
output- the output stream - 输出流- Returns:
- the JSON writer - JSON 写入器
-
createWriter
Creates a JsonWriter for a Writer. 为 Writer 创建 JsonWriter。- Parameters:
writer- the writer - Writer- Returns:
- the JSON writer - JSON 写入器
-
serialize
-
serializeToBytes
Serializes an object to JSON bytes. 将对象序列化为 JSON 字节数组。- Parameters:
obj- the object - 对象- Returns:
- the JSON bytes - JSON 字节数组
-
serialize
Serializes an object to an OutputStream. 将对象序列化到输出流。- Parameters:
obj- the object - 对象output- the output stream - 输出流
-
serialize
-
deserialize
-
deserialize
Deserializes JSON using TypeReference. 使用 TypeReference 反序列化 JSON。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON string - JSON 字符串typeReference- the type reference - 类型引用- Returns:
- the object - 对象
-
deserialize
Deserializes JSON bytes. 反序列化 JSON 字节数组。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON bytes - JSON 字节数组clazz- the target class - 目标类- Returns:
- the object - 对象
-
deserialize
Deserializes JSON bytes using TypeReference. 使用 TypeReference 反序列化 JSON 字节数组。- Type Parameters:
T- the target type - 目标类型- Parameters:
json- the JSON bytes - JSON 字节数组typeReference- the type reference - 类型引用- Returns:
- the object - 对象
-
deserialize
Deserializes from InputStream. 从输入流反序列化。- Type Parameters:
T- the target type - 目标类型- Parameters:
input- the input stream - 输入流clazz- the target class - 目标类- Returns:
- the object - 对象
-
deserialize
-
deserializeArray
-
deserializeMap
Deserializes JSON object to Map. 将 JSON 对象反序列化为 Map。- Type Parameters:
K- the key type - 键类型V- the value type - 值类型- Parameters:
json- the JSON string - JSON 字符串keyType- the key type - 键类型valueType- the value type - 值类型- Returns:
- the map - Map
-
parseTree
-
parseTree
Parses JSON bytes to tree. 将 JSON 字节数组解析为树。- Parameters:
json- the JSON bytes - JSON 字节数组- Returns:
- the root node - 根节点
-
valueToTree
-
treeToObject
-
registerModule
Registers a JSON module with this instance. 向此实例注册 JSON 模块。The module's
JsonModule.setupModule(JsonModule.SetupContext)method is called immediately to register all components.模块的
JsonModule.setupModule(JsonModule.SetupContext)方法会被立即调用以注册所有组件。- Parameters:
module- the module to register - 要注册的模块- Returns:
- this instance for chaining - 此实例,用于链式调用
-
addMixin
-
setPropertyFilter
Registers a named property filter. 注册命名属性过滤器。- Parameters:
filterId- the filter identifier - 过滤器标识符filter- the property filter - 属性过滤器- Returns:
- this instance for chaining - 此实例,用于链式调用
-
getMixinSource
Returns the mixin source. 返回混入源。- Returns:
- the mixin source - 混入源
-
getPropertyFilter
Returns the property filter for the given ID. 返回给定 ID 的属性过滤器。- Parameters:
filterId- the filter identifier - 过滤器标识符- Returns:
- the filter, or null if not found - 过滤器,如果未找到则返回 null
-
getModules
Returns the registered modules. 返回注册的模块。- Returns:
- unmodifiable list of modules - 不可修改的模块列表
-
getConfig
-
getProvider
-