Class GsonRedisSerializer<T>
java.lang.Object
io.github.xezzon.zeroweb.common.redis.GsonRedisSerializer<T>
- Type Parameters:
T- 序列化和反序列化的对象类型
- All Implemented Interfaces:
org.springframework.data.redis.serializer.RedisSerializer<@NonNull T>
public class GsonRedisSerializer<T>
extends Object
implements org.springframework.data.redis.serializer.RedisSerializer<@NonNull T>
使用 Gson 进行序列化和反序列化的 Redis 序列化器。
此序列化器能够将 Java 对象序列化为 UTF-8 编码的 JSON 字符串字节数组,
并将字节数组反序列化回 Java 对象。
- Author:
- xezzon
-
Constructor Summary
ConstructorsConstructorDescriptionGsonRedisSerializer(com.google.common.reflect.TypeToken<@NonNull T> typeToken) 构造一个新的GsonRedisSerializer实例。 -
Method Summary
Modifier and TypeMethodDescription@Nullable Tdeserialize(byte @Nullable [] bytes) 从字节数组反序列化为对象。byte @NonNull []将给定对象序列化为字节数组。Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.redis.serializer.RedisSerializer
canSerialize, getTargetType
-
Constructor Details
-
GsonRedisSerializer
构造一个新的GsonRedisSerializer实例。- Parameters:
typeToken- 用于获取泛型类型的TypeToken,确保在运行时可以正确反序列化泛型对象。
-
-
Method Details
-
serialize
将给定对象序列化为字节数组。- Specified by:
serializein interfaceorg.springframework.data.redis.serializer.RedisSerializer<T>- Parameters:
value- 要序列化的对象,可以为null。- Returns:
- 对象的 UTF-8 编码 JSON 字节数组。如果输入值为
null,则返回表示 "null" 的字节数组。 - Throws:
org.springframework.data.redis.serializer.SerializationException- 如果序列化过程中发生错误。
-
deserialize
从字节数组反序列化为对象。- Specified by:
deserializein interfaceorg.springframework.data.redis.serializer.RedisSerializer<T>- Parameters:
bytes- 包含对象 UTF-8 编码 JSON 数据的字节数组,可以为null。- Returns:
- 反序列化后的对象实例。如果输入字节数组为
null,则返回null。 - Throws:
org.springframework.data.redis.serializer.SerializationException- 如果反序列化过程中发生错误。
-