[alibaba/fastjson]老问题,field的key(@type)的顺序必须在第一位,否则序列化反序列化没有数据

2025-10-28 298 views
7
406 291 1941

但是这几个都被关闭了。

wenshao commented on 13 Apr 2016
bug fixed. included in 1.2.9 & 1.1.49.android

一个可执行的代码如下:

public class App {
    private static String s1 = "{\"a\":\"b\",\"@type\":\"java.util.HashMap\"}";
    private static String s2 = "{\"@type\":\"java.util.HashMap\",\"a\":\"b\"}";

    public static void main(String[] args) {
        s1();
        s2();
    }

    public static void s1() {
        System.out.println(JSON.toJSONString(JSON.parse(s1)));
    }

    public static void s2() {
        System.out.println(JSON.toJSONString(JSON.parse(s2)));
    }
}

期待结果是

{"a":"b"}
{"a":"b"}

实际结果是

{}
{"a":"b"}

版本:1.2.58,maven目前的最新版。

回答

3

这个问题在最新版本的1.2.67依然存在

0

1.2.75版本依然存在

4

1.2.73同样存在着