Created
October 12, 2018 19:11
-
-
Save xerial/58e14b21a4492d6f87765fe9bff4b61a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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