Skip to content

Commit

Permalink
Merge pull request #183 from ktoso/wip-quoted-BIG-numbers-ktoso
Browse files Browse the repository at this point in the history
+#182 allows BigInt/BigDecimal to be obtained from JsStrings
  • Loading branch information
sirthias committed Apr 25, 2016
2 parents 4fbde3a + aae2c6e commit 1f98f29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/scala/spray/json/BasicFormats.scala
Expand Up @@ -79,6 +79,7 @@ trait BasicFormats {
}
def read(value: JsValue) = value match {
case JsNumber(x) => x
case JsString(x) => BigDecimal(x)
case x => deserializationError("Expected BigDecimal as JsNumber, but got " + x)
}
}
Expand All @@ -90,6 +91,7 @@ trait BasicFormats {
}
def read(value: JsValue) = value match {
case JsNumber(x) => x.toBigInt
case JsString(x) => BigInt(x)
case x => deserializationError("Expected BigInt as JsNumber, but got " + x)
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/spray/json/BasicFormatsSpec.scala
Expand Up @@ -105,6 +105,9 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol {
"convert a JsNumber to a BigDecimal" in {
JsNumber(42).convertTo[BigDecimal] mustEqual BigDecimal(42)
}
"""convert a JsString to a BigDecimal (to allow the quoted-large-numbers pattern)""" in {
JsString("9223372036854775809").convertTo[BigDecimal] mustEqual BigDecimal("9223372036854775809")
}
}

"The BigIntJsonFormat" should {
Expand All @@ -114,6 +117,9 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol {
"convert a JsNumber to a BigInt" in {
JsNumber(42).convertTo[BigInt] mustEqual BigInt(42)
}
"""convert a JsString to a BigInt (to allow the quoted-large-numbers pattern)""" in {
JsString("9223372036854775809").convertTo[BigInt] mustEqual BigInt("9223372036854775809")
}
}

"The UnitJsonFormat" should {
Expand Down

0 comments on commit 1f98f29

Please sign in to comment.