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:
  • Method Details

    • getType

      Class<T> getType()
      Returns the type this adapter handles. 返回此适配器处理的类型。
      Returns:
      the type class - 类型类
    • getGenericType

      default Type getGenericType()
      Returns the generic type if applicable. 返回泛型类型(如果适用)。
      Returns:
      the generic type, or null - 泛型类型,或 null
    • toJson

      JsonNode toJson(T value)
      Serializes a value to a JsonNode. 将值序列化为 JsonNode。
      Parameters:
      value - the value to serialize - 要序列化的值
      Returns:
      the JSON node - JSON 节点
    • fromJson

      T fromJson(JsonNode node)
      Deserializes a JsonNode to a value. 将 JsonNode 反序列化为值。
      Parameters:
      node - the JSON node - JSON 节点
      Returns:
      the deserialized value - 反序列化的值
    • write

      default void write(JsonWriter writer, T value)
      Writes the value directly to a JsonWriter (optional). 直接将值写入 JsonWriter(可选)。
      Parameters:
      writer - the JSON writer - JSON 写入器
      value - the value to write - 要写入的值
    • read

      default T read(JsonReader reader)
      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 - 适配器