kakaopensource / KakaJSON

Fast conversion between JSON and model in Swift.
MIT License
1.16k stars 125 forks source link

Hi,使用的时候发现:解析大的 Int 数据会失败 #54

Open Junnor opened 3 years ago

Junnor commented 3 years ago
1

` static func number( value: Any, _ type: Any.Type) -> NumberValue? { guard let str = _numberString(value) else { return nil } guard let decimal = Decimal(string: str) else { return nil }

    // digit
    if let digitType = type as? DigitValue.Type {
        // 大 Int 数据解析永远是 18035768958676992
        // let x = 18035768958676993,             // 解析失败, 解析结果成 18035768958676992
        // let y = 18035768958676992              // 解析成功
        return Double("\(decimal)")
            .flatMap { NSNumber(value: $0) }
            .flatMap { digitType.init(truncating: $0) }
    }

    // decimal number
    if type is NSDecimalNumber.Type {
        return NSDecimalNumber(decimal: decimal)
    }

    // decimal
    if type is Decimal.Type { return decimal }

    // other
    return Double("\(decimal)").flatMap { NSNumber(value: $0) }
}

`

Junnor commented 3 years ago

Double("(decimal)") 的这个解析由于精度问题会返回不一样的值

zhuangxq commented 3 years ago
image

跟 #46 一样的, 我是这样改的,可以参考一下。

Junnor commented 3 years ago

@zhuangxq 你好,感谢提供解决方法。 我目前的解决办法是直接把model里面的 Int 设置成String了,😂,那样精度问题也就不存在了。