0

What happens if I serialize a Map(or List) with a java version, and I try to deserialize it with other java version, where the serialVersionUID changed? I suppose it will fail.

If you create a lib for others to use what will be the preferred way of serializing objects, using Java Objects like Map, List or using an array of self made objects?

e.g. List<MyObject> or MyObject[]? Map<String, MyObject> or MyObject2[] (MyObject2 contains the key and MyObject)?

3
  • 1
    If you authored the class, you control the serialVersionUID field. This won't change simply because people are using different Java versions. Are you confusing these concepts together? Commented Sep 29, 2014 at 8:30
  • 1
    Yes I create MyObject class and for this I can control the serialVersionUID, but I can't control the serialVersionUID of List or Map so I was wondering if it is not better to use my custom object instead of using List or Map? Commented Sep 29, 2014 at 8:41
  • As the serialVersionUID of the JDK's implementations of Map hasn't changed in living memory, it is diffixult to see what this question is actually about. Commented Sep 29, 2014 at 10:23

2 Answers 2

1

If you control the class and if you did not change the serialVersionUID also deserialization of an instance of a class from a older version is possible. There for java provides a concept which is called binary compatibility. Most of the flexibility of binary compatibility comes from the use of a late binding of symbolic references for the names of classes, interfaces, fields, methods:

http://docs.oracle.com/javase/7/docs/platform/serialization/spec/version.html

So core classes from java e.g. HashMap, ArrayList, Vector... will be deserializable even if the class will be involved in a future version of java.

If you wish to control versioning in your own class, you simply have to provide the serialVersionUID field manually and ensure it is always the same, no matter what changes you make to the classfile.

See also this article: http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you are correct, deserialization with changed serialVersionUID will fail. Version of JDK doesn't matter here.

If you create a lib for others to use what will be the preferred way of serializing objects, using Java Objects like Map, List or using an array of self made objects?

You can serialize objects to some more portable format, like plain text with (e.g. JSON, XML). You may take a look at JAXB or XStream.

But keep in mind, that main usage of serialization is to transfer objects over the network. If you would like to store some state you typically should use a database. Serialization to bytes is useful mainly for short-lived objects (because as you noticed, object may change, and thus serializationVersionId may change also).

Hope it helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.