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);
}
}
}
=====================================================
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);
}
}
}
=====================================================