wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes deleting a class value from a serialized object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "deleting a class value from a serialized object" Watch "deleting a class value from a serialized object" New topic
Author

deleting a class value from a serialized object

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
Suppose I have a class like this:

class Dog implements Serializable{
int size;
boolean chasesCars;
String name;
static final long serialversionUID = 463683736;
// or something like that
}

Then I serialize the object. Then I add other class values to Dog:

char inishal;
int weight;
ArrayList listOfFoodEaten;

Then I read in the serialized object. I've done this before several times, (but not with char or int). The ArrayList object gets a default value of null, right? The int gets a default value of 0, right? How about the char? What is its default value? What is the default value for a boolean?

Now, suppose instead of adding class values, I delete the class values. Suppose I delete the boolean, so I have this:

class Dog implements Serializable{
int size;
String name;
static final long serialversionUID = 463683736;
// or something like that
}


According to what I've read, when the serialized object is read in, an exception is thrown, because there is a boolean in the serialized object, but no class value to assign it to. What can be done? Here are some ideas.

1. Put in a try-catch loop to handle the exception. But I have a feeling it won't work.
2. Make the boolean transient, then read in the serialized object. Would that work? If it does, then I could serialize the object again (without the boolean because it is transient). Then I could delete the boolean with no problem.
3. Read in the object, make the boolean its default value (or in case of an Object, make it null), serialize it (would it not be serialized because it is the default value?), then delete the boolean with no problem because it was not serialized.
4. Make a new class:
class NewDogValues{
int size;
String name;
}

then, give it only the values that I want to serialize:
NewDogValues myNewDog = new NewDogValues();
myNewDog.size = dog.size;
myNewDog.name = dog.name;

Then serialize myNewDog. Then delete the boolean from Dog. Then read in myNewDog. Then,
dog.size = myNewDog.size;
dog.name = myNewDog.name;

5. Serialize a list of the Objects that I need:
myObjectWriter.write(dog.name);
myObjectWriter.write(new Integer(dog.size));
Then delete the boolean from Dog. Then,
Dog mydog = new Dog;
mydog.name = (String) myObjectReader.readObject();
mydog.size = ((Integer) myObjectReader.readObject()).intValue();

I have a feeling that only 4. and 5. will work. But if 1. 2. 3. would work, they would be easier, I think.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Another option would be to instead write the data that the object represents to a file in some easily parseable format. You could then write code to parse the data and translate it into any type of object you want, using only the fields and values you need.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
Thank you.
Do you mean make a text file that has the values that I need, like this?

BufferedWriter writer;
writer.write(mydog.name);
writer.newLine();
writer.write(Integer.toString(mydog.size));
writer.close();

Then delete the boolean from Dog, then,

BufferedReader reader;
mydog.name = reader.readLine();
mydog.size = Integer.parseInt(reader.readLine());

Is that what you mean?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: deleting a class value from a serialized object
 
Similar Threads
A query regarding serialization in Java
Why are transient static variables allowed?
Serialization
final transient and static transient
Clarification on concepts for Serialization