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

Fix Json.Num.apply for doubles and java.math.BigInteger #1299

Merged
merged 1 commit into from
Feb 10, 2025
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
6 changes: 3 additions & 3 deletions zio-json/shared/src/main/scala/zio/json/ast/ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,10 @@ object Json {
if (value.isValidLong) apply(value.toLong)
else new Json.Num(new java.math.BigDecimal(value.bigInteger))
def apply(value: java.math.BigInteger): Num =
if (value.bitCount < 64) apply(value.longValue)
if (value.bitLength < 64) apply(value.longValue)
else new Json.Num(new java.math.BigDecimal(value))
def apply(value: Float): Num = new Num(new java.math.BigDecimal(value.toString))
def apply(value: Double): Num = new Num(new java.math.BigDecimal(value))
def apply(value: Float): Num = new Num(new java.math.BigDecimal(SafeNumbers.toString(value)))
def apply(value: Double): Num = new Num(new java.math.BigDecimal(SafeNumbers.toString(value)))

implicit val decoder: JsonDecoder[Num] = new JsonDecoder[Num] {
def unsafeDecode(trace: List[JsonError], in: RetractReader): Num =
Expand Down
21 changes: 21 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/ast/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@ import zio.json._
import zio.test.Assertion._
import zio.test._

import java.math.BigInteger

object JsonSpec extends ZIOSpecDefault {

val spec: Spec[Environment, Any] =
suite("Json")(
suite("apply")(
test("Num()") {
assertTrue(Json.Num(0).toString == "0") &&
assertTrue(Json.Num(0.0).toString == "0.0") &&
assertTrue(Json.Num(1.0).toString == "1.0") &&
assertTrue(Json.Num(-0.0).toString == "0.0") &&
assertTrue(Json.Num(-1.0).toString == "-1.0") &&
assertTrue(Json.Num(7: Byte).toString == "7") &&
assertTrue(Json.Num(777: Short).toString == "777") &&
assertTrue(Json.Num(123456789).toString == "123456789") &&
assertTrue(Json.Num(1.2345678f).toString == "1.2345678") &&
assertTrue(Json.Num(1.2345678901234567).toString == "1.2345678901234567") &&
assertTrue(Json.Num(1234567890123456789L).toString == "1234567890123456789") &&
assertTrue(Json.Num(BigInteger.valueOf(1234567890123456789L)).toString == "1234567890123456789") &&
assertTrue(Json.Num(new BigInteger("12345678901234567890")).toString == "12345678901234567890") &&
assertTrue(Json.Num(BigInt(1234567890123456789L)).toString == "1234567890123456789") &&
assertTrue(Json.Num(BigInt("12345678901234567890")).toString == "12345678901234567890") &&
assertTrue(Json.Num(BigDecimal(1234567890123456789L)).toString == "1234567890123456789") &&
assertTrue(Json.Num(BigDecimal("12345678901234567890")).toString == "12345678901234567890")
},
test("Bool()") {
assertTrue(Json.Bool.True eq Json.Bool(true)) &&
assertTrue(Json.Bool.False eq Json.Bool(false))
Expand Down