Class JsonSerializer
java.lang.Object
cloud.opencode.base.serialization.json.JsonSerializer
- All Implemented Interfaces:
Serializer
JsonSerializer - JSON Serialization (delegates to OpenJson)
JSON 序列化器(委托给 OpenJson)
Provides JSON serialization by delegating to the opencode-base-json component. This ensures consistent JSON handling across the library.
通过委托给 opencode-base-json 组件提供 JSON 序列化。 这确保了整个库中一致的 JSON 处理。
Features | 主要功能:
- Human-readable output - 人类可读的输出
- Full generic type support - 完整的泛型类型支持
- Cross-platform compatibility - 跨平台兼容性
- Text-based format - 基于文本的格式
Usage Examples | 使用示例:
JsonSerializer serializer = new JsonSerializer();
// Serialize
byte[] data = serializer.serialize(user);
String json = new String(data, StandardCharsets.UTF_8);
// Deserialize
User restored = serializer.deserialize(data, User.class);
// Generic types
List<User> users = serializer.deserialize(data, new TypeReference<List<User>>() {});
Security | 安全性:
- Thread-safe: Yes (delegates to thread-safe OpenJson) - 线程安全: 是(委托给线程安全的 OpenJson)
Performance | 性能特性:
- Time complexity: O(n) where n = object graph size - O(n), n为对象图大小
- Space complexity: O(n) for JSON string - JSON字符串 O(n)
- Since:
- JDK 25, opencode-base-serialization V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Tdeserialize(byte[] data, TypeReference<T> typeRef) Deserializes byte array to a generic type using TypeReference.<T> Tdeserialize(byte[] data, Class<T> type) Deserializes byte array to an object of the specified class.<T> Tdeserialize(byte[] data, Type type) Deserializes byte array using a Type.Returns the format name of this serializer.Returns the MIME type for this serializer.booleanReturns whether this serializer produces text output.byte[]Serializes an object to byte array.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Serializer
deserialize, deserialize, info, serialize, supports
-
Field Details
-
FORMAT
-
-
Constructor Details
-
JsonSerializer
public JsonSerializer()
-
-
Method Details
-
serialize
Description copied from interface:SerializerSerializes an object to byte array. 将对象序列化为字节数组。- Specified by:
serializein interfaceSerializer- Parameters:
obj- the object to serialize - 要序列化的对象- Returns:
- the serialized bytes - 序列化后的字节数组
-
deserialize
Description copied from interface:SerializerDeserializes byte array to an object of the specified class. 将字节数组反序列化为指定类的对象。- Specified by:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据type- the target class - 目标类- Returns:
- the deserialized object - 反序列化后的对象
-
deserialize
Description copied from interface:SerializerDeserializes byte array to a generic type using TypeReference. 使用 TypeReference 将字节数组反序列化为泛型类型。This method preserves generic type information that would otherwise be lost due to type erasure.
此方法保留了由于类型擦除而丢失的泛型类型信息。
- Specified by:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据typeRef- the type reference - 类型引用- Returns:
- the deserialized object - 反序列化后的对象
-
deserialize
Description copied from interface:SerializerDeserializes byte array using a Type. 使用 Type 反序列化字节数组。- Specified by:
deserializein interfaceSerializer- Type Parameters:
T- the target type - 目标类型- Parameters:
data- the serialized data - 序列化的数据type- the target type - 目标类型- Returns:
- the deserialized object - 反序列化后的对象
-
getFormat
Description copied from interface:SerializerReturns the format name of this serializer. 返回此序列化器的格式名称。Examples: "json", "xml", "kryo", "protobuf", "jdk"
示例: "json", "xml", "kryo", "protobuf", "jdk"
- Specified by:
getFormatin interfaceSerializer- Returns:
- the format name - 格式名称
-
getMimeType
Description copied from interface:SerializerReturns the MIME type for this serializer. 返回此序列化器的 MIME 类型。- Specified by:
getMimeTypein interfaceSerializer- Returns:
- the MIME type (default: "application/octet-stream") - MIME 类型
-
isTextBased
public boolean isTextBased()Description copied from interface:SerializerReturns whether this serializer produces text output. 返回此序列化器是否产生文本输出。Text-based serializers (JSON, XML) can be converted to String without data loss.
基于文本的序列化器(JSON、XML)可以无损转换为字符串。
- Specified by:
isTextBasedin interfaceSerializer- Returns:
- true if text-based - 如果是基于文本的则返回 true
-