Skip to content

JSONEncoder.OutputFormatting.sortedKeys behavior differs between Darwin and non-Darwin implementation #4702

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

Closed
fumoboy007 opened this issue Feb 10, 2023 · 0 comments · Fixed by #4757

Comments

@fumoboy007
Copy link
Contributor

The behavior of JSONEncoder.OutputFormatting.sortedKeys differs between the Darwin and non-Darwin implementation of Foundation.

A developer may rely on this formatting option to make a test deterministic. However, because the behavior differs across platforms, the test is not portable.

Test Case

import Foundation

let dictionary = [
  "a": "a",
  "B": "b"
]

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = [.sortedKeys]

let jsonBytes = try! jsonEncoder.encode(dictionary)
let jsonString = String(decoding: jsonBytes, as: UTF8.self)

print(jsonString)

Darwin Output

{"a":"a","B":"b"}

Non-Darwin Output

{"B":"b","a":"a"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant