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

[SR-7119] Swift.Decodable doesn't work properly with big Int64 numbers on iOS 10 #4250

Open
swift-ci opened this issue Mar 5, 2018 · 2 comments

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Mar 5, 2018

Previous ID SR-7119
Radar None
Original Reporter vincent (JIRA User)
Type Bug
Environment

iOS 10
Swift 4.0

Additional Detail from JIRA
Votes 2
Component/s Foundation
Labels Bug, Codable
Assignee None
Priority Medium

md5: 7519541942cc0ceab4b15571af866560

Issue Description:

The "Parsed JSON number <1000000000000000070> does not fit in Int64." error occurs when running the following code on iOS 10.

struct MyDecodable: Decodable {
    var id: Int64
}

let str = "{\"id\":1000000000000000070}"
let data = str.data(using: .utf8)!
let decodable = try! JSONDecoder().decode(MyDecodable.self, from: data)

It seems that on iOS 10, for some big numbers that fit in an Int64, JSONSerialization creates not a normal NSNumber but an instance of NSDecimalNumber.
On iOS 11, a big number like the one above becomes an NSNumber so everything works fine.

iOS 10 probably can't be fixed but it seems to me that changing the code at https://github.com/apple/swift/blob/a62647f0685c3c832756d9f635cfe527d45037de/stdlib/public/SDK/Foundation/JSONEncoder.swift#L2100-L2103 to the code below would fix the problem.

let int64 = number.int64Value
let recreatedNumber = number is NSDecimalNumber ? NSDecimalNumber(value: int64) : NSNumber(value: int64)
guard recreatedNumber == number else {
    throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
}
@swift-ci
Copy link
Contributor Author

Comment by Fil Sviatoslav (JIRA)

Someone, when it's, will be fixed? (@belkadan?)

@swift-ci
Copy link
Contributor Author

Comment by Junkai Wu (JIRA)

Still not fixed...

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant