Interface JsonTypeAdapter<T>
- Type Parameters:
T- the type this adapter handles - 此适配器处理的类型Features | 主要功能:
- Custom serialization/deserialization for specific types - 特定类型的自定义序列化/反序列化
- Optional streaming read/write support - 可选的流式读/写支持
- Lambda-based adapter creation via factory methods - 通过工厂方法的Lambda适配器创建
Security | 安全性:
- Thread-safe: Implementation-dependent - 线程安全: 取决于实现
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- All Known Implementing Classes:
JsonTypeAdapter.None
public interface JsonTypeAdapter<T>
JSON Type Adapter - Custom Serialization/Deserialization
JSON 类型适配器 - 自定义序列化/反序列化
This interface defines custom serialization and deserialization logic for specific types. Implementations can be registered with the JsonAdapterRegistry.
此接口定义特定类型的自定义序列化和反序列化逻辑。 实现可以注册到 JsonAdapterRegistry。
Example | 示例:
public class MoneyAdapter implements JsonTypeAdapter<Money> {
@Override
public Class<Money> getType() {
return Money.class;
}
@Override
public JsonNode toJson(Money value) {
return JsonNode.object()
.put("amount", value.getAmount())
.put("currency", value.getCurrency());
}
@Override
public Money fromJson(JsonNode node) {
return new Money(
node.get("amount").asBigDecimal(),
node.get("currency").asString()
);
}
}
// Register
JsonAdapterRegistry.register(new MoneyAdapter());
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classPlaceholder class used as default value for annotation attributes that reference aJsonTypeAdapter. -
Method Summary
Modifier and TypeMethodDescriptionDeserializes a JsonNode to a value.default TypeReturns the generic type if applicable.getType()Returns the type this adapter handles.default booleanReturns whether null values should use this adapter.static <T> JsonTypeAdapter<T> Creates a simple adapter from lambdas.static <T> JsonTypeAdapter<T> Creates an adapter for simple string conversion.default Tread(JsonReader reader) Reads the value directly from a JsonReader (optional).default booleanReturns whether this adapter supports streaming.Serializes a value to a JsonNode.default voidwrite(JsonWriter writer, T value) Writes the value directly to a JsonWriter (optional).
-
Method Details
-
getType
-
getGenericType
Returns the generic type if applicable. 返回泛型类型(如果适用)。- Returns:
- the generic type, or null - 泛型类型,或 null
-
toJson
-
fromJson
-
write
Writes the value directly to a JsonWriter (optional). 直接将值写入 JsonWriter(可选)。- Parameters:
writer- the JSON writer - JSON 写入器value- the value to write - 要写入的值
-
read
Reads the value directly from a JsonReader (optional). 直接从 JsonReader 读取值(可选)。- Parameters:
reader- the JSON reader - JSON 读取器- Returns:
- the read value - 读取的值
-
supportsStreaming
default boolean supportsStreaming()Returns whether this adapter supports streaming. 返回此适配器是否支持流式处理。- Returns:
- true if streaming is supported - 如果支持流式处理则返回 true
-
handlesNull
default boolean handlesNull()Returns whether null values should use this adapter. 返回是否应为 null 值使用此适配器。- Returns:
- true to handle nulls - 如果处理 null 则返回 true
-
of
static <T> JsonTypeAdapter<T> of(Class<T> type, Function<T, JsonNode> serializer, Function<JsonNode, T> deserializer) Creates a simple adapter from lambdas. 从 lambda 创建简单适配器。- Type Parameters:
T- the type - 类型- Parameters:
type- the type class - 类型类serializer- the serialization function - 序列化函数deserializer- the deserialization function - 反序列化函数- Returns:
- the adapter - 适配器
-
ofString
static <T> JsonTypeAdapter<T> ofString(Class<T> type, Function<T, String> toString, Function<String, T> fromString) Creates an adapter for simple string conversion. 创建简单字符串转换的适配器。- Type Parameters:
T- the type - 类型- Parameters:
type- the type class - 类型类toString- convert to string - 转换为字符串fromString- convert from string - 从字符串转换- Returns:
- the adapter - 适配器
-