Skip to content

Fix JSONEncoder.OutputFormatting.sortedKeys behavior #4757

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

Merged
merged 2 commits into from
Dec 12, 2023

Conversation

woxtu
Copy link
Contributor

@woxtu woxtu commented Jun 10, 2023

This PR tries to fix JSONEncoder.OutputFormatting.sortedKeys behavior. Resolves #4702.

I estimated string comparison options by trying the following code on macOS.

let testValue = [
    "2": "2",
    "25": "25",
    "7": "7",
    "alice": "alice",
    "bob": "bob",
    "Charlie": "Charlie",
    "日本": "日本",
    "中国": "中国",
    "韓国": "韓国",
]

let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys

let data = try! encoder.encode(testValue)
let string = String(data: data, encoding: .utf8)!
print(string)
// {"2":"2","7":"7","25":"25","alice":"alice","bob":"bob","Charlie":"Charlie","中国":"中国","日本":"日本","韓国":"韓国"}

print(testValue.sorted { $0.key.compare($1.key, options: [.caseInsensitive, .diacriticInsensitive, .forcedOrdering, .numeric, .widthInsensitive]) == .orderedAscending }.map(\.key))
// ["2", "7", "25", "alice", "bob", "Charlie", "中国", "日本", "韓国"]

@YOCKOW
Copy link
Member

YOCKOW commented Dec 11, 2023

@swift-ci Please test

@YOCKOW YOCKOW merged commit 1b514e4 into swiftlang:main Dec 12, 2023
@woxtu woxtu deleted the sorted-keys branch March 30, 2024 11:15
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 this pull request may close these issues.

JSONEncoder.OutputFormatting.sortedKeys behavior differs between Darwin and non-Darwin implementation
2 participants