是自定义枚举序列化吗? [英] Is custom enum Serializable too?

查看:117
本文介绍了是自定义枚举序列化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解枚举是序列化。因此,它是安全的话。 (selectedCountry是枚举国家

I understand Enum is Serializable. Hence, it is safe to do so. (selectedCountry is enum Country)

public enum Country {
    Australia,
    Austria,
    UnitedState;
}

片段

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        selectedCountry = (Country)savedInstanceState.getSerializable(SELECTED_COUNTRY_KEY);
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putSerializable(SELECTED_COUNTRY_KEY, selectedCountry);
}

但是,如果我有自定义枚举类不可序列化的成员?例如,

However, what if I have non-serializable members in custom enum class? For instance,

package org.yccheok;

import org.yccheok.R;

/**
 *
 * @author yccheok
 */
public enum Country {
    Australia(R.drawable.flag_au),
    Austria(R.drawable.flag_at),
    UnitedState(R.drawable.flag_us);

    Country(int icon) {
        this.icon = icon;
        nonSerializableClass = new NonSerializableClass(this.toString());
    }

    public int getIcon() {
        return icon;
    }

    public static class NonSerializableClass {
        public NonSerializableClass(String dummy) { this.dummy = dummy; }
        public String dummy;
    }

    private final int icon;

    public NonSerializableClass nonSerializableClass;
}

我测试。有用。 (我测试打印出的成员变量的所有前值和序列化之后,它们是相同的前,后)

I tested. It works. (I tested by printing out all the value of member variables before and after serialization. They are same before and after)

但是,我不明白为什么它的工作原理?由于我不提供适当的的readObject 的writeObject ,所要求的序列化接口。

However, I do not understand why it works? As I do not provide proper readObject and writeObject, as required by Serializable interface.

正如在有效的Java的项目75:考虑使用自定义序列化形式的,我是否需要提供自己的的readObject 的writeObject ,如果我在我的枚举有自定义的成员变量?

As pointed in Effective Java Item 75: Consider using a custom serialized form, do I need to provide my own readObject and writeObject, if I have custom member variables in my enum?

推荐答案

它的工作原理的原因是序列化进程枚举的是序列化过程中的其他类不同。从<一个href="http://docs.oracle.com/javase/1.5.0/docs/guide/serialization/spec/serial-arch.html#enum">official文档:

The reason it works is that serialization process for Enum's is different from serialization process for other classes. From the official documentation:

1.12序列化枚举常量的

枚举常量的序列化不同于普通的序列化或外部化的对象。枚举常量的序列化形式只包含​​其名称;常量字段值不是present形式。序列化一个枚举常量,ObjectOutputStream中写道由枚举常量的名称方法返回的值。反序列化的枚举常量,ObjectInputStream中读取数据流的常量的名称;反序列化的常量然后通过调用java.lang.Enum.valueOf方法,并传递常量的枚举类型以及所接收到的常量名称作为参数获得。像其他序列化或外部化的对象,enum常量可以作为的序列化流中随后出现的反向引用的目标。

Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form. To serialize an enum constant, ObjectOutputStream writes the value returned by the enum constant's name method. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the java.lang.Enum.valueOf method, passing the constant's enum type along with the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream.

这意味着,所有的自定义字段的不会进行序列化。在你的情况一切正常,因为你的应用程序仍在运行,而您所得到的同一个 枚举你传递给 savedInstanceState.putSerializable 。

That means, all your custom fields won't be serialized. In your case everything works well because your application process is still running and you are getting the same Enum instance that you passed to savedInstanceState.putSerializable.

不过,设想一种情况,你的应用程序被打死,因为安卓没有足够的内存。下一次用户打开应用程序,你会得到一个 枚举实例,有关自定义字段中的所有信息都将丢失(但构造函数被调用)。

But imagine a situation where your app get killed because Android has no enough memory. The next time user opens the app you will get a new Enum instance and all information about custom fields will be lost (but the constructor will be called).

这篇关于是自定义枚举序列化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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