1

I am working on an Android application in which one I have to persist some data. In order to persist them, I use the java serialization mechanism.

Here the structure of the class I am serializing :

public final class A
    implements Serializable
{

  public static final class B
      implements Serializable
  {

    public final String field1;

    public final String field2;

    public B(String field1, String field2)
    {
      this.field1= field1;
      this.field2= field2;
    }
  }

  public static final class C
      implements Serializable
  {

    public long field1;

    public final B field2;

    public C(B field2)
    {
      this.field2 = field2;
    }

    public void setField1(long field1)
    {
      this.field1= field1;
    }

  }

  private final List<C> list1= new ArrayList<>();

  private final List<C> list2= new ArrayList<>();

  public A()
  {

  }

  public List<C> getList1()
  {
    return list1;
  }

  public List<C> getList2()
  {
    return list2;
  }

  //some other methods

}

As you can see, classes implement Serializable but I do not specify the serialVersionUID static field.

I have to make a big update into this app and remove some methods from the class A and C.

As expected, when I try to deserialize the old classes into the new ones, I have the following exception :

Caused by: java.io.InvalidClassException: com.my.package.A; Incompatible class (SUID): com.my.package.A: static final long serialVersionUID =9103658319690261655L; but expected com.my.package.A: static final long serialVersionUID =7863476896806312319L; at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336) at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657) at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782) at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

So, into my class A, I tried to specify the serialVersionUID static field with the expected value :

private static final long serialVersionUID = 7863476896806312319L;

But my app still crashes... I now have the following stacktrace :

Caused by: java.io.InvalidClassException: com.my.package.A; Incompatible class (SUID): com.my.package.A: static final long serialVersionUID =7863476896806312319L; but expected com.my.package.A: static final long serialVersionUID =9103658319690261655L; at java.io.ObjectInputStream.verifyAndInit(ObjectInputStream.java:2336) at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1643) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657) at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1782) at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:761) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)

As you can see, the old serialVersionUID field has now the value of the expected one and the expected one has the value of the old serialVersionUID field...

So the question is : how can I recover my data and force the deserialization of the old classes into the new ones ?

Thank you for your help.

rolandl
  • 1,769
  • 1
  • 25
  • 48

1 Answers1

3

Looking at a closely related question here, I think you should try setting the serialVersionUID to the expected value of 9103658319690261655L.

Please try setting:

private static final long serialVersionUID = 9103658319690261655L;

I hope this helps, give it a try and let us know.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32