diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/cc/spray/json/JsValue.scala | 8 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/BasicFormatsSpec.scala | 20 |
2 files changed, 25 insertions, 3 deletions
diff --git a/src/main/scala/cc/spray/json/JsValue.scala b/src/main/scala/cc/spray/json/JsValue.scala index 86dbb06..e4ab734 100644 --- a/src/main/scala/cc/spray/json/JsValue.scala +++ b/src/main/scala/cc/spray/json/JsValue.scala @@ -110,7 +110,11 @@ case class JsNumber(value: BigDecimal) extends JsValue object JsNumber { def apply(n: Int) = new JsNumber(BigDecimal(n)) def apply(n: Long) = new JsNumber(BigDecimal(n)) - def apply(n: Double) = new JsNumber(BigDecimal(n)) + def apply(n: Double) = n match { + case n if n.isNaN => JsNull + case n if n.isInfinity => JsNull + case _ => new JsNumber(BigDecimal(n)) + } def apply(n: BigInt) = new JsNumber(BigDecimal(n)) def apply(n: String) = new JsNumber(BigDecimal(n)) } @@ -135,4 +139,4 @@ case object JsFalse extends JsBoolean { /** * The representation for JSON null. */ -case object JsNull extends JsValue
\ No newline at end of file +case object JsNull extends JsValue diff --git a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala index fcae4bb..7f74e37 100644 --- a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala @@ -26,6 +26,15 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a Float to a JsNumber" in { 4.2f.toJson mustEqual JsNumber(4.2f) } + "convert a Float.NaN to a JsNull" in { + Float.NaN.toJson mustEqual JsNull + } + "convert a Float.PositiveInfinity to a JsNull" in { + Float.PositiveInfinity.toJson mustEqual JsNull + } + "convert a Float.NegativeInfinity to a JsNull" in { + Float.NegativeInfinity.toJson mustEqual JsNull + } "convert a JsNumber to a Float" in { JsNumber(4.2f).fromJson[Float] mustEqual 4.2f } @@ -35,6 +44,15 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a Double to a JsNumber" in { 4.2.toJson mustEqual JsNumber(4.2) } + "convert a Double.NaN to a JsNull" in { + Double.NaN.toJson mustEqual JsNull + } + "convert a Double.PositiveInfinity to a JsNull" in { + Double.PositiveInfinity.toJson mustEqual JsNull + } + "convert a Double.NegativeInfinity to a JsNull" in { + Double.NegativeInfinity.toJson mustEqual JsNull + } "convert a JsNumber to a Double" in { JsNumber(4.2).fromJson[Double] mustEqual 4.2 } @@ -119,4 +137,4 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { } } -}
\ No newline at end of file +} |