Skip to content

JsonNumber.truncateToInt does not work correctly #334

Description

@kushnerdmitry

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions