how to expose class names when serializing with Gson

后端 未结 3 1026
夕颜
夕颜 2021-01-26 02:44

My scenario is very complicated but here\'s a summary:

I\'m trying to understand source of a compiler -- and to understand what each AST node represents, I\'m generating

3条回答
  •  后悔当初
    2021-01-26 03:20

    Check runtimeAdapter ,

     RuntimeTypeAdapter nodeAdapter = RuntimeTypeAdapter.create(Node.class,"type");
     nodeAdapter.registerSubtype(Begin.class,"Begin");
     nodeAdapter.registerSubtype(State.class,"State");
     gsonBuilder.registerTypeHierarchyAdapter(Class.class, new GsonClassAdapter())
                .registerTypeAdapter(Node.class,nodeAdapter )
                .enableComplexMapKeySerialization().setPrettyPrinting();
     gson = gsonBuilder.create();
    

提交回复
热议问题