中国纪检监察报总编,做seo推广大概多少钱,58同城最新招聘信息,怎么投诉网络平台工具类#xff1a;FastJsonRedisSerializer 依赖yml文件FastJsonRedisSerializer.java 依赖 !-- 主要用于处理 JSON 数据的序列化和反序列化--!-- 序列化#xff1a;将对象转换为一种可以存储或传输的格式#xff08;如 JSON、XML、二进制等#xff09… 工具类FastJsonRedisSerializer 依赖yml文件FastJsonRedisSerializer.java 依赖 !-- 主要用于处理 JSON 数据的序列化和反序列化--!-- 序列化将对象转换为一种可以存储或传输的格式如 JSON、XML、二进制等。这样可以将对象的状态保存到文件中或通过网络发送到其他系统。--!-- 反序列化将序列化后的数据重新转换为对象的过程。这使得程序能够读取存储的数据并将其恢复为原来的对象形式。--dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.53/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencyyml文件 jackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT8FastJsonRedisSerializer.java
package com.nnutc.common.utils;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;/*** FastJsonRedisSerializer 是一个使用 FastJSON 库进行对象序列化和反序列化的 RedisSerializer 实现类。** param T 被序列化和反序列化的对象类型*/
public class FastJsonRedisSerializerT implements RedisSerializerT {public static final Charset DEFAULT_CHARSET StandardCharsets.UTF_8;private final ClassT clazz; // 被序列化对象的类类型static {// 启用 FastJSON 的自动类型支持ParserConfig.getGlobalInstance().setAutoTypeSupport(true);}/*** 构造函数接受一个 ClassT 类型的参数** param clazz 被序列化对象的类类型*/public FastJsonRedisSerializer(ClassT clazz) {this.clazz clazz;}/*** 序列化方法将对象转换为字节数组** param t 被序列化的对象* return 序列化后的字节数组* throws SerializationException 如果序列化失败*/Overridepublic byte[] serialize(T t) throws SerializationException {if (t null) {return new byte[0]; // 如果对象为 null返回空字节数组}try {return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);} catch (Exception e) {throw new SerializationException(序列化对象失败: t, e);}}/*** 反序列化方法将字节数组转换为对象** param bytes 要反序列化的字节数组* return 反序列化得到的对象* throws SerializationException 如果反序列化失败*/Overridepublic T deserialize(byte[] bytes) throws SerializationException {if (bytes null || bytes.length 0) {return null; // 如果字节数组为 null 或空返回 null}try {String json new String(bytes, DEFAULT_CHARSET); // 将字节数组转换为 JSON 字符串return JSON.parseObject(json, clazz); // 使用 FastJSON 将 JSON 字符串解析为指定类型的对象} catch (Exception e) {throw new SerializationException(反序列化字节数组失败: bytes, e);}}
}