Is custom enum Serializable too?

前端 未结 3 833
北海茫月
北海茫月 2020-11-27 03:32

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

Original enum without customer member var

相关标签:
3条回答
  • 2020-11-27 03:56

    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 Serialization of Enum Constants

    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.

    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 custom fields will have been lost and reinitialized by the constructor. Thus, mutable fields in an enum are always effectively transient.

    0 讨论(0)
  • 2020-11-27 04:02

    The serialization of enum members is not working. The nonSerializable field is never serialized as @vmironov answered. Here is a test:

    public enum Country {
    Australia;
    
        public static class NonSerializableClass {
           public NonSerializableClass() {}
           public String dummy;
        }
    
        public NonSerializableClass nonSerializableClass;
    }
    

    The code writing the enum to the serialization stream:

    public class SerializationTestWrite {
        public static void main(String[] args) throws Exception{
            FileOutputStream f = new FileOutputStream("tmp");
            ObjectOutput s = new ObjectOutputStream(f);
    
            Country.Australia.nonSerializableClass = new Country.NonSerializableClass();
            Country.Australia.nonSerializableClass.dummy = "abc";
    
            s.writeObject(Country.Australia);
            s.flush();
    
            System.out.println(Country.Australia.nonSerializableClass.dummy);
        }
    }    
    

    On writing the value of the dummy field is: abc

    The code reading the enum from the serialization stream:

    public class SerializationTestRead {
        public static void main(String[] args) throws Exception{
            FileInputStream in = new FileInputStream("tmp");
            ObjectInputStream so = new ObjectInputStream(in);
            Country readed = (Country) so.readObject();
    
            System.out.println(readed.nonSerializableClass);
        }
    }
    

    But on reading, the value of the field nonSerializableClass is: null

    0 讨论(0)
  • 2020-11-27 04:08

    As per Serializable documentation, readObject and writeObject are not needed at all, so your question might be not fully correct.

    Serializable is a marker interface and doesn't have any methods.

    I refer you to this answer which provides additional details about the Serialization implementation (which explains why you don't necessary need write and read functions).

    And, as mentioned here by Dianne Hackborn, Parcelable is much more efficient for Android.

    If you're particularly interested in Enum, refer to below paragraph:

    1.12 Serialization of Enum Constants

    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.

    The process by which enum constants are serialized cannot be customized: any class-specific writeObject, readObject, readObjectNoData, writeReplace, and readResolve methods defined by enum types are ignored during serialization and deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L. Documenting serializable fields and data for enum types is unnecessary, since there is no variation in the type of data sent.

    So, I don't think that the Enum is the right choice to test internal non-serializable classes work.

    0 讨论(0)
提交回复
热议问题