Java – serialize a class variable that does not implement serialization

I have a class that implements serializable Class has another class object that does not implement serializability What should I do to serialize the members of this class

My class is 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 didn't access the address class to make it serializable Please help. Thank you in advance

Solution

Of course, there is an obvious solution to put serializable on top I understand that this is not always an option

Maybe you can expand the address and put serializable on your child Then you create it so that employee has a child field instead of an address field

Here are some considerations:

>You can add employee The address field is left as the address type If you call employee Setaddress (New serializableaddress()), serializable > if address is null, the entire employee can be serialized even if the address type is not serializable. > If the address is marked transient, attempts to serialize the address are skipped This will solve your problem

Then there are other "serialization" frameworks, such as xStream, that do not require a markup interface to work It depends on whether your request is an option

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>