Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.