I have a custom class (constructor below) that I cannot seem to serialize.
public ObjectNode(String name, int crackLevel,ArrayList<ObjectNode> filesOnComputer)
Every time I try to serialize an object of this class from an ArrayList of object nodes I get a Class cast exception. ArrayList cannot be cast to ObjectNode
Code- Global Vars:
ArrayList<ObjectNode>a=new ArrayList<ObjectNode>();
File file = new File("/mnt/sdcard/","cheesepuff.txt");
Relevant Code:
public void serializeFile()
{
a.add(new ObjectNode("Level 1 Waterwall","ww1", 1, 5));
try {
Log.i("AAA","before serialize: "+a.toString());
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(a);
oos.flush();
oos.close();
Log.i("AAA","finished serialize");
}
catch(Exception e) {
Log.i("aaa","Exception during serialization: " + e);
System.exit(0);
}
}
public void deserializeFile()
{
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
ObjectNode obj=(ObjectNode)ois.readObject();
Log.i("aaa","obj: "+obj.ts());
//a.add((ObjectNode) ois.readObject());
ois.close();
Log.i("aaa","after serialize: " + a);
}
catch(Exception e) {
Log.i("aaa","Exception during deserialization: " +
e);
System.exit(0);
}
}
Is my best option to make everything a string in order to serialize that, and then convert the string back to what I actually need after deserialization?
You are serializing
awhich isArrayList<ObjectNode>.When deserializing, you get back exactly that, but you try to store it as
ObjectNodeYou should do
UPDATE: Or, as PeterLawrey correctly states,
(probably then you would have to redefine
aasList<ObjectNode>, but that is another good thing to do).