Hello Zlatko,
a simple way would be to store a version attribute before you store anything else.
That way you can also easily migrate old data on the fly. I have seen this work very
well in C++. However, if you have lots of data and complex version migrations
(v1->v2, v2->v3, ...) this will cost you lots of extra cpu. (see sample below)
Regards
Kim
/**
* Version 1:
*/
class A {
private static final int version = 1;
private ArrayList data = new ArrayList();
private void writeObject(ObjectOutputStream ous) throws IOException {
out.writeObject(version);
out.writeObject(data);
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
version = out.readObject();
if ( 1 == version ) {
data = out.readObject();
}
}
}
/**
* Version 2:
*/
class A {
private static final int version = 2;
private ArrayList data = new ArrayList();
private String name = "name";
private void writeObject(ObjectOutputStream ous) throws IOException {
out.writeObject(version);
out.writeObject(data);
out.writeObject(name);
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
version = out.readObject();
data = out.readObject();
if (version > 1) {
name = out.readObject();
}
else {
/* convert version 1 data to meaningful version 2 or throw exception */
name = "none";
}
}
}
/**
* Version 3:
*/
class A {
private static final int version = 3;
private ArrayList data = new ArrayList();
private String name = "name";
private int age = 0;
private void writeObject(ObjectOutputStream ous) throws IOException {
out.writeObject(version);
out.writeObject(data);
out.writeObject(name);
out.writeObject(age);
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
version = out.readObject();
data = out.readObject();
if (version > 1) {
name = out.readObject();
}
else {
/* convert version 1 data to meaningful version 2 or throw exception */
name = "name";
}
if (version > 2) {
age = out.readObject();
}
else {
/* convert version 2 data to meaningful version 3 or throw exception */
age = 0;
}
}
}
-----Ursprungliche Nachricht-----
Von: Zlatko Kostadinov [mailto:[EMAIL PROTECTED]]
Gesendet: Montag, 26. August 2002 13:14
An: JDJList
Betreff: [jdjlist] problem with serizlization
Hi guys
I had serizlized objects in this way:
class A {
private ArrayList data = new ArrayList();
private void writeObject(ObjectOutputStream ous) throws IOException {
out.writeObject(data);
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
data = out.readObject();
}
}
And my program now works with objects of this type. I want to add new field
in the class so the class will be
class A {
private ArrayList data = new ArrayList();
private name = "name";
private void writeObject(ObjectOutputStream ous) throws IOException {
out.writeObject(data);
out.writeObject(name);
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
data = out.readObject();
// here is the problem - how to determine is this an instance of the
new version or no.
// must I read the name or not.
}
}
Any ideas are wellcome
Regards
Zlatko
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm
To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm