Skip to content

Commit

Permalink
Implemented deserialization support (Issue #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Fritzsche committed Oct 11, 2011
1 parent 211e72c commit fe8fca4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/scala/cc/spray/json/BasicFormats.scala
Expand Up @@ -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")
}
}
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -124,4 +126,4 @@ trait BasicFormats {
}
}

}
}
6 changes: 6 additions & 0 deletions src/test/scala/cc/spray/json/BasicFormatsSpec.scala
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit fe8fca4

Please sign in to comment.