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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so you say that, longer the string is it loses more data?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.