fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

$ref 引用类型丢失问题,存在于所有版本

Open LY1806620741 opened this issue 2 years ago • 0 comments

//测试类
package com.alibaba.fastjson.Test;

public class Test {
    public HashMap<String,Object> fields;
}

@Test
public void testBug(){
    //序列化
    com.alibaba.fastjson.Test test = new com.alibaba.fastjson.Test();
    test.fields.put("child",test);
    String out = JSONObject.toJSONString(test,new SerializeConfig(true), SerializerFeature.WriteClassName);
    System.out.println(out);

    com.alibaba.fastjson.Test o = JSONObject.parseObject(out,com.alibaba.fastjson.Test.class, new ParserConfig(true)); //输出 {"@type":"com.alibaba.fastjson.Test","fields":{"@type":"java.util.HashMap","child":{"$ref":".."}}}

    Assert.assertTrue(o instanceof com.alibaba.fastjson.Test);
    Assert.assertTrue(o.fields.get("child") instanceof com.alibaba.fastjson.Test); //失败,实际是个hashmap,应是引用com.alibaba.fastjson.Test才对
}

LY1806620741 avatar Jul 21 '22 08:07 LY1806620741