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] 使用WriteClassName序列化,然后再反序列化时@type信息没有去除 #2623

Closed
elitewe opened this issue May 23, 2024 · 6 comments
Assignees
Labels
bug Something isn't working fixed question Further information is requested
Milestone

Comments

@elitewe
Copy link

elitewe commented May 23, 2024

问题描述

使用序列化方法JSON.toJSONString(dataMap, SerializerFeature.WriteClassName),反序列话后的对象中包含@type信息,理想情况下应该不会出现@type(fastjson1反序列化后没有@type)

环境信息

  • OS信息: Windows
  • JDK信息: JDK17
  • 版本信息:Fastjson2 2.0.49

重现步骤

        Map<String, Object> dataMap = new HashMap<>(2);
        dataMap.put("aInteger", 1);
        dataMap.put("aLong", 2L);
        dataMap.put("aFloat", 3F);
        String jsonStr = JSON.toJSONString(dataMap, SerializerFeature.WriteClassName);
        System.out.println(jsonStr);

        System.out.println("--- fastjson -----");
        Map<String, Object> fastMap = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        printMap(fastMap);

期待的正确结果

期望的输出结果(使用fastjson1.2.68)
image

相关日志输出

image

@elitewe elitewe added the bug Something isn't working label May 23, 2024
@hnyyghk
Copy link
Contributor

hnyyghk commented May 24, 2024

可以通过开启SupportAutoType避免反序列化后携带@type字段

Map<String, Object> fastMap = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {}, JSONReader.Feature.SupportAutoType);

@elitewe
Copy link
Author

elitewe commented May 24, 2024

可以通过开启SupportAutoType避免反序列化后携带@type字段

Map<String, Object> fastMap = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {}, JSONReader.Feature.SupportAutoType);

好的谢谢,为什么fastjson1.2.68版本不用主动开启SupportAutoType就可以过滤@type字段?是fastjson1和fastjson2的默认策略不一样吗

@wenshao
Copy link
Member

wenshao commented May 25, 2024

2.0.x如果不配置SupportAutoType,就不会做任何相关处理,这个是为了安全考虑。

@wenshao wenshao added question Further information is requested and removed bug Something isn't working labels May 25, 2024
@elitewe
Copy link
Author

elitewe commented May 27, 2024

2.0.x如果不配置SupportAutoType,就不会做任何相关处理,这个是为了安全考虑。

好的,请问开启SupportAutoType这个功能是在哪一个版本添加上去的,我使用fastjson2.0.13版本加上SupportAutoType后@type仍然没有去除

@elitewe
Copy link
Author

elitewe commented May 27, 2024

2.0.x如果不配置SupportAutoType,就不会做任何相关处理,这个是为了安全考虑。

另外我发现一个问题,如果序列化对象是java.sql.Date时,使用上述接口会报错

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

public class demo21 {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("a", 1L);
        map.put("b", 2);
        map.put("c", 3f);
        map.put("d", new Date(System.currentTimeMillis()));
        
        String s = JSON.toJSONString(map, JSONWriter.Feature.WriteClassName);
        System.out.println(s);
        Map<String, Object> map2 = JSON.parseObject(s, new TypeReference<Map<String, Object>>(){}, JSONReader.Feature.SupportAutoType);
        System.out.println(map2);
    }
}

报错日志如下
Exception in thread "main" com.alibaba.fastjson2.JSONException: read LocalDateTime error val, offset 86, character :, line 1, column 86, fastjson-version 2.0.49 {"@type":"java.util.HashMap","a":1L,"b":2,"c":3.0F,"d":{"@type":"java.sql.Date","val":1716789581756}}
at com.alibaba.fastjson2.JSONReader.readLocalDateTime(JSONReader.java:1226)
at com.alibaba.fastjson2.util.JdbcSupport$DateReader.readObject(JdbcSupport.java:669)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:397)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:991)
at demo21.main(demo21.java:24)

@wenshao wenshao added this to the 2.0.51 milestone May 31, 2024
@wenshao wenshao added bug Something isn't working fixed labels May 31, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.51
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jun 1, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants