Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deleting a class value from a serialized object

 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic