Last active
July 10, 2019 18:23
-
-
Save justinhj/e7ebd98c8870fbe1b057f94255173536 to your computer and use it in GitHub Desktop.
Serialize and deserialize a map with a case class key
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
// "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