Class OpenSerializer
java.lang.Object
cloud.opencode.base.serialization.OpenSerializer
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 Summary
Modifier and TypeMethodDescriptionstatic <T> Tconvert(Object source, TypeReference<T> typeRef) Converts an object to a generic type using serialization.static <T> TConverts an object to another type using serialization.static <T> TConverts an object to another type using the specified format.static <T> TdeepCopy(T obj) Creates a deep copy of an object.static <T> TCreates a deep copy of an object using the specified serializer.static <T> Tdeserialize(byte[] data, TypeReference<T> typeRef) Deserializes byte array to a generic type using the default serializer.static <T> Tdeserialize(byte[] data, TypeReference<T> typeRef, String format) Deserializes byte array to a generic type using the specified format.static <T> Tdeserialize(byte[] data, Class<T> type) Deserializes byte array to an object using the default serializer.static <T> Tdeserialize(byte[] data, Class<T> type, String format) Deserializes byte array to an object using the specified format.static <T> Tdeserialize(String data, TypeReference<T> typeRef) Deserializes string to a generic type using the default serializer.static <T> Tdeserialize(String data, Class<T> type) Deserializes string to an object using the default serializer.static <T> Tdeserialize(String data, Class<T> type, String format) Deserializes string to an object using the specified format.static <T> List<T> deserializeList(byte[] data, Class<T> elementType) Deserializes byte array to a List.static <T> List<T> deserializeList(String data, Class<T> elementType) Deserializes string to a List.static <K,V> Map <K, V> deserializeMap(byte[] data, Class<K> keyType, Class<V> valueType) Deserializes byte array to a Map.static <K,V> Map <K, V> deserializeMap(String data, Class<K> keyType, Class<V> valueType) Deserializes string to a Map.static <T> Set<T> deserializeSet(byte[] data, Class<T> elementType) Deserializes byte array to a Set.static SerializerGets a serializer by format name.static SerializerConfigGets the global configuration.static SerializerGets the default serializer.Gets all registered format names.static booleanChecks if a format is available.static booleanChecks if DeepClone module is available.static voidregister(Serializer serializer) Registers a serializer.static byte[]Serializes an object to byte array using the default serializer.static byte[]Serializes an object to byte array using the specified format.static StringserializeToString(Object obj) Serializes an object to string using the default serializer.static StringserializeToString(Object obj, String format) Serializes an object to string using the specified format.static voidsetConfig(SerializerConfig config) Sets the global configuration.static voidsetDefault(Serializer serializer) Sets the default serializer.static voidsetDefault(String format) Sets the default serializer by format name.
-
Method Details
-
isDeepCloneAvailable
public static boolean isDeepCloneAvailable()Checks if DeepClone module is available. 检查 DeepClone 模块是否可用。- Returns:
- true if available - 如果可用返回 true
-
register
Registers a serializer. 注册序列化器。- Parameters:
serializer- the serializer to register - 要注册的序列化器
-
setDefault
Sets the default serializer by format name. 按格式名称设置默认序列化器。- Parameters:
format- the format name - 格式名称
-
setDefault
Sets the default serializer. 设置默认序列化器。- Parameters:
serializer- the serializer - 序列化器
-
setConfig
Sets the global configuration. 设置全局配置。- Parameters:
config- the configuration - 配置
-
getConfig
Gets the global configuration. 获取全局配置。- Returns:
- the configuration - 配置
-
get
Gets a serializer by format name. 按格式名称获取序列化器。- Parameters:
format- the format name - 格式名称- Returns:
- the serializer - 序列化器
- Throws:
OpenSerializationException- if serializer not found - 如果未找到序列化器
-
getDefault
Gets the default serializer. 获取默认序列化器。- Returns:
- the default serializer - 默认序列化器
-
getFormats
-
hasFormat
Checks if a format is available. 检查格式是否可用。- Parameters:
format- the format name - 格式名称- Returns:
- true if available - 如果可用则返回 true
-
serialize
Serializes an object to byte array using the default serializer. 使用默认序列化器将对象序列化为字节数组。- Parameters:
obj- the object to serialize - 要序列化的对象- Returns:
- the serialized bytes - 序列化后的字节数组
-
serialize
-
serializeToString
-
serializeToString
-
deserialize
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
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
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
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
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
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
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
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
-
deserializeSet
-
deserializeMap
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
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
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
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
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
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 - 转换后的对象
-