Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/scala/spray/json/BasicFormats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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)
}
}

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)
}
}
Expand All @@ -59,15 +59,15 @@ 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)
}
}

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)
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/scala/spray/json/BasicFormatsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down