sjson
sjson copied to clipboard
Certain double values throw NumberFormatExceptions
Double.PositiveInfinity, Double.NaN, etc.
In my local branch I've got:
implicit object DoubleFormat extends Format[Double] {
def writes(o: Double) = try {
JsValue.apply(o)
}
catch {
// we need Infinity/NaN goodness
case e: NumberFormatException => JsValue.apply(o.toString)
}
def reads(json: JsValue) = json match {
case JsString(n) => n.toDouble
case JsNumber(n) => n.doubleValue
case _ => throw new RuntimeException("Double (or string) expected")
}
}
I'm not 100% sure the wisdom of this, but it works for me.
why? i don't think NaN or PositiveInfinity are allowed in JSON so imho writes should signal an error in these cases.