You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: 27570ce488d8bfb4ade92694a1fb2e55
duplicates:
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
The text was updated successfully, but these errors were encountered: