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

    Constructors
    Constructor
    Description
    GsonRedisSerializer(com.google.common.reflect.TypeToken<@NonNull T> typeToken)
    构造一个新的 GsonRedisSerializer 实例。
  • Method Summary

    Modifier and Type
    Method
    Description
    @Nullable T
    deserialize(byte @Nullable [] bytes)
    从字节数组反序列化为对象。
    byte @NonNull []
    serialize(@Nullable T value)
    将给定对象序列化为字节数组。

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.data.redis.serializer.RedisSerializer

    canSerialize, getTargetType
  • Constructor Details

    • GsonRedisSerializer

      public GsonRedisSerializer(com.google.common.reflect.TypeToken<@NonNull T> typeToken)
      构造一个新的 GsonRedisSerializer 实例。
      Parameters:
      typeToken - 用于获取泛型类型的 TypeToken,确保在运行时可以正确反序列化泛型对象。
  • Method Details

    • serialize

      public byte @NonNull [] serialize(@Nullable T value)
      将给定对象序列化为字节数组。
      Specified by:
      serialize in interface org.springframework.data.redis.serializer.RedisSerializer<T>
      Parameters:
      value - 要序列化的对象,可以为 null
      Returns:
      对象的 UTF-8 编码 JSON 字节数组。如果输入值为 null,则返回表示 "null" 的字节数组。
      Throws:
      org.springframework.data.redis.serializer.SerializationException - 如果序列化过程中发生错误。
    • deserialize

      public @Nullable T deserialize(byte @Nullable [] bytes)
      从字节数组反序列化为对象。
      Specified by:
      deserialize in interface org.springframework.data.redis.serializer.RedisSerializer<T>
      Parameters:
      bytes - 包含对象 UTF-8 编码 JSON 数据的字节数组,可以为 null
      Returns:
      反序列化后的对象实例。如果输入字节数组为 null,则返回 null
      Throws:
      org.springframework.data.redis.serializer.SerializationException - 如果反序列化过程中发生错误。