Interface JsonProvider
public interface JsonProvider
JSON Provider - SPI Interface for JSON Processing Engines
JSON 提供者 - JSON 处理引擎的 SPI 接口
This interface defines the contract for JSON processing implementations. Implementations can wrap Jackson, Gson, Fastjson2, or other JSON libraries.
此接口定义 JSON 处理实现的契约。实现可以包装 Jackson、Gson、Fastjson2 或其他 JSON 库。
Implementation Notes | 实现注意事项:
- Implementations must be thread-safe - 实现必须是线程安全的
- All methods should respect the provided JsonConfig - 所有方法应遵循提供的 JsonConfig
- Implementations should register via ServiceLoader - 实现应通过 ServiceLoader 注册
Features | 主要功能:
- SPI interface for pluggable JSON engines - 可插拔JSON引擎的SPI接口
- Full serialization, deserialization, tree model, and streaming API - 完整的序列化、反序列化、树模型和流式API
- Provider priority and availability management - 提供者优先级和可用性管理
Usage Examples | 使用示例:
// Implement the JsonProvider interface
// 实现 JsonProvider 接口
Security | 安全性:
- Thread-safe: Implementation-dependent - 线程安全: 取决于实现
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidconfigure(JsonConfig config) Configures this provider with the given configuration.<T> TconvertValue(Object obj, TypeReference<T> typeReference) Converts an object to another type using a TypeReference.<T> TconvertValue(Object obj, Class<T> clazz) Converts an object to another type.copy()Creates a copy of this provider with independent configuration.createReader(InputStream input) Creates a JsonReader for the given input stream.createReader(Reader reader) Creates a JsonReader for the given reader.createWriter(OutputStream output) Creates a JsonWriter for the given output stream.createWriter(Writer writer) Creates a JsonWriter for the given writer.<T> TfromJson(byte[] json, TypeReference<T> typeReference) Deserializes JSON bytes using a TypeReference.<T> TDeserializes JSON bytes to an object of the specified class.<T> TfromJson(InputStream input, Class<T> clazz) Deserializes from an InputStream to an object of the specified class.<T> TDeserializes from a Reader to an object of the specified class.<T> TfromJson(String json, TypeReference<T> typeReference) Deserializes a JSON string using a TypeReference.<T> TDeserializes a JSON string to an object of the specified class.<T> TDeserializes a JSON string to an object of the specified type.<T> List<T> fromJsonArray(String json, Class<T> elementType) Deserializes a JSON array to a List.<K,V> Map <K, V> fromJsonMap(String json, Class<K> keyType, Class<V> valueType) Deserializes a JSON object to a Map.getName()Returns the provider name (e.g., "jackson", "gson", "fastjson2").default intReturns the priority of this provider (higher = preferred).<T> TReturns the underlying JSON library object (e.g., ObjectMapper for Jackson).Returns the provider version.default booleanReturns whether this provider is available.parseTree(byte[] json) Parses JSON bytes to a JsonNode tree.Parses a JSON string to a JsonNode tree.booleansupportsFeature(JsonFeature feature) Returns whether a feature is supported by this provider.Serializes an object to a JSON string.voidtoJson(Object obj, OutputStream output) Serializes an object to an OutputStream.voidSerializes an object to a Writer.byte[]toJsonBytes(Object obj) Serializes an object to a JSON byte array.<T> TtreeToValue(JsonNode node, Class<T> clazz) Converts a JsonNode to an object of the specified class.valueToTree(Object obj) Converts an object to a JsonNode tree.
-
Method Details
-
getName
String getName()Returns the provider name (e.g., "jackson", "gson", "fastjson2"). 返回提供者名称(如 "jackson"、"gson"、"fastjson2")。- Returns:
- the provider name - 提供者名称
-
getVersion
-
getPriority
default int getPriority()Returns the priority of this provider (higher = preferred). 返回此提供者的优先级(越高越优先)。- Returns:
- the priority (default 0) - 优先级(默认0)
-
isAvailable
default boolean isAvailable()Returns whether this provider is available. 返回此提供者是否可用。- Returns:
- true if available - 如果可用则返回 true
-
configure
Configures this provider with the given configuration. 使用给定配置配置此提供者。- Parameters:
config- the configuration - 配置
-
supportsFeature
Returns whether a feature is supported by this provider. 返回此提供者是否支持某特性。- Parameters:
feature- the feature to check - 要检查的特性- Returns:
- true if supported - 如果支持则返回 true
-
toJson
-
toJsonBytes
Serializes an object to a JSON byte array. 将对象序列化为 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
-
fromJson
-
fromJson
-
fromJson
Deserializes a JSON string using a 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 of the specified class. 将 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 a 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 an InputStream to an object of the specified class. 从输入流反序列化为指定类的对象。- Type Parameters:
T- the target type - 目标类型- Parameters:
input- the input stream - 输入流clazz- the target class - 目标类- Returns:
- the deserialized object - 反序列化的对象
-
fromJson
-
fromJsonArray
-
fromJsonMap
Deserializes a JSON object to a 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 a JsonNode tree. 将 JSON 字节数组解析为 JsonNode 树。- Parameters:
json- the JSON bytes - JSON 字节数组- Returns:
- the root node - 根节点
-
treeToValue
-
valueToTree
-
createReader
Creates a JsonReader for the given input stream. 为给定输入流创建 JsonReader。- Parameters:
input- the input stream - 输入流- Returns:
- the JSON reader - JSON 读取器
-
createReader
Creates a JsonReader for the given reader. 为给定 Reader 创建 JsonReader。- Parameters:
reader- the reader - Reader- Returns:
- the JSON reader - JSON 读取器
-
createWriter
Creates a JsonWriter for the given output stream. 为给定输出流创建 JsonWriter。- Parameters:
output- the output stream - 输出流- Returns:
- the JSON writer - JSON 写入器
-
createWriter
Creates a JsonWriter for the given writer. 为给定 Writer 创建 JsonWriter。- Parameters:
writer- the writer - Writer- Returns:
- the JSON writer - JSON 写入器
-
convertValue
-
convertValue
Converts an object to another type using a TypeReference. 使用 TypeReference 将对象转换为另一种类型。- Type Parameters:
T- the target type - 目标类型- Parameters:
obj- the source object - 源对象typeReference- the type reference - 类型引用- Returns:
- the converted object - 转换后的对象
-
getUnderlyingProvider
<T> T getUnderlyingProvider()Returns the underlying JSON library object (e.g., ObjectMapper for Jackson). 返回底层 JSON 库对象(如 Jackson 的 ObjectMapper)。- Type Parameters:
T- the expected type - 预期类型- Returns:
- the underlying object - 底层对象
-
copy
JsonProvider copy()Creates a copy of this provider with independent configuration. 创建具有独立配置的此提供者的副本。- Returns:
- a new provider instance - 新提供者实例
-