Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add number serialization tests #1077

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading