Skip to content

Commit

Permalink
Fix Json.Num.apply for floats, doubles and java.math.BigInteger (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Feb 10, 2025
1 parent 92955bb commit 2a0c55a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
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

0 comments on commit 2a0c55a

Please sign in to comment.