Skip to content

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