How to serialize a class with an interface?

前端 未结 4 1027
南方客
南方客 2020-11-29 23:18

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 i         


        
4条回答
  •  囚心锁ツ
    2020-11-29 23:52

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

    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 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.

提交回复
热议问题