Skip to content

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

Closed
@fumoboy007

Description

@fumoboy007

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"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions