Serialization and Base Class serialVersionUID ?

Discussion in 'Java' started by swenjohnson@yahoo.com, Feb 17, 2005.

  1. Guest

    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);
    }
    }
    }
    =====================================================
    , Feb 17, 2005
    #1
    1. Advertising

  2. Wendy S Guest

    <> wrote:
    > 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.

    --
    Wendy
    Wendy S, Feb 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,215
    deadsea
    Jan 2, 2005
  2. Steven

    serialVersionUID and Eclipse

    Steven, Apr 29, 2005, in forum: Java
    Replies:
    1
    Views:
    767
    Peter MacMillan
    Apr 29, 2005
  3. Alf P. Steinbach
    Replies:
    6
    Views:
    525
    John Carson
    Sep 3, 2005
  4. Hicham Mouline
    Replies:
    1
    Views:
    569
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    123
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page