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-7195] JSONSerialization encodes Double differently on Darwin and Linux #3727
Comments
cc @itaiferber. There may have been a dup of this already. |
@itaiferber, I am still seeing this issue in Swift 4.2 on Linux. Could you take a look at this? |
On Linux, JSONSerialization uses Note that Ubuntu18 currently uses ICU-60 so upgrading ICU to a later version would change the output format of JSONSerialization anyway but not in a way guaranteed to match Darwin. |
Looks like floating-point numbers are printed out via let string = CFNumberFormatterCreateStringWithNumber(nil, _numberformatter, num._cfObject)._swiftObject
writer(string) with private lazy var _numberformatter: CFNumberFormatter = {
let formatter: CFNumberFormatter
formatter = CFNumberFormatterCreate(nil, CFLocaleCopyCurrent(), kCFNumberFormatterNoStyle)
CFNumberFormatterSetProperty(formatter, kCFNumberFormatterMaxFractionDigits, NSNumber(value: 15))
CFNumberFormatterSetFormat(formatter, "0.###############"._cfObject)
return formatter
}() This indeed doesn't match what we do on Darwin, which is cc @spevans — you've worked more on |
@itaiferber, I just tested this with Swift 3.1.1 on Linux and issues exists there as well. So this not a regression in Swift 4. @spevans, Using String to encode Double/Float should be a good solution for now. This ensures round-trip correctness, which is more important than having the exact same JSON on both platforms, I guess. |
Fixed in #1722 |
Environment
Swift 4.0.3, macOS 10.13.3 and Ubuntu 16.04
Additional Detail from JIRA
md5: e38af2c3f6bfc9b1fe10fcfba2bf9763
relates to:
1.1
Issue Description:
`JSONSerialization` and thus `JSONEncoder` encode floating point values in a different way on Linux than on Darwin. This can cause a loss of precision and decoding a different value than the one that was encoded on Linux.
Running the following code demonstrates the issue.
Swift 4.0.3 on macOS 10.13.3 produces the following output and encodes and decodes all values as expected.
And Swift 4.0.3 on Ubuntu 16.04. Both input double values are encoded as 7.70870099662 in the resulting JSON.
The text was updated successfully, but these errors were encountered: