serialization - Convert Any type in scala to Array[Byte] and back -
i have following question:
i have variable value in program declared value.
i want convert value byte array..
how can serialize byte array , back? found examples related other types such double or int, not any.
this should need. it's pretty similar how 1 in java.
import java.io.{bytearrayinputstream, bytearrayoutputstream, objectinputstream, objectoutputstream} object serialization extends app { def serialise(value: any): array[byte] = { val stream: bytearrayoutputstream = new bytearrayoutputstream() val oos = new objectoutputstream(stream) oos.writeobject(value) oos.close stream.tobytearray } def deserialise(bytes: array[byte]): = { val ois = new objectinputstream(new bytearrayinputstream(bytes)) val value = ois.readobject ois.close value } println(deserialise(serialise("my test"))) println(deserialise(serialise(list(1)))) println(deserialise(serialise(map(1 -> 2)))) println(deserialise(serialise(1))) }
Comments
Post a Comment