[MUD-Dev] Java Serialization

Kwon Ekstrom justice at softhome.net
Thu Dec 12 23:19:06 CET 2002


From: <ceo at grexengine.com>

> Assuming you are referring to the problem of:

>   1. Create class

>   2. Instantiate Object from Class

>   3. Serialize Object

>   4. Alter Class, and attempt to de-serialize Object as instance
>   of change Class.

> Then sorry, but that's not merely inaccurate, its completely
> untrue. Java explicitly and effortlessly (takes approx 30 seconds,
> once) supports loading and saving of different versions of the
> same Object.

Actually, I'm correct, to recover data from a modified class you
must:

  A. run serialver to retrieve the SUID of the previous class.

  B. define the field serialVersionUID with the outputted value

  C. load the data.

In a rapidly changing environment, this is a fairly tedious process,
especially if you have to modify several serialized objects.  If you
poke through documentation, you'll notice that alot of components
are migrating to using XML to serialize.

> FYI, if you look at the documentation for serialization, you will
> find you merely need to declare a "public static final String"
> (IIRC) which you initialize to the hash assigned to objects of
> that class by Serialization.

private static final long serialVersionUID = <value>;

> So, perform steps 1 (and possibly 2) above. Then use the
> appropriate java tool (cannot remember the name, but it is
> described in the

serialver

> explicitly identifies itself. Now you can effortlessly load and
> save objects, and alter the Class as often as you like.

I'd hardly call it effortless.

There are several good uses for serialization.  RMI, fairly static
base classes, etc.  What I tend to use is a HashMap to store
key/value pairs, it works fast, extremely fast, you have to manually
define your key/value pairs of course and write some code to handle
loading them, and granted it takes a hair more space than direct
serialization, but it's quite scalable (the value can be any
serializable object, including a hashmap containing key/value pairs
for reconstructing a child object).

It's simply a matter of choosing your tools, but at least I can
choose my tools.  ATM I use both files (for system data to be loaded
up, including database configuration) and database tables.  Often
some values store as database fields (allowing me to easily generate
certain statistics) while others are stored as blobs.

There are also field modifiers for serialization to consider in the
java language, which can make it difficult to figure out exactly
what data gets serialized and what doesn't.

-- Kwon J. Ekstrom


_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list