栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java gson的多态性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java gson的多态性

根据我的研究以及使用gson-2.0时,你确实不想使用registerTypeHierarchyAdapter方法,而是更平凡的registerTypeAdapter。而且,你当然不需要为派生类做instanceofs或编写适配器:只为基类或接口提供一个适配器,当然,你对派生类的默认序列化感到满意。无论如何,这是代码(删除了打包和导入)(也可以在github中找到):

基类(在我的情况下为接口):

public interface IAnimal { public String sound(); }

这两个派生类Cat:

public class Cat implements IAnimal {    public String name;    public Cat(String name) {        super();        this.name = name;    }    @Override    public String sound() {        return name + " : "meaow"";    };}

And Dog:

public class Dog implements IAnimal {    public String name;    public int ferocity;    public Dog(String name, int ferocity) {        super();        this.name = name;        this.ferocity = ferocity;    }    @Override    public String sound() {        return name + " : "bark" (ferocity level:" + ferocity + ")";    }}

IAnimalAdapter:

public class IAnimalAdapter implements JsonSerializer<IAnimal>, JsonDeserializer<IAnimal>{    private static final String CLASSNAME = "CLASSNAME";    private static final String INSTANCE  = "INSTANCE";    @Override    public JsonElement serialize(IAnimal src, Type typeOfSrc, JsonSerializationContext context) {        JsonObject retValue = new JsonObject();        String className = src.getClass().getName();        retValue.addProperty(CLASSNAME, className);        JsonElement elem = context.serialize(src);         retValue.add(INSTANCE, elem);        return retValue;    }    @Override    public IAnimal deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException  {        JsonObject jsonObject = json.getAsJsonObject();        JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);        String className = prim.getAsString();        Class<?> klass = null;        try { klass = Class.forName(className);        } catch (ClassNotFoundException e) { e.printStackTrace(); throw new JsonParseException(e.getMessage());        }        return context.deserialize(jsonObject.get(INSTANCE), klass);    }}

和测试类:

public class Test {    public static void main(String[] args) {        IAnimal animals[] = new IAnimal[]{new Cat("Kitty"), new Dog("Brutus", 5)};        Gson gsonExt = null;        { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(IAnimal.class, new IAnimalAdapter()); gsonExt = builder.create();        }        for (IAnimal animal : animals) { String animalJson = gsonExt.toJson(animal, IAnimal.class); System.out.println("serialized with the custom serializer:" + animalJson); IAnimal animal2 = gsonExt.fromJson(animalJson, IAnimal.class); System.out.println(animal2.sound());        }    }}

当运行Test :: main时,将得到以下输出:

serialized with the custom serializer:{"CLASSNAME":"com.synelixis.caches.viz.json.playground.plainAdapter.Cat","INSTANCE":{"name":"Kitty"}}Kitty : "meaow"serialized with the custom serializer:{"CLASSNAME":"com.synelixis.caches.viz.json.playground.plainAdapter.Dog","INSTANCE":{"name":"Brutus","ferocity":5}}Brutus : "bark" (ferocity level:5)

我实际上也使用registerTypeHierarchyAdapter方法完成了上述操作,但这似乎需要实现自定义的DogAdapter和CatAdapter序列化器/反序列化器类,这在你想向Dog或Cat添加另一个字段时很难维护。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号