python-javaobj icon indicating copy to clipboard operation
python-javaobj copied to clipboard

if serialized by encoding toString ISO-8859-1, how to unserialized

Open stevenxuwoss opened this issue 4 years ago • 2 comments

public static String serialize(Object obj) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream; objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); //String string = byteArrayOutputStream.toString(); String string = byteArrayOutputStream.toString("ISO-8859-1"); objectOutputStream.close(); byteArrayOutputStream.close(); return string; }

stevenxuwoss avatar Apr 29 '20 06:04 stevenxuwoss

If I understand correctly, you want to transmit the serialized object as a String. The ISO-8859-1 encoding should indeed ensure to have the equivalent of the underlying bytes without conversion nor error. Note that internally, the String will have its "modified UTF-8" representation of the content: it is a Java String.

If you can, it would be preferable to use the byte array returned by byteArrayOutputStream.toByteArray(), as you won't have to handle encoding.

If you need the result to be a String, I recommend you to use a format that is not sensitive to the encoding, like Base64

tcalmant avatar Apr 29 '20 09:04 tcalmant

thanks very much, i'll hava a try with base64

stevenxuwoss avatar Apr 29 '20 10:04 stevenxuwoss