Skip to content

Instantly share code, notes, and snippets.

@skvithalani
Last active February 25, 2020 11:56
Show Gist options
  • Save skvithalani/e1deba416104202984db4355e5f46be6 to your computer and use it in GitHub Desktop.
Save skvithalani/e1deba416104202984db4355e5f46be6 to your computer and use it in GitHub Desktop.
class BorerAkkaSerializer() extends Serializer with Codecs {
override def identifier: Int = 19923
override def includeManifest: Boolean = true
override def toBinary(o: AnyRef): Array[Byte] = o match {
case x: Zoo => Cbor.encode(x).toByteArray
case x: Park => Cbor.encode(x).toByteArray
case x: Theatre => Cbor.encode(x).toByteArray
case _ =>
throw new RuntimeException(s"does not support encoding of $o")
}
override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = {
if (classOf[Zoo].isAssignableFrom(manifest.get))
Cbor.decode(bytes).to[Zoo].value
else if (classOf[Park].isAssignableFrom(manifest.get))
Cbor.decode(bytes).to[Park].value
else if (classOf[Theatre].isAssignableFrom(manifest.get))
Cbor.decode(bytes).to[Theatre].value
else
throw new RuntimeException(s"does not support decoding of ${manifest.get}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment