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

JsonNumber.truncateToInt does not work correctly #334

Open
kushnerdmitry opened this issue Apr 2, 2020 · 1 comment
Open

JsonNumber.truncateToInt does not work correctly #334

kushnerdmitry opened this issue Apr 2, 2020 · 1 comment

Comments

@kushnerdmitry
Copy link

truncateToInt does not truncate Long number that is greater than Int.MaxValue to Int.MaxValue

Example(in scala repl)
Input:

import argonaut.JsonLong

val intMaxPlusOne = JsonLong(2147483648L).truncateToInt
val intMax = JsonLong(2147483647).truncateToInt
val long = JsonLong(100000000000L).truncateToInt

Output:

intMaxPlusOne: Int = -2147483648
intMax: Int = 2147483647
long: Int = 1215752192

Another example with codecs(also in scala repl):
Input:

import argonaut.{CodecJson, Json}
import argonaut.Argonaut._

val json = Json(
  "long" := 10002100000000L
)

case class DecodedInt(truncatedInt: Int)

implicit def DecodedIntCodec: CodecJson[DecodedInt] = 
  casecodec1(DecodedInt.apply, DecodedInt.unapply)("long")

val decoded = json.as[DecodedInt].value.get.truncatedInt

Output:

json: argonaut.Json = {"long":10002100000000}

defined class DecodedInt

DecodedIntCodec: argonaut.CodecJson[DecodedInt]

decoded: Int = -878832384

Scala version: 2.12.10
Argonaut version: 6.2.5

@k5ta
Copy link

k5ta commented Apr 2, 2020

truncateToInt implementation:

  • argonaut 6.1.x: def truncateToInt: Int = toDouble.toInt

  • argonaut 6.2.x: def truncateToInt: Int = toBigDecimal.toInt

I suppose that these changes were made due to the addition of lazy val toDouble in JsonNumber instead JsonDecimal. truncateToInt will work with truncateToDouble instead of toBigDecimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants