Skip to content

Commit

Permalink
add number serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Sep 15, 2024
1 parent 6d2feae commit f82eab0
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions play-json/jvm/src/test/scala/play/api/libs/json/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package play.api.libs.json

import java.math.BigInteger
import java.util.Calendar
import java.util.Date
import java.util.TimeZone
Expand Down Expand Up @@ -477,6 +478,55 @@ class JsonSpec extends org.specs2.mutable.Specification {
fromJson[JsonNode](jsNum).map(_.toString).must_==(JsSuccess("12.345"))
}

"Serialize JsNumbers with integers correctly" in {
val numStrings = Seq(
"0",
"1",
"-1",
Int.MaxValue.toString,
Int.MinValue.toString,
Long.MaxValue.toString,
Long.MinValue.toString,
BigInteger.valueOf(Long.MaxValue).add(BigInteger.ONE).toString,
BigInteger.valueOf(Long.MinValue).add(BigInteger.valueOf(-1)).toString
)
numStrings.map { numString =>
val bigDec = new java.math.BigDecimal(numString)
Json.stringify(JsNumber(bigDec)).must_==(bigDec.toString)
}
}

"Serialize JsNumbers with decimal points correctly" in {
val numStrings = Seq(
"0.123",
"1.23456789",
"-1.23456789",
Float.MaxValue.toString,
Float.MinValue.toString,
Double.MaxValue.toString,
Double.MinValue.toString,
java.math.BigDecimal.valueOf(Double.MaxValue).add(java.math.BigDecimal.valueOf(1)).toString,
java.math.BigDecimal.valueOf(Double.MinValue).add(java.math.BigDecimal.valueOf(-1)).toString
)
numStrings.map { numString =>
val bigDec = new java.math.BigDecimal(numString)
Json.stringify(JsNumber(bigDec)).must_==(bigDec.toString)
}
}

"Serialize JsNumbers with e notation correctly" in {
val numStrings = Seq(
"1.23456789012345679012345679e999",
"-1.23456789012345679012345679e999",
"1.23456789012345679012345679e-999",
"-1.23456789012345679012345679e-999"
)
numStrings.map { numString =>
val bigDec = new java.math.BigDecimal(numString)
Json.stringify(JsNumber(bigDec)).must_==(bigDec.toString)
}
}

"parse from InputStream" in {
val js = Json.obj(
"key1" -> "value1",
Expand Down

0 comments on commit f82eab0

Please sign in to comment.