Skip to content

Instantly share code, notes, and snippets.

@applethepeanut
Created December 11, 2016 21:28
Show Gist options
  • Save applethepeanut/1f3c0c9d3cb495da7d4756f1df9ad7c1 to your computer and use it in GitHub Desktop.
Save applethepeanut/1f3c0c9d3cb495da7d4756f1df9ad7c1 to your computer and use it in GitHub Desktop.
case class Blah(id: String, name: String)
object Blah {
implicit val jsonFormats = new RootJsonFormat[Blah] {
override def read(json: JsValue): Blah = {
val js = json.asJsObject
(js.fields("some_id"), js.fields("some_name")) match {
case (JsString(id), JsString(name)) => Blah(id, name)
case _ => deserializationError(s"Failed to read `Blah` from json `$json`")
}
}
override def write(blah: Blah): JsValue = JsObject(
"id" -> JsString(blah.id),
"name" -> JsString(blah.name)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment