Skip to content

Instantly share code, notes, and snippets.

@xerial
Created October 12, 2018 19:11
Show Gist options
  • Save xerial/58e14b21a4492d6f87765fe9bff4b61a to your computer and use it in GitHub Desktop.
Save xerial/58e14b21a4492d6f87765fe9bff4b61a to your computer and use it in GitHub Desktop.
package serde
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
sealed trait Color
case object RED extends Color
case class A(colors:Seq[Color])
object SerdeTest {
def serialize(a: A): Array[Byte] = {
val b = new ByteArrayOutputStream()
val oo = new ObjectOutputStream(b)
oo.writeObject(a)
oo.close()
b.toByteArray
}
def deserialize(b: Array[Byte]): Any = {
val in = new ByteArrayInputStream(b)
val oi = new ObjectInputStream(in)
val obj = oi.readObject()
obj
}
def main(args:Array[String]): Unit = {
val a = A(Seq(RED))
val b = serialize(a) // OK
val a1 = deserialize(b) // NG: cannot assign instance of scala.collection.generic.DefaultSerializationProxy to field serde.A.colors of type scala.collection.immutable.Seq in \
instance of serde.A
println(a1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment