Skip to content

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

@Cooperzzy

Description

@Cooperzzy

问题描述

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions