Skip to content

[BUG] Deserialization of EnumMap with WriteClassName and autotype #2583

@Cooperzzy

Description

@Cooperzzy

问题描述

EnumMap在配置WriteClassName和SupportAutoType后,无法使用JSON.parseObject反序列化。使用JSONB.parseObject以及在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.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.alibaba.fastjson.parser.Feature.SupportAutoType;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1679Mutated_763 {

    @Test
    public void testAll() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSONB.toBytes(bean);
        Bean bean1 = (Bean) JSONB.parseObject(bytes, Bean.class);
        assertEquals(0, bean1.enumMap.size());

        String json = JSON.toJSONString(bean);
        Bean bean2 = (Bean) JSON.parseObject(json, Bean.class);
        assertEquals(0, bean2.enumMap.size());

        byte[] bytes2 = JSON.toJSONString(bean).getBytes(StandardCharsets.UTF_8);
        Bean bean3 = (Bean) JSON.parseObject(bytes2, Bean.class);
        assertEquals(0, bean3.enumMap.size());

        String json2 = com.alibaba.fastjson.JSON.toJSONString(bean);
        Bean bean4 = (Bean) com.alibaba.fastjson.JSON.parseObject(json2, Bean.class);
        assertEquals(0, bean4.enumMap.size());
    }

    @Test
    public void test_jsonb() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSONB.toBytes(bean, JSONWriter.Feature.WriteClassName);
        Bean bean1 = (Bean) JSONB.parseObject(bytes, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        String json = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName);
        Bean bean1 = (Bean) JSON.parseObject(json, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json_getBytes() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = JSON.toJSONString(bean, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        Bean bean1 = (Bean) JSON.parseObject(bytes, Bean.class, JSONReader.Feature.SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }


    @Test
    public void test_json_fj() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        String json = com.alibaba.fastjson.JSON.toJSONString(bean, SerializerFeature.WriteClassName);
        Bean bean1 = (Bean) com.alibaba.fastjson.JSON.parseObject(json, Bean.class, SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }

    @Test
    public void test_json_getBytes_fj() {
        Bean bean = new Bean();
        bean.enumMap = new EnumMap(TimeUnit.class);

        byte[] bytes = com.alibaba.fastjson.JSON.toJSONString(bean, SerializerFeature.WriteClassName).getBytes(StandardCharsets.UTF_8);
        Bean bean1 = (Bean) com.alibaba.fastjson.JSON.parseObject(bytes, Bean.class, SupportAutoType);
        assertEquals(0, bean1.enumMap.size());
    }



    public static class Bean {
        public Map<TimeUnit, Object> enumMap;
    }
}

期待的正确结果

能够反序列化

相关日志输出

java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "name" is null
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:379)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.autoType(ObjectReaderAdapter.java:247)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1577)
at Issue1679Mutated_763.test_json_getBytes(Issue1679Mutated_763.java:69)

java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "name" is null
at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:379)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.autoType(ObjectReaderAdapter.java:247)
at com.alibaba.fastjson2.reader.ORG_1_1_Bean.readObject(Unknown Source)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1062)
at Issue1679Mutated_763.test_json(Issue1679Mutated_763.java:59)

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