diff options
author | Mathias <mathias@parboiled.org> | 2011-10-12 01:00:25 -0700 |
---|---|---|
committer | Mathias <mathias@parboiled.org> | 2011-10-12 01:00:25 -0700 |
commit | c453ff95bcd69f57299b198c552e409e27488f74 (patch) | |
tree | ff1f4ab000c68f18c8b63cbba19ca8f019b3a600 | |
parent | 2e7e3e955a56ccb5fa9382a42f61de29a31709cd (diff) | |
parent | fe8fca4e69a21b5a795a000d4bd27026a06aa6a0 (diff) | |
download | spray-json-c453ff95bcd69f57299b198c552e409e27488f74.tar.gz spray-json-c453ff95bcd69f57299b198c552e409e27488f74.tar.bz2 spray-json-c453ff95bcd69f57299b198c552e409e27488f74.zip |
Merge pull request #9 from stefri/issue8
Thanks for this contribution, Steffen!
-rw-r--r-- | src/main/scala/cc/spray/json/BasicFormats.scala | 4 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/JsValue.scala | 8 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/BasicFormatsSpec.scala | 26 |
3 files changed, 34 insertions, 4 deletions
diff --git a/src/main/scala/cc/spray/json/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala index e16b4ca..32ae91e 100644 --- a/src/main/scala/cc/spray/json/BasicFormats.scala +++ b/src/main/scala/cc/spray/json/BasicFormats.scala @@ -42,6 +42,7 @@ trait BasicFormats { def write(x: Float) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.floatValue + case JsNull => Float.NaN case _ => throw new DeserializationException("Float expected") } } @@ -50,6 +51,7 @@ trait BasicFormats { def write(x: Double) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.doubleValue + case JsNull => Double.NaN case _ => throw new DeserializationException("Double expected") } } @@ -124,4 +126,4 @@ trait BasicFormats { } } -}
\ No newline at end of file +} 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..11576a4 100644 --- a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala @@ -26,18 +26,42 @@ 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 } + "convert a JsNull to a Float" in { + JsNull.fromJson[Float].isNaN mustEqual Float.NaN.isNaN + } } "The DoubleJsonFormat" should { "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 } + "convert a JsNull to a Double" in { + JsNull.fromJson[Double].isNaN mustEqual Double.NaN.isNaN + } } "The ByteJsonFormat" should { @@ -119,4 +143,4 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { } } -}
\ No newline at end of file +} |