fastjson bug when format Object[]

omvjsjqw  于 2021-11-27  发布在  Java
关注(0)|答案(5)|浏览(271)
Cpu cpu = new Cpu("intel", 3.3);
        Screen screen = new Screen(16, 9, "samsung");
        Student student = new Student();
        Computer computer = student.assembling(cpu,screen);
        cpu.setName("intell");

        Object[] objectArray = new Object[4];
        objectArray[0] = cpu;
        objectArray[1] = screen;
        objectArray[2] = "2";
        objectArray[3] = "3";
        List<Object> list1 = new ArrayList<>();
        list1.add(objectArray);
        list1.add(computer);
        String s = JSON.toJSONString(list1);
        System.out.println(s);

the output is [[{"name":"intell","speed":3.3},{"length":16,"name":"samsung","width":9},"2","3"],{"cpu":{"$ref":"$[0].null"},"screen":{"$ref":"$[0].null"}}]
I don't think the ".null" is the expected output,especially when i use alibaba/arthas debugging.it makes me uncomfortable.
you can find the logic in ObjectArrayCodec.write which result in the issue
@wenshao

qxsslcnc

qxsslcnc1#

please tell me which version

slsn1g29

slsn1g292#

the version is
1.2.53
by the way,the assertEquals of Issue2132 may be like "cpu":{"$ref":"$[0].[0]"}(now the output is "cpu":{"$ref":"$[0].null")

zdwk9cvp

zdwk9cvp3#

the class Computer in your Issue2132 test case don't have getters and setters @wenshao
with getters and setters,the test failed.the result is:
junit.framework.ComparisonFailure:
Expected :[[{"name":"intell","speed":3.3},{"height":9,"name":"samsung","width":16},"2","3"],{}]
Actual :[[{"name":"intell","speed":3.3},{"height":9,"name":"samsung","width":16},"2","3"],{"cpu":{"$ref":"$[0].null"},"screen":{"$ref":"$[0].null"}}]

nnt7mjpx

nnt7mjpx4#

when i use
String s = JSON.toJSONString(arr, SerializerFeature.WriteClassName, SerializerFeature.PrettyFormat);
There is still the problem

7kjnsjlb

7kjnsjlb5#

i use the newest version :1.2.60

相关问题