Navigation Menu

Skip to content
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-13414] JSONEncoder sortedKeys doesn't apply to Dictionary values #3972

Open
swift-ci opened this issue Aug 18, 2020 · 2 comments
Open

[SR-13414] JSONEncoder sortedKeys doesn't apply to Dictionary values #3972

swift-ci opened this issue Aug 18, 2020 · 2 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-13414
Radar rdar://problem/67492239
Original Reporter efirestone (JIRA User)
Type Bug
Environment

MacOS Catalina 10.15.6

Xcode 11.6 (11E708) which I think is Swift 5.2

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 2f4be0e23a94302b63fa5a92e04fe1e2

Issue Description:

The OutputFormatting.sortedKeys option for JSONEncoder doesn't apply to keys in a nested dictionary field, which makes it impossible to write deterministic tests against the resulting JSON output.

import Foundation


struct Foo: Encodable {
    var name = "baz"
    var dictionary: [Int32: String] = [3: "foo", 2: "bar"]
}


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


let foo = Foo()
let result = String(data: try! encoder.encode(foo), encoding: .utf8)!


print(result)
// Outputs: {"dictionary":[3,"foo",2,"bar"],"name":"baz"}
@typesanitizer
Copy link

@swift-ci create

@YOCKOW
Copy link
Collaborator

YOCKOW commented Sep 12, 2020

FWIW, as described in SR-7788, a dictionary with non-String/non-Int key is encoded as an array of key-value pairs. JSONEncoder cannot know it was a dictionary or an array.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants