sjson icon indicating copy to clipboard operation
sjson copied to clipboard

Certain double values throw NumberFormatExceptions

Open adamklein opened this issue 11 years ago • 1 comments

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.

adamklein avatar Aug 23 '12 20:08 adamklein