fastjson2
fastjson2 copied to clipboard
[BUG] 反序列化异常, EnumMap 以及 为负值的LocalDateTime 反序列化异常
问题描述
简要描述您碰到的问题。 1: 反序列化 EnumMap 2: 当LocalDateTime 为负值时报错
环境信息
请填写以下信息:
- OS信息: Darwin Mac-mini.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:09:52 PDT 2024; root:xnu-10063.121.3~5/RELEASE_X86_64 x86_64
- JDK信息:
openjdk version "1.8.0_372"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_372-b07)
OpenJDK 64-Bit Server VM (Temurin)(build 25.372-b07, mixed mode)
- 版本信息:
Maven: com.alibaba.fastjson2:fastjson2:2.0.51
Maven: com.alibaba.fastjson2:fastjson2-extension:2.0.51
Maven: com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.51
重现步骤
如何操作可以重现该问题:
- 反序列化EnumMap
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
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
相关日志输出
请复制并粘贴任何相关的日志输出。
附加信息
如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.53-SNAPSHOT/ 问题已修复,帮忙用2.0.53-SNAPSHOT帮忙验证,2.0.53版本预计在月底发布