• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object deserialiaztion .... with different versionUid

 
Debopam Poddar
Ranch Hand
Posts: 51
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class PersonDetails with version 1

public class PersonDetails implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private String name;
private int age;

//getter,setter methods
}


With this version I have serialized one PersonDetails object into a file. Now I have added one more field and updated the versionUid to 2



public class PersonDetails implements Serializable {

/**
*
*/
private static final long serialVersionUID = 2L;

private String name;
private int age;
private String address;

//getter,setter methods
}


Now if I want to deserialize the object it thows me
java.io.InvalidClassException
. If I do not update the versionUid to 2 it deserialize the object successfully and put Address as null.

My question is there any way to deserialize the object keeping the updated versionId?
 
Abhi Deshmukh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we can not deserialize the object keeping the updated serialVersionUID.
It is because serialVersionUID is an exception to the rule that “static fields don’t get serialized”.
ObjectOutputStream writes every time the value of serialVersionUID to the output stream.
ObjectInputStream reads it back and if the value read from the stream does not agree with the serialVersionUID value in the current version of the class, then it throws the InvalidClassException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic