在GSON中反序列化递归多态类

编程入门 行业动态 更新时间:2024-10-23 22:29:09
本文介绍了在GSON中反序列化递归多态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

类Complex实现递归{ Map< String,Recursive>地图; ... } 类简单实现递归{...}

如何反序列化此json:

{type:complex ,map:{a:{type:简单},b:{ type:complex,map:{ba:{type:simple} } } }

使用Google GSON?

当然,可以改进管理临界情况(例如,如果没有类型字段?)。

package stackoverflow.questions; import java.lang.reflect.Type; import java.util。*; 导入stackoverflow.questions.Q20254329。*; import com.google.gson。*; import com.google.gson.reflect.TypeToken; 公共类Q20327670 { 静态类Complex实现递归{ Map< String,Recursive>地图; @Override public String toString(){ returnComplex [map =+ map +]; $ b静态类简单实现递归{ @Override public String toString(){返回Simple []; } } public static class RecursiveDeserializer实现JsonDeserializer<递归> { public递归反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext上下文)抛出JsonParseException {递归r = null; if(json == null) r = null; else { JsonElement t = json.getAsJsonObject()。get(type); String type = null; if(t!= null){ type = t.getAsString(); switch(type){ casecomplex:{ Complex c = new Complex(); JsonElement e = json.getAsJsonObject()。get(map); if(e!= null){ Type mapType = new TypeToken< Map< String,Recursive>>(){} .getType(); c.map = context.deserialize(e,mapType); } r = c; 休息; } casesimple:{ r = new Simple(); 休息; } //记得管理默认.. } } } return r; $ b public static void main(String [] args){ String json ={+ \\ \\complex \:\complex \,+ \map \:{+ \a\:{+ \type \:\simple \+ },+ \b\:{+ \type \:\complex \,+ \map \:{+ \ba \:{ + \type \:\\ simple\ + } + } + } + }}; GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(Recursive.class,new RecursiveDeserializer()); Gson gson = gb.create(); 递归r = gson.fromJson(json,Recursive.class); System.out.println(r); } }

这是我的代码结果如下: $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ []}]}]

class Complex implements Recursive { Map<String, Recursive> map; ... } class Simple implements Recursive { ... }

How do I deserialize this json:

{ "type" : "complex", "map" : { "a" : { "type" : "simple" }, "b" : { "type" : "complex", "map" : { "ba" : { "type" : "simple" } } } }

using Google GSON?

解决方案

To deserialize your JSON you need a custom deserializer for your Recursive interface. In that kind of class you need to examine your JSON and decide what kind of class to instantiate as the type field in the JSON itself. Here you have a basic deserializer I wrote for you example.

Of course it can be improve to manage borderline cases (for example, what happens if you do not have type field?).

package stackoverflow.questions; import java.lang.reflect.Type; import java.util.*; import stackoverflow.questions.Q20254329.*; import com.google.gson.*; import com.google.gson.reflect.TypeToken; public class Q20327670 { static class Complex implements Recursive { Map<String, Recursive> map; @Override public String toString() { return "Complex [map=" + map + "]"; } } static class Simple implements Recursive { @Override public String toString() { return "Simple []"; } } public static class RecursiveDeserializer implements JsonDeserializer<Recursive> { public Recursive deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Recursive r = null; if (json == null) r = null; else { JsonElement t = json.getAsJsonObject().get("type"); String type = null; if (t != null) { type = t.getAsString(); switch (type) { case "complex": { Complex c = new Complex(); JsonElement e = json.getAsJsonObject().get("map"); if (e != null) { Type mapType = new TypeToken<Map<String, Recursive>>() {}.getType(); c.map = context.deserialize(e, mapType); } r = c; break; } case "simple": { r = new Simple(); break; } // remember to manage default.. } } } return r; } } public static void main(String[] args) { String json = " { " + " \"type\" : \"complex\", " + " \"map\" : { " + " \"a\" : { " + " \"type\" : \"simple\" " + " }, " + " \"b\" : { " + " \"type\" : \"complex\", " + " \"map\" : { " + " \"ba\" : { " + " \"type\" : \"simple\" " + " } " + " } " + " } " + " } } "; GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(Recursive.class, new RecursiveDeserializer()); Gson gson = gb.create(); Recursive r = gson.fromJson(json, Recursive.class); System.out.println(r); } }

This is the result of my code:

Complex [map={a=Simple [], b=Complex [map={ba=Simple []}]}]

更多推荐

在GSON中反序列化递归多态类

本文发布于:2023-08-04 05:59:39,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   序列化   多态   GSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!