Serialization and Base Class serialVersionUID ?

S

swenjohnson

I'm attempting to send a JDK 1.4 HashMap from a JVM to a file, and then
deserialize into a .Net environment. The Microsoft java.util.HashMap is
based on an earlier version of the JDK (1.2 ?), and the
serialVersionUID doesn't match the 1.4 version.

So, I've created a class called CrossSerializableHashMap that extends
java.util.HashMap with it's own readObject and writeObject methods and
serialVersionUID. However, when I call the .Net
ObjectInputStream.readObject I get a java.IO.InvalidClassException
complaining about a mismatched serialVersionUID for java.util.HashMap.

I also tried implementing Externalizable with
readExternal/writeExternal instead of readObject/writeObject. This
caused the .ser file to be a little shorter, but I still got the same
exception.

Is there some way to tell the serialization mechanism to ignore the
serialVersionUID's of base classes ? Failing that, does anyone have
another workaround to suggest ?


-- Swen


=====================================================
public class CrossSerializableHashMap extends java.util.HashMap {

private static final long serialVersionUID = 7320563740544296214L;


// Use an array of these to hold the contents of the Map
private static class CrossSerializableEntry implements Serializable {

private static final long serialVersionUID = -1838944641876062279L;

public final Object key;
public final Object value;
public CrossSerializableEntry (Map.Entry from) {
this.key = from.getKey();
this.value = from.getValue();
}
}

private void writeObject (ObjectOutputStream s) throws IOException {
Object[] rawEntries = this.entrySet().toArray();
CrossSerializableEntry[] entries =
new CrossSerializableEntry[rawEntries.length];
for (int i = 0; i < entries.length; i++) {
entries = new CrossSerializableEntry((Map.Entry)rawEntries);
}
s.writeObject(entries);
}

private void readObject (ObjectInputStream s) throws IOException,
ClassNotFoundException {
CrossSerializableEntry[] entries;
entries = (CrossSerializableEntry[])s.readObject();
for (int i = 0; i < entries.length; i++) {
CrossSerializableEntry entry = entries;
this.put(entry.key, entry.value);
}
}
}
=====================================================
 
W

Wendy S

Failing that, does anyone have another workaround to suggest ?

XML? I know some people object to it on moral grounds, but both systems
understand it and if you need to see what's going back and forth you can
just look at it in a text editor.
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top