diff --git a/src/main/scala/cc/spray/json/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala index e16b4caf..32ae91e8 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/test/scala/cc/spray/json/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala index 7f74e379..11576a44 100644 --- a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala @@ -38,6 +38,9 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "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 { @@ -56,6 +59,9 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "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 {