Description
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