Java 序列化未实现可序列化的类变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18643115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:47:03  来源:igfitidea点击:

Serializing a class variable which does not implement serializable

javaserialization

提问by user2047302

I have a class which implements Serializable. There is an other class object in the class which does not implement serializable. What should be done to serialize the member of the class.

我有一个实现可序列化的类。类中还有一个未实现可序列化的类对象。应该做什么来序列化类的成员。

My class is something like this

我的课是这样的

public class Employee implements Serializable{
    private String name;
    private Address address;
}


public class Address{
    private String street; 
    private String area;   
    private String city;
}

Here, I dont have access to the Address class to make it implement Serializable. Please help. Thanks in advance

在这里,我无权访问 Address 类以使其实现可序列化。请帮忙。提前致谢

回答by Daniel Kaplan

Well of course there's the obvious solution to put Serializableon it. I understand that's not always an option.

嗯,当然有明显的解决方案Serializable。我知道这并不总是一种选择。

Perhaps you can extend the Addressand put Serializableon the child you make. Then you make it so Employeehas a Childfield instead of an Addressfield.

也许您可以延长Address并穿上Serializable您制作的孩子。然后你让它Employee有一个Child字段而不是一个Address字段。

Here are some other things to consider:

以下是一些需要考虑的其他事项:

  • You cankeep the Employee.addressfield as an Addresstype. You can serialize if you call the Employee.setAddress(new SerializableAddress())
  • If Addressis null, you can serialize the whole employee even if Address's type is not serializable.
  • If you mark Addressas transient, it will skip trying to serialize Address. This may solve your problem.
  • 可以将该Employee.address字段保留为一种Address类型。如果您调用Employee.setAddress(new SerializableAddress())
  • 如果Address为空,即使Address的类型不可序列化,您也可以序列化整个员工。
  • 如果您标记Address为瞬态,它将跳过尝试序列化Address. 这可能会解决您的问题。

Then there are other "serialization" frameworks like XStreamthat don't require the marker interface to work. It depends on your requirements whether that's an option though.

然后还有其他“序列化”框架,如XStream,不需要标记接口即可工作。不过,这是否是一种选择取决于您的要求。

回答by Ankur Shanbhag

You caanot directly make this Address class serializable as you do not have access to modify it.

您不能直接使这个 Address 类可序列化,因为您无权修改它。

There are few options :

有几个选项:

  • Create a subclass of Address class and use it. You can mark this class as serializable.
  • Mark the Address as transient.
  • 创建 Address 类的子类并使用它。您可以将此类标记为可序列化。
  • 将地址标记为临时地址。

Please take a look at this stackoverflowlink

请看一下这个stackoverflow链接

回答by Katona

If you have the option to use a 3rd party library for serialization, then you can use for example kryo. This has a default serialization which doesn't require implementing interfaces or annotating fields.

如果您可以选择使用 3rd 方库进行序列化,那么您可以使用例如kryo。这具有不需要实现接口或注释字段的默认序列化。

You can check Which is the best alternative for Java Serialization?for more alternatives.

您可以检查哪个是 Java 序列化的最佳替代方案?更多选择。