Class JsonAdapterRegistry

java.lang.Object
cloud.opencode.base.json.adapter.JsonAdapterRegistry

public final class JsonAdapterRegistry extends Object
JSON Adapter Registry - Registry for Custom Type Adapters JSON 适配器注册表 - 自定义类型适配器的注册表

This class manages registration and lookup of custom type adapters for JSON serialization and deserialization.

此类管理 JSON 序列化和反序列化的自定义类型适配器的注册和查找。

Example | 示例:

// Register a custom adapter
JsonAdapterRegistry.register(new MoneyAdapter());

// Get adapter for a type
JsonTypeAdapter<Money> adapter = JsonAdapterRegistry.getAdapter(Money.class);

// Use adapter
JsonNode json = adapter.toJson(new Money(100, "USD"));
Money money = adapter.fromJson(json);

Features | 主要功能:

  • Global and isolated adapter registries - 全局和隔离的适配器注册表
  • Built-in adapters for common Java types - 常见Java类型的内置适配器
  • Factory pattern for dynamic adapter creation - 动态适配器创建的工厂模式

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • register

      public static <T> void register(JsonTypeAdapter<T> adapter)
      Registers a type adapter. 注册类型适配器。
      Type Parameters:
      T - the type - 类型
      Parameters:
      adapter - the adapter to register - 要注册的适配器
    • register

      public static <T> void register(Class<T> type, JsonTypeAdapter<T> adapter)
      Registers an adapter for a specific type. 为特定类型注册适配器。
      Type Parameters:
      T - the type - 类型
      Parameters:
      type - the type - 类型
      adapter - the adapter - 适配器
    • registerFactory

      public static void registerFactory(JsonAdapterRegistry.AdapterFactory factory)
      Registers an adapter factory. 注册适配器工厂。
      Parameters:
      factory - the factory - 工厂
    • getAdapter

      public static <T> JsonTypeAdapter<T> getAdapter(Class<T> type)
      Gets an adapter for a type. 获取类型的适配器。
      Type Parameters:
      T - the type - 类型
      Parameters:
      type - the type - 类型
      Returns:
      the adapter, or null if not found - 适配器,如果未找到则返回 null
    • getAdapter

      public static JsonTypeAdapter<?> getAdapter(Type type)
      Gets an adapter for a generic type. 获取泛型类型的适配器。
      Parameters:
      type - the type - 类型
      Returns:
      the adapter, or null if not found - 适配器,如果未找到则返回 null
    • hasAdapter

      public static boolean hasAdapter(Class<?> type)
      Checks if an adapter exists for a type. 检查类型是否存在适配器。
      Parameters:
      type - the type - 类型
      Returns:
      true if adapter exists - 如果存在适配器则返回 true
    • unregister

      public static JsonTypeAdapter<?> unregister(Class<?> type)
      Unregisters an adapter for a type. 注销类型的适配器。
      Parameters:
      type - the type - 类型
      Returns:
      the removed adapter, or null - 移除的适配器,或 null
    • getRegisteredTypes

      public static Set<Type> getRegisteredTypes()
      Returns all registered types. 返回所有注册的类型。
      Returns:
      set of registered types - 注册类型的集合
    • clear

      public static void clear()
      Clears all custom adapters (keeps built-in). 清除所有自定义适配器(保留内置)。
    • createRegistry

      public static JsonAdapterRegistry.Registry createRegistry()
      Creates a new isolated registry. 创建新的隔离注册表。
      Returns:
      a new registry instance - 新注册表实例