Class OpenSerializer

java.lang.Object
cloud.opencode.base.serialization.OpenSerializer

public final class OpenSerializer extends Object
OpenSerializer - Unified Serialization Facade 统一序列化门面

This is the main entry point for all serialization operations. It provides a unified API for serializing and deserializing objects using various formats (JSON, XML, Kryo, Protobuf, etc.).

这是所有序列化操作的主入口点。它提供统一的 API, 用于使用各种格式(JSON、XML、Kryo、Protobuf 等)序列化和反序列化对象。

Features | 主要功能:

  • Unified API for all formats - 所有格式的统一 API
  • SPI-based serializer discovery - 基于 SPI 的序列化器发现
  • TypeReference support for generics - 泛型的 TypeReference 支持
  • Deep copy and type conversion - 深拷贝和类型转换
  • Thread-safe operation - 线程安全操作

Usage Examples | 使用示例:

// Basic serialization/deserialization
byte[] data = OpenSerializer.serialize(user);
User restored = OpenSerializer.deserialize(data, User.class);

// Specify format
byte[] json = OpenSerializer.serialize(user, "json");
byte[] kryo = OpenSerializer.serialize(user, "kryo");

// String serialization
String jsonStr = OpenSerializer.serializeToString(user);

// Generic types
List<User> users = OpenSerializer.deserialize(data, new TypeReference<List<User>>() {});
List<User> users = OpenSerializer.deserializeList(data, User.class);

// Deep copy
User copy = OpenSerializer.deepCopy(user);

// Type conversion
UserDTO dto = OpenSerializer.convert(user, UserDTO.class);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是

Performance | 性能特性:

  • Time complexity: O(n) where n = object graph size - O(n), n为对象图大小
  • Space complexity: O(n) for serialized bytes - 序列化字节 O(n)
Since:
JDK 25, opencode-base-serialization V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isDeepCloneAvailable

      public static boolean isDeepCloneAvailable()
      Checks if DeepClone module is available. 检查 DeepClone 模块是否可用。
      Returns:
      true if available - 如果可用返回 true
    • register

      public static void register(Serializer serializer)
      Registers a serializer. 注册序列化器。
      Parameters:
      serializer - the serializer to register - 要注册的序列化器
    • setDefault

      public static void setDefault(String format)
      Sets the default serializer by format name. 按格式名称设置默认序列化器。
      Parameters:
      format - the format name - 格式名称
    • setDefault

      public static void setDefault(Serializer serializer)
      Sets the default serializer. 设置默认序列化器。
      Parameters:
      serializer - the serializer - 序列化器
    • setConfig

      public static void setConfig(SerializerConfig config)
      Sets the global configuration. 设置全局配置。
      Parameters:
      config - the configuration - 配置
    • getConfig

      public static SerializerConfig getConfig()
      Gets the global configuration. 获取全局配置。
      Returns:
      the configuration - 配置
    • get

      public static Serializer get(String format)
      Gets a serializer by format name. 按格式名称获取序列化器。
      Parameters:
      format - the format name - 格式名称
      Returns:
      the serializer - 序列化器
      Throws:
      OpenSerializationException - if serializer not found - 如果未找到序列化器
    • getDefault

      public static Serializer getDefault()
      Gets the default serializer. 获取默认序列化器。
      Returns:
      the default serializer - 默认序列化器
    • getFormats

      public static Set<String> getFormats()
      Gets all registered format names. 获取所有已注册的格式名称。
      Returns:
      set of format names - 格式名称集合
    • hasFormat

      public static boolean hasFormat(String format)
      Checks if a format is available. 检查格式是否可用。
      Parameters:
      format - the format name - 格式名称
      Returns:
      true if available - 如果可用则返回 true
    • serialize

      public static byte[] serialize(Object obj)
      Serializes an object to byte array using the default serializer. 使用默认序列化器将对象序列化为字节数组。
      Parameters:
      obj - the object to serialize - 要序列化的对象
      Returns:
      the serialized bytes - 序列化后的字节数组
    • serialize

      public static byte[] serialize(Object obj, String format)
      Serializes an object to byte array using the specified format. 使用指定格式将对象序列化为字节数组。
      Parameters:
      obj - the object to serialize - 要序列化的对象
      format - the format name - 格式名称
      Returns:
      the serialized bytes - 序列化后的字节数组
    • serializeToString

      public static String serializeToString(Object obj)
      Serializes an object to string using the default serializer. 使用默认序列化器将对象序列化为字符串。
      Parameters:
      obj - the object to serialize - 要序列化的对象
      Returns:
      the serialized string - 序列化后的字符串
    • serializeToString

      public static String serializeToString(Object obj, String format)
      Serializes an object to string using the specified format. 使用指定格式将对象序列化为字符串。
      Parameters:
      obj - the object to serialize - 要序列化的对象
      format - the format name - 格式名称
      Returns:
      the serialized string - 序列化后的字符串
    • deserialize

      public static <T> T deserialize(byte[] data, Class<T> type)
      Deserializes byte array to an object using the default serializer. 使用默认序列化器将字节数组反序列化为对象。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target class - 目标类
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(byte[] data, Class<T> type, String format)
      Deserializes byte array to an object using the specified format. 使用指定格式将字节数组反序列化为对象。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      type - the target class - 目标类
      format - the format name - 格式名称
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(String data, Class<T> type)
      Deserializes string to an object using the default serializer. 使用默认序列化器将字符串反序列化为对象。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized string - 序列化的字符串
      type - the target class - 目标类
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(String data, Class<T> type, String format)
      Deserializes string to an object using the specified format. 使用指定格式将字符串反序列化为对象。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized string - 序列化的字符串
      type - the target class - 目标类
      format - the format name - 格式名称
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(byte[] data, TypeReference<T> typeRef)
      Deserializes byte array to a generic type using the default serializer. 使用默认序列化器将字节数组反序列化为泛型类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      typeRef - the type reference - 类型引用
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(String data, TypeReference<T> typeRef)
      Deserializes string to a generic type using the default serializer. 使用默认序列化器将字符串反序列化为泛型类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized string - 序列化的字符串
      typeRef - the type reference - 类型引用
      Returns:
      the deserialized object - 反序列化后的对象
    • deserialize

      public static <T> T deserialize(byte[] data, TypeReference<T> typeRef, String format)
      Deserializes byte array to a generic type using the specified format. 使用指定格式将字节数组反序列化为泛型类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      data - the serialized data - 序列化的数据
      typeRef - the type reference - 类型引用
      format - the format name - 格式名称
      Returns:
      the deserialized object - 反序列化后的对象
    • deserializeList

      public static <T> List<T> deserializeList(byte[] data, Class<T> elementType)
      Deserializes byte array to a List. 将字节数组反序列化为 List。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      data - the serialized data - 序列化的数据
      elementType - the element type - 元素类型
      Returns:
      the deserialized list - 反序列化后的列表
    • deserializeList

      public static <T> List<T> deserializeList(String data, Class<T> elementType)
      Deserializes string to a List. 将字符串反序列化为 List。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      data - the serialized string - 序列化的字符串
      elementType - the element type - 元素类型
      Returns:
      the deserialized list - 反序列化后的列表
    • deserializeSet

      public static <T> Set<T> deserializeSet(byte[] data, Class<T> elementType)
      Deserializes byte array to a Set. 将字节数组反序列化为 Set。
      Type Parameters:
      T - the element type - 元素类型
      Parameters:
      data - the serialized data - 序列化的数据
      elementType - the element type - 元素类型
      Returns:
      the deserialized set - 反序列化后的集合
    • deserializeMap

      public static <K,V> Map<K,V> deserializeMap(byte[] data, Class<K> keyType, Class<V> valueType)
      Deserializes byte array to a Map. 将字节数组反序列化为 Map。
      Type Parameters:
      K - the key type - 键类型
      V - the value type - 值类型
      Parameters:
      data - the serialized data - 序列化的数据
      keyType - the key type - 键类型
      valueType - the value type - 值类型
      Returns:
      the deserialized map - 反序列化后的 Map
    • deserializeMap

      public static <K,V> Map<K,V> deserializeMap(String data, Class<K> keyType, Class<V> valueType)
      Deserializes string to a Map. 将字符串反序列化为 Map。
      Type Parameters:
      K - the key type - 键类型
      V - the value type - 值类型
      Parameters:
      data - the serialized string - 序列化的字符串
      keyType - the key type - 键类型
      valueType - the value type - 值类型
      Returns:
      the deserialized map - 反序列化后的 Map
    • deepCopy

      public static <T> T deepCopy(T obj)
      Creates a deep copy of an object. 创建对象的深拷贝。

      This method delegates to OpenClone for optimal performance if the deepclone module is available. Otherwise, it falls back to serialization-based deep copy.

      如果 deepclone 模块可用,此方法会委托给 OpenClone 以获得最佳性能。 否则,它会降级到基于序列化的深拷贝。

      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      obj - the object to copy - 要拷贝的对象
      Returns:
      the deep copy - 深拷贝
    • deepCopy

      public static <T> T deepCopy(T obj, String format)
      Creates a deep copy of an object using the specified serializer. 使用指定的序列化器创建对象的深拷贝。
      Type Parameters:
      T - the object type - 对象类型
      Parameters:
      obj - the object to copy - 要拷贝的对象
      format - the format name - 格式名称
      Returns:
      the deep copy - 深拷贝
    • convert

      public static <T> T convert(Object source, Class<T> targetType)
      Converts an object to another type using serialization. 使用序列化将对象转换为另一种类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      source - the source object - 源对象
      targetType - the target type - 目标类型
      Returns:
      the converted object - 转换后的对象
    • convert

      public static <T> T convert(Object source, TypeReference<T> typeRef)
      Converts an object to a generic type using serialization. 使用序列化将对象转换为泛型类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      source - the source object - 源对象
      typeRef - the target type reference - 目标类型引用
      Returns:
      the converted object - 转换后的对象
    • convert

      public static <T> T convert(Object source, Class<T> targetType, String format)
      Converts an object to another type using the specified format. 使用指定格式将对象转换为另一种类型。
      Type Parameters:
      T - the target type - 目标类型
      Parameters:
      source - the source object - 源对象
      targetType - the target type - 目标类型
      format - the format name - 格式名称
      Returns:
      the converted object - 转换后的对象