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-7037] Inconsistent behaviour deserialising JSON using Foundation on Linux. #3733

Closed
johnno1962 opened this issue Feb 20, 2018 · 2 comments

Comments

@johnno1962
Copy link
Contributor

Previous ID SR-7037
Radar None
Original Reporter @johnno1962
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 27570ce488d8bfb4ade92694a1fb2e55

duplicates:

  • SR-1610 Cross-platform NSNumber coercion behavior

Issue Description:

The following example can be used to test JSONSerializer and works on Darwin and Linux

let input = [10.5]

let json = try! JSONSerialization.data(withJSONObject: input)

print(String(data: json, encoding: .utf8)!)

let array = try! JSONSerialization.jsonObject(with: json)

print((array as! [Any])[0] as! Double)

This same code fails however on the last line when run on Linux if the Double value in the input array happens to be a whole number e.g 10.0 giving the error:

Could not cast value of type 'Swift.Int' (0x10286e430) to 'Swift.Double' (0x10286b948).

This is because the JSON will be serialised giving the value as simply 10 and on deserialization this is represented as an Int which can not be downcast to a Double. On Darwin this is not a problem as NSNumber is used rather than Any as the generic value container for numbers and this can always be downcast to Double.

Potential solutions are being discussed in this PR

@johnno1962
Copy link
Contributor Author

Slightly different problem.

@johnno1962
Copy link
Contributor Author

Resolved with the “bridging work” in 4.2 for Linux and #1634

@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
This issue was closed.
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