public static void main(String[] args) {
RedisSerializer<Object> serializer = new GenericFastJsonRedisSerializer(new String[]{"cn.", "com.", "java."});
Map<ApplicationType, List<ApplicationDO>> a = new EnumMap<>(ApplicationType.class);
ApplicationDO code = new ApplicationDO();
code.setId(1L);
code.setEnabled(true);
code.setCreateTime(LocalDateTime.now());
a.computeIfAbsent(ApplicationType.AI, it -> new ArrayList<>()).add(code);
byte[] serialize = serializer.serialize(a);
System.out.println(new String(serialize));
HashMap<ApplicationType, List<ApplicationDO>> deserialize = (HashMap<ApplicationType, List<ApplicationDO>>) serializer.deserialize(serialize);
System.out.println(deserialize);
System.out.println(deserialize.getClass());
}
输出:
{"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"2024-08-09 14:35:22.967","enabled":true,"id":1L}]}
Exception in thread "main" org.springframework.data.redis.serializer.SerializationException: Could not deserialize: java.lang.String cannot be cast to java.lang.Enum; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:71)
at cn.iocoder.yudao.FastJson2Test.main(FastJson2Test.java:25)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at java.util.EnumMap.<init>(EnumMap.java:178)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.lambda$static$0(ObjectReaderImplMap.java:21)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.readObject(ObjectReaderImplMap.java:459)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1624)
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:68)
... 1 more
反序列化 LocalDateTime 当时间为负值
public static void main(String[] args) {
RedisSerializer<Object> serializer = new GenericFastJsonRedisSerializer(new String[]{"cn.", "com.", "java."});
Map<ApplicationType, List<ApplicationDO>> a = new EnumMap<>(ApplicationType.class);
ApplicationDO code = new ApplicationDO();
code.setId(1L);
code.setEnabled(true);
code.setCreateTime(LocalDateTime.MIN); // 这里 <---<----<---------
a.computeIfAbsent(ApplicationType.AI, it -> new ArrayList<>()).add(code);
byte[] serialize = serializer.serialize(a);
System.out.println(new String(serialize));
HashMap<ApplicationType, List<ApplicationDO>> deserialize = (HashMap<ApplicationType, List<ApplicationDO>>) serializer.deserialize(serialize);
System.out.println(deserialize);
System.out.println(deserialize.getClass());
}
输出:
{"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
Exception in thread "main" org.springframework.data.redis.serializer.SerializationException: Could not deserialize: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}; nested exception is com.alibaba.fastjson2.JSONException: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:71)
at cn.iocoder.yudao.FastJson2Test.main(FastJson2Test.java:25)
Caused by: com.alibaba.fastjson2.JSONException: read LocalDateTime error -999999999-01-01 00:00:00, offset 162, character ", line 1, column 162, fastjson-version 2.0.51 {"@type":"java.util.EnumMap","AI":[{"@type":"cn.iocoder.yudao.module.business.dal.dataobject.application.ApplicationDO","createTime":"-999999999-01-01 00:00:00","enabled":true,"id":1L}]}
at com.alibaba.fastjson2.JSONReader.readLocalDateTime(JSONReader.java:1244)
at com.alibaba.fastjson2.reader.ObjectReaderImplLocalDateTime.readObject(ObjectReaderImplLocalDateTime.java:57)
at com.alibaba.fastjson2.reader.ORG_1_14_ApplicationDO.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSONReader.readArray(JSONReader.java:2559)
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:2090)
at com.alibaba.fastjson2.reader.ObjectReaderImplMap.readObject(ObjectReaderImplMap.java:453)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1624)
at com.alibaba.fastjson2.support.spring.data.redis.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:68)
... 1 more
问题描述
简要描述您碰到的问题。 1: 反序列化 EnumMap 2: 当LocalDateTime 为负值时报错
环境信息
请填写以下信息:
重现步骤
如何操作可以重现该问题:
反序列化EnumMap
输出:
反序列化 LocalDateTime 当时间为负值
输出:
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
相关日志输出
请复制并粘贴任何相关的日志输出。
附加信息
如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。