From 89798b0079f9957cb6ed322f657eec17d0cbb109 Mon Sep 17 00:00:00 2001 From: Igor Shymko Date: Wed, 30 Nov 2016 17:31:16 +0200 Subject: [PATCH] Throw, don't overflow. Fix #160 --- src/main/scala/spray/json/BasicFormats.scala | 8 +++---- .../scala/spray/json/BasicFormatsSpec.scala | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/scala/spray/json/BasicFormats.scala b/src/main/scala/spray/json/BasicFormats.scala index 65b9ecbd..e11ec025 100644 --- a/src/main/scala/spray/json/BasicFormats.scala +++ b/src/main/scala/spray/json/BasicFormats.scala @@ -25,7 +25,7 @@ trait BasicFormats { implicit object IntJsonFormat extends JsonFormat[Int] { def write(x: Int) = JsNumber(x) def read(value: JsValue) = value match { - case JsNumber(x) => x.intValue + case JsNumber(x) if x.isValidInt => x.intValue case x => deserializationError("Expected Int as JsNumber, but got " + x) } } @@ -33,7 +33,7 @@ trait BasicFormats { implicit object LongJsonFormat extends JsonFormat[Long] { def write(x: Long) = JsNumber(x) def read(value: JsValue) = value match { - case JsNumber(x) => x.longValue + case JsNumber(x) if x.isValidLong => x.longValue case x => deserializationError("Expected Long as JsNumber, but got " + x) } } @@ -59,7 +59,7 @@ trait BasicFormats { implicit object ByteJsonFormat extends JsonFormat[Byte] { def write(x: Byte) = JsNumber(x) def read(value: JsValue) = value match { - case JsNumber(x) => x.byteValue + case JsNumber(x) if x.isValidByte => x.byteValue case x => deserializationError("Expected Byte as JsNumber, but got " + x) } } @@ -67,7 +67,7 @@ trait BasicFormats { implicit object ShortJsonFormat extends JsonFormat[Short] { def write(x: Short) = JsNumber(x) def read(value: JsValue) = value match { - case JsNumber(x) => x.shortValue + case JsNumber(x) if x.isValidShort => x.shortValue case x => deserializationError("Expected Short as JsNumber, but got " + x) } } diff --git a/src/test/scala/spray/json/BasicFormatsSpec.scala b/src/test/scala/spray/json/BasicFormatsSpec.scala index 00da8139..3b8a97d6 100644 --- a/src/test/scala/spray/json/BasicFormatsSpec.scala +++ b/src/test/scala/spray/json/BasicFormatsSpec.scala @@ -27,6 +27,12 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a JsNumber to an Int" in { JsNumber(42).convertTo[Int] mustEqual 42 } + "respect Int upper bound" in { + JsNumber(1000000000000L).convertTo[Int] must throwA[DeserializationException] + } + "respect Int lower bound" in { + JsNumber(-1000000000000L).convertTo[Int] must throwA[DeserializationException] + } } "The LongJsonFormat" should { @@ -36,6 +42,12 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a JsNumber to a Long" in { JsNumber(7563661897011259335L).convertTo[Long] mustEqual 7563661897011259335L } + "respect Long upper bound" in { + JsNumber(BigDecimal("1000000000000000000000000")).convertTo[Long] must throwA[DeserializationException] + } + "respect Long lower bound" in { + JsNumber(BigDecimal("-1000000000000000000000000")).convertTo[Long] must throwA[DeserializationException] + } } "The FloatJsonFormat" should { @@ -87,6 +99,12 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a JsNumber to a Byte" in { JsNumber(42).convertTo[Byte] mustEqual 42 } + "respect Byte upper bound" in { + JsNumber(1000).convertTo[Byte] must throwA[DeserializationException] + } + "respect Byte lower bound" in { + JsNumber(-1000).convertTo[Byte] must throwA[DeserializationException] + } } "The ShortJsonFormat" should { @@ -96,6 +114,12 @@ class BasicFormatsSpec extends Specification with DefaultJsonProtocol { "convert a JsNumber to a Short" in { JsNumber(42).convertTo[Short] mustEqual 42 } + "respect Short upper bound" in { + JsNumber(100000).convertTo[Short] must throwA[DeserializationException] + } + "respect Short lower bound" in { + JsNumber(-100000).convertTo[Short] must throwA[DeserializationException] + } } "The BigDecimalJsonFormat" should {