1

I have an Object called Doodle, I serialize it into a String and it's ok. The problem arrises when I try to deserialize the object, the error is this: java.io.InvalidClassException: java.util.ArrayList; local class incompatible: stream classdesc serialVersionUID = 8664875232659988799, local class serialVersionUID = 8683452581122892189

The methods to serialize and deserialize are the following:

public static String serDoodle(Doodle dood){
    String serializzato = null;
    try {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(dood);
        so.flush();
        serializzato = bo.toString();
        so.close();
        bo.close();
    } catch (Exception e) {
        System.out.println(e);

    }
    return serializzato;

}
public static Doodle deserDoodle(String deserializza){
    Doodle dod = new Doodle();


    try {
        byte[] b = deserializza.getBytes(); 
        ByteArrayInputStream bi = new ByteArrayInputStream(b);
        ObjectInputStream si = new ObjectInputStream(bi);
        dod=(Doodle) si.readObject();
        si.readObject().getClass();
        si.close();
        bi.close();
    } catch (Exception e) {
        System.out.println("deserDoodle "+e);

    }
    return dod;


}

I use the same method(but with different variable) to serialize another type of object and with that one it works greatly. I don't understand where is the trouble!

J. Doe
  • 29
  • 7
user3457185
  • 175
  • 1
  • 1
  • 7

1 Answers1

1

I serialize it into a String and it's ok

No, it isn't OK. String is not a container for binary data. The round-trip between byte-array and String isn't guaranteed to be losses. Don't do this. Use byte[], or at least Base64-encode it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • so you say that, longer the string is it loses more data? – user3457185 Mar 30 '14 at 23:32
  • @user3457185 As usual with 'so you say' questions, I said nothing of the kind. I said it isn't OK to use `String` as a container for binary data. Period. It has nothing to do with the length. – user207421 Mar 30 '14 at 23:47