fastjson content is empty when deserialize json of guava Multimap type

jv2fixgn  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(288)
  1. when using WriteClassName feature, it outputs a strange classname.
  2. parse json without classname, I get empty content.

Ask for help. Please check what happed.

public class MultimapTest {
    public static void main(String[] args) {
        System.setProperty("fastjson.parser.autoTypeSupport","true");

        Multimap<String, List> mt = ArrayListMultimap.create();
        mt.put("multimap1", Arrays.asList("1","2","3"));
        System.out.println("json string with class name: ");
        String jsonWithClass = JSON.toJSONString(mt, SerializerFeature.WriteClassName);

        System.out.println(jsonWithClass);

        try {
            System.out.println(JSON.parse(jsonWithClass));
        } catch (Throwable e) {
           //com.alibaba.fastjson.JSONException: unsupport type class com.google.common.collect.AbstractMapBasedMultimap$AsMap
            e.printStackTrace();
        }

        Multimap<String, List> stringListMultimap = JSON.parseObject(JSON.toJSONString(mt), new TypeReference<ArrayListMultimap<String, List>>() {
        });

        System.out.println(stringListMultimap);
    }
}

相关问题