Last active
August 29, 2015 14:13
-
-
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
This file contains hidden or 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
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