Serialising Enums

R

Roedy Green

It just hit me that there must be some special kludge to handle
serialising the values of enums.

I have not checked if enums are indeed serialisable. If they are, when
you reconstitute, you want to get the value reconstituted to point to
the enum constant object in the target machine, not a pointer to a
reconstructed clone of the enum constant object from the source
machine.

Perhaps they are serialised as ordinals. Perhaps they are serialised
as string enum constant names. (That would allow you to add new enum
constants and still read old serialised files).

Has anyone ever experimented to figure out how it works or if it works
at all?
 
J

John B. Matthews

Roedy Green said:
It just hit me that there must be some special kludge to handle
serialising the values of enums.

I have not checked if enums are indeed serialisable. If they are,
when you reconstitute, you want to get the value reconstituted to
point to the enum constant object in the target machine, not a
pointer to a reconstructed clone of the enum constant object from the
source machine.

Perhaps they are serialised as ordinals. Perhaps they are serialised
as string enum constant names. (That would allow you to add new enum
constants and still read old serialised files).

Has anyone ever experimented to figure out how it works or if it
works at all?

I haven't tried it, but it looks like a special case, using the name to
serialize and valueOf() to de-serialize:

<http://java.sun.com/j2se/1.5.0/docs/guide/serialization/relnotes15.html>
 
M

Mike Schilling

Roedy said:
It just hit me that there must be some special kludge to handle
serialising the values of enums.

I have not checked if enums are indeed serialisable. If they are, when
you reconstitute, you want to get the value reconstituted to point to
the enum constant object in the target machine, not a pointer to a
reconstructed clone of the enum constant object from the source
machine.

That's really no harder than the normal deserialization for singletons. It
does indeed work.

If you want to see something complicated, look at a switch on an enum. It
has to work even if the Enum has been modified so that its ordinal values
change.
 
D

Daniel Pitts

Roedy said:
It just hit me that there must be some special kludge to handle
serialising the values of enums.

I have not checked if enums are indeed serialisable. If they are, when
you reconstitute, you want to get the value reconstituted to point to
the enum constant object in the target machine, not a pointer to a
reconstructed clone of the enum constant object from the source
machine.

Perhaps they are serialised as ordinals. Perhaps they are serialised
as string enum constant names. (That would allow you to add new enum
constants and still read old serialised files).

Has anyone ever experimented to figure out how it works or if it works
at all?
Why experiment when it is clearly documented?
<http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/serial-arch.html#enum>
 
A

Arved Sandstrom

Roedy said:
It is not properly documented. The problem is Sun puts things in
release notes or guides, then removes the information for the next
release. If the information also applies to subsequent releases, it
should be permanently integrated into the docs.

There is a "Serialized Form" link on the
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html page. This
tells you exactly which fields are serialized. Seems like enough
information to me.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

enums and signed/unsigned 4
Smuggling information to enums 22
Loving Enums 2
enums and modifiers 5
Converting between different enums? 3
enums 6
I don't understand enums 14
passing enums through stdarg 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top