I'm trying to serialize and deserialize an array list with a object inside:
HairBirt param = new HairBirt();
param.setName("name");
param.setValue(2.3f);
HairBirt param2 = new HairBirt();
param2.setName("name2");
param2.setValue(2.4f);
ArrayList<HairBirt> list = new ArrayList<HairBirt>();
list.add(param);
list.add(param2);
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
ObjectOutputStream obj_out = new ObjectOutputStream(bos);
obj_out.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
}
String encoded = bos.toString();
try {
encoded = URLEncoder.encode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.print("The serialized output is: " + encoded);
//DECODE
ArrayList<HairBirt> paramDecoded;
String myParam = null;
try {
myParam = URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.println("Got parameters");
ByteArrayInputStream bis = new ByteArrayInputStream(myParam.getBytes());
try {
ObjectInputStream obj_in = new ObjectInputStream(bis);
paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
The HairList object is also a serializable object.
This code execution is returning the following error:
java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189
in line paramDecoded = (ArrayList<HairBirt>) obj_in.readObject();
I don't know what i'm doing wrong. Can you give a tip?
Update:
Resolved: Just used a native array of HairBirt instead of a ArrayList and it works:
HairBirt[] list = new HairBirt[x];
instead of
ArrayList<HairBirt> list = new ArrayList<HairBirt>();
Thank you all for your help.
In Java, the ArrayList class implements a Serializable interface by default i.e., ArrayList is by default serialized. We can just use the ObjectOutputStream directly to serialize it.
In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly in order to serialize ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.
List is not but implementation classes like ArrayLists are serializable. You can use them.
How to serialize and deserialize ArrayList in Java In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly in order to serialize ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.
A Java object is serializable if its class or any of its superclasses implement either the java.io.Serializable interface or its sub interface, java.io.Externalizable. When an object is serialized, information that identifies its class is recorded in the serialized stream.
So it is serializer that should not add this type information... and if so, deserialization should work (exception indicates it is surprised to see an array). So I think there is a problem in handling. However: another way to work around this issue would be to wrap typed value in a wrapper like:
ArrayList is a class under the collections framework of java. It is present in java.util package. An ArrayList is a re-sizable array in java i.e., unlike an array, the size of an ArrayList can be modified dynamically according to our requirement.
Don't use ByteArrayOutputStream.toString()
- instead, use toByteArray()
and base64-encode that binary data to convert it into a string without losing information.
I strongly suspect that's the main problem - that you were losing the data after serialization. You should probably also close or at least flush the ObjectOutputStream
. I don't know offhand whether that actually does anything in this case, but it would seem to be a good idea.
I don't believe there's any base64 support directly in Java (in a public class, anyway) but there are various 3rd party libraries you can use, such as the one in the Apache Commons Codec library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With