Skip to content

Instantly share code, notes, and snippets.

@johnykov
Last active August 29, 2015 14:13
Show Gist options
  • Save johnykov/51a14c7ded8bde425d0d to your computer and use it in GitHub Desktop.
Save johnykov/51a14c7ded8bde425d0d to your computer and use it in GitHub Desktop.
This is my class parsing special fuckeup JSONs where instead `"key":numericValue` I get `"key":"numericValue"`. I use https://github.com/spray/spray-json
import spray.json._
object MyJsonProtocol extends DefaultJsonProtocol {
implicit object MetricJsonFormatDouble extends RootJsonFormat[Metric[Double]] {
def write(c: Metric[Double]) = JsObject()
def read(value: JsValue) = value.asJsObject.getFields("total", "ok", "ko") match {
case Seq(JsString(total), JsString(ok), JsString(ko)) =>
Metric[Double](total.toDouble, ok.toDouble, ko.replace("-", "0").toDouble)
case _ => deserializationError("Metric expected")
}
}
implicit object MetricJsonFormatInt extends RootJsonFormat[Metric[Int]] {
def write(c: Metric[Int]) = JsObject()
def read(value: JsValue) = value.asJsObject.getFields("total", "ok", "ko") match {
case Seq(JsString(total), JsString(ok), JsString(ko)) =>
Metric[Int](total.toInt, ok.toInt, ko.replace("-", "0").toInt)
case _ => deserializationError("Metric expected")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment