I
I
Igor2019-03-06 08:39:14
Java
Igor, 2019-03-06 08:39:14

How to use serialize as save?

Good afternoon.
For my home project, I made a save file through serialization.
Everything worked great.
But when there were changes in the classes for saving, the whole saving began to fall, although, according to the idea, only the changed classes should not be read.
But when you change one class, you get a ClassCastException or InvalidClassException on this line

Map<String, Object> saveMap = (HashMap<String, Object>) oin.readObject();

And the whole save fails.
Although, logically, one class should fall, since we read in this line not a specific class, but Object. And then I already cast the object to some specific type.
Why and how to avoid it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2019-03-06
@cawendish

With standard serialization, this cannot be avoided in any way. I suggest using third-party libraries:
- json format - a lot of libraries for working with it, for example https://github.com/google/gson
- kryo - excellent and very fast serializer (!) - https://github.com/EsotericSoftware/ kryo - protobuf
- also very good - https://developers.google.com/protocol-buffers/doc...
- xml format - a lot of libraries, even built into the JVM
or on kryo. But if cross-platform is needed, then protobuf.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question