Skip to content

Instantly share code, notes, and snippets.

@justinhj
Last active July 10, 2019 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhj/e7ebd98c8870fbe1b057f94255173536 to your computer and use it in GitHub Desktop.
Save justinhj/e7ebd98c8870fbe1b057f94255173536 to your computer and use it in GitHub Desktop.
Serialize and deserialize a map with a case class key
// "com.lihaoyi" %% "upickle" % "0.7.5",
import upickle.default.{read,write}
import upickle.default.{ReadWriter => RW, macroRW}
object PickleMap {
case class KeyClass(s: String, i: Int)
object KeyClass{
implicit val rw: RW[KeyClass] = macroRW
}
val m = Map[KeyClass, String](KeyClass("test 1", 17) -> "data 1",
KeyClass("test 2", 21) -> "data 1")
def main(args : Array[String]) : Unit = {
// convert to Json
val serialized = write(m)
println(s"Json output $serialized")
// convert back
val parsed = read[Map[KeyClass, String]](serialized)
println(s"Parsed $parsed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment