Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[BUG] Deserialization includes additional @type information after WriteClassName #2581

Closed
Cooper-Zhong opened this issue May 16, 2024 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Cooper-Zhong
Copy link

问题描述

Map序列化配置WriteClassName后,反序列化会多出类型信息的entry(配置SupportAutoType也是),在fastjson 1.2.83上没有这个问题

环境信息

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson2 2.0.50]

重现步骤

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue336Mutated_758 {

    @Test
    public void testEmpty() {
        HashMap<String, String> map = new HashMap<>();
        map.put("date", "2022");
        String jsonString = JSON.toJSONString(map, JSONWriter.Feature.WriteClassName);
//        HashMap result = JSON.parseObject(jsonString, HashMap.class, JSONReader.Feature.SupportAutoType);
        HashMap result = JSON.parseObject(jsonString, HashMap.class);
        assertEquals(1, result.size());
    }

    @Test
    public void testEmpty1() {
        HashMap<String, String> map = new HashMap<>();
        map.put("date", "2022");
        String jsonString = com.alibaba.fastjson.JSON.toJSONString(map, SerializerFeature.WriteClassName);
        HashMap result = com.alibaba.fastjson.JSON.parseObject(jsonString, HashMap.class);
        assertEquals(1, result.size());
    }
}

期待的正确结果

不含有@type -> java.util.HashMap

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 16, 2024
@wenshao
Copy link
Member

wenshao commented May 17, 2024

WriteClassName在parse时应该和SupportAutoType配对使用,这个不是BUG

@wenshao wenshao added the wontfix This will not be worked on label May 17, 2024
@Cooper-Zhong
Copy link
Author

public void test() {
        HashMap<String, String> map = new HashMap<>();
        map.put("date", "2022");
        String jsonString = JSON.toJSONString(map, JSONWriter.Feature.WriteClassName);
        HashMap result = JSON.parseObject(jsonString, HashMap.class, JSONReader.Feature.SupportAutoType);
        System.out.println(JSON.toJSONString(result));
        // {"date":"2022","@type":"java.util.HashMap"}
        assertEquals(1, result.size());
    }

是配对使用的,但在parse后的map entry里面依然保留着类型信息@type -> java.util.HashMap

@wenshao wenshao removed the wontfix This will not be worked on label May 17, 2024
@wenshao
Copy link
Member

wenshao commented May 17, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证

@wenshao wenshao added this to the 2.0.51 milestone May 17, 2024
@wenshao wenshao added the fixed label May 17, 2024
@Cooper-Zhong
Copy link
Author

验证已修复,辛苦温少

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants