如何使用接口序列化类? [英] How to serialize a class with an interface?

查看:18
本文介绍了如何使用接口序列化类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有做过很多序列化,但我正在尝试使用 Google 的 gson将 Java 对象序列化为文件.这是我的问题的示例:

I have never done much with serialization, but am trying to use Google's gson to serialize a Java object to a file. Here is an example of my issue:

public interface Animal {
    public String getName();
}


 public class Cat implements Animal {

    private String mName = "Cat";
    private String mHabbit = "Playing with yarn";

    public String getName() {
        return mName;
    }

    public void setName(String pName) {
        mName = pName;
    }

    public String getHabbit() {
        return mHabbit;
    }

    public void setHabbit(String pHabbit) {
        mHabbit = pHabbit;
    }

}

public class Exhibit {

    private String mDescription;
    private Animal mAnimal;

    public Exhibit() {
        mDescription = "This is a public exhibit.";
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String pDescription) {
        mDescription = pDescription;
    }

    public Animal getAnimal() {
        return mAnimal;
    }

    public void setAnimal(Animal pAnimal) {
        mAnimal = pAnimal;
    }

}

public class GsonTest {

public static void main(String[] argv) {
    Exhibit exhibit = new Exhibit();
    exhibit.setAnimal(new Cat());
    Gson gson = new Gson();
    String jsonString = gson.toJson(exhibit);
    System.out.println(jsonString);
    Exhibit deserializedExhibit = gson.fromJson(jsonString, Exhibit.class);
    System.out.println(deserializedExhibit);
}
}

所以这很好地序列化 - 但可以理解的是删除了 Animal 的类型信息:

So this serializes nicely -- but understandably drops the type information on the Animal:

{"mDescription":"This is a public exhibit.","mAnimal":{"mName":"Cat","mHabbit":"Playing with yarn"}}

这会导致反序列化的真正问题:

This causes real problems for deserialization, though:

Exception in thread "main" java.lang.RuntimeException: No-args constructor for interface com.atg.lp.gson.Animal does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

我明白为什么会发生这种情况,但我无法找出处理此问题的正确模式.我确实查看了指南,但它没有直接解决这个问题.

I get why this is happening, but am having trouble figuring out the proper pattern for dealing with this. I did look in the guide but it didn't address this directly.

推荐答案

把动物设为transient,它就不会被序列化.

Put the animal as transient, it will then not be serialized.

或者您可以通过实现 defaultWriteObject(...)defaultReadObject(...) 来自己序列化它(我认为这就是它们的名字...)

Or you can serialize it yourself by implementing defaultWriteObject(...) and defaultReadObject(...) (I think thats what they were called...)

编辑请参阅关于编写实例创建者"的部分此处.

EDIT See the part about "Writing an Instance Creator" here.

Gson 无法反序列化接口,因为它不知道将使用哪个实现类,因此您需要为您的 Animal 提供一个实例创建者并设置默认值或类似值.

Gson cant deserialize an interface since it doesnt know which implementing class will be used, so you need to provide an instance creator for your Animal and set a default or similar.

这篇关于如何使用接口序列化类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆