Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG]序列化Set<Date>类型的数据异常,导致解析报错 #2475

Closed
ybbwk opened this issue Apr 23, 2024 · 3 comments
Closed

[BUG]序列化Set<Date>类型的数据异常,导致解析报错 #2475

ybbwk opened this issue Apr 23, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@ybbwk
Copy link

ybbwk commented Apr 23, 2024

问题描述

使用GenericFastJsonRedisSerializer或者JSON.toJSONString方法序列化到Redis,对象中存在Set类型的字段,序列化到Redis的数据为“Set[new Date(xxxxxxxxxxxxxxx)]”,当从Redis反序列化到对象的时候,报错“string length only support string input”。将Set类型改为List则能正确的序列化并可正确反序列化

环境信息

  • OS信息: Windows 11、Ubuntu
  • JDK信息: Openjdk 17
  • 版本信息:Fastjson2 2.0.49
@ybbwk ybbwk added the bug Something isn't working label Apr 23, 2024
@wenshao wenshao added this to the 2.0.50 milestone Apr 23, 2024
hnyyghk pushed a commit to hnyyghk/fastjson2 that referenced this issue Apr 24, 2024
hnyyghk added a commit to hnyyghk/fastjson2 that referenced this issue Apr 24, 2024
@wenshao
Copy link
Member

wenshao commented Apr 28, 2024

@wenshao wenshao added the fixed label Apr 28, 2024
@ybbwk
Copy link
Author

ybbwk commented Apr 28, 2024

使用2.0.50-SNAPSHOT可正常解析,不报错。序列化后的JSON字符串没有变化,List和Set的解析结果不一致(Set带类名,List不带类名),不知是否有意为之。

附测试代码

public static void testFastJson2SetDate() {
    FastJsonConfig config = new FastJsonConfig();
    config.setReaderFeatures(JSONReader.Feature.SupportAutoType);
    config.setWriterFeatures(JSONWriter.Feature.WriteClassName);

    Set<Date> dateSet = new HashSet<>();
    dateSet.add(new Date());

    List<Date> dateList = new ArrayList<>();
    dateList.add(new Date());

    TestDto testDto = new TestDto();
    testDto.setId(UUID.fastUUID().toString());
    testDto.setCollSetDate(dateSet);
    testDto.setCollListDate(dateList);
    System.out.println("TestDto: " + testDto);

    String jsonString = JSON.toJSONString(testDto, config.getWriterFeatures());
    System.out.println("JsonString: " + jsonString);

    Object json = JSON.parseObject(jsonString, Object.class, config.getReaderFeatures());
    System.out.println("Json: " + json);
}

@Data
static class TestDto {
    private String id;

    private Set<Date> collSetDate;

    private List<Date> collListDate;
}

输出结果
`
TestDto: FastJson2Test.TestDto(id=5466fe74-8cd9-4f58-86e6-f98e8f93df3e, collSetDate=[Sun Apr 28 09:14:12 CST 2024], collListDate=[Sun Apr 28 09:14:12 CST 2024])

JsonString: {"@type":"FastJson2Test$TestDto","collListDate":["2024-04-28 09:14:12.768"],"collSetDate":Set[new Date(1714266852768)],"id":"5466fe74-8cd9-4f58-86e6-f98e8f93df3e"}

Json: FastJson2Test.TestDto(id=5466fe74-8cd9-4f58-86e6-f98e8f93df3e, collSetDate=[Sun Apr 28 09:14:12 CST 2024], collListDate=[Sun Apr 28 09:14:12 CST 2024])
`

@wenshao
Copy link
Member

wenshao commented May 12, 2024

Set带类名设计就是这样的

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

@wenshao wenshao closed this as completed May 12, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants