fastjson FastJson 反序列化不支持Guava Immutable Class

ohfgkhjo  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(559)

是否考虑支持Immutable的反序列化?

public class FastJsonGuavaTest {

    @Test
    public void immutableMapTest() {
        Object obj = ImmutableMap.of("abc", "abc", "123", "123");
        Class<?> clazz = obj.getClass();

        String jsonData = JSON.toJSONString(obj);
        System.out.println(jsonData);
        //此处抛出异常:om.alibaba.fastjson.JSONException: unsupport type class com.google.common.collect.RegularImmutableMap
        obj = JSON.parseObject(jsonData, clazz);
        System.out.println(JSON.toJSONString(obj));
    }

    @Test
    public void immutableMapTest2() {
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        Object obj = ImmutableMap.of("abc", "abc", "123", "123");
        Class<?> clazz = obj.getClass();

        String jsonData = JSON.toJSONString(obj,
            SerializerFeature.WriteClassName);
        System.out.println(jsonData);
        //抛出异常:om.alibaba.fastjson.JSONException: unsupport type class com.google.common.collect.RegularImmutableMap
        obj = JSON.parseObject(jsonData, clazz);
        System.out.println(JSON.toJSONString(obj));

    }
}

相关问题