-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4757 from woxtu/sorted-keys
Fix `JSONEncoder.OutputFormatting.sortedKeys` behavior
- Loading branch information
Showing
2 changed files
with
58 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,27 +235,73 @@ class TestJSONEncoder : XCTestCase { | |
""") | ||
} | ||
|
||
func test_encodingOutputFormattingSortedKeys() { | ||
let expectedJSON = "{\"email\":\"[email protected]\",\"name\":\"Johnny Appleseed\"}".data(using: .utf8)! | ||
let person = Person.testValue | ||
func test_encodingOutputFormattingSortedKeys() throws { | ||
let expectedJSON = try XCTUnwrap(""" | ||
{"2":"2","7":"7","25":"25","alice":"alice","bob":"bob","Charlie":"Charlie","中国":"中国","日本":"日本","韓国":"韓国"} | ||
""".data(using: .utf8)) | ||
let testValue = [ | ||
"2": "2", | ||
"25": "25", | ||
"7": "7", | ||
"alice": "alice", | ||
"bob": "bob", | ||
"Charlie": "Charlie", | ||
"日本": "日本", | ||
"中国": "中国", | ||
"韓国": "韓国", | ||
] | ||
#if os(macOS) || DARWIN_COMPATIBILITY_TESTS | ||
if #available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) { | ||
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys]) | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = .sortedKeys | ||
let payload = try encoder.encode(testValue) | ||
XCTAssertEqual(expectedJSON, payload) | ||
} | ||
#else | ||
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys]) | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = .sortedKeys | ||
let payload = try encoder.encode(testValue) | ||
XCTAssertEqual(expectedJSON, payload) | ||
#endif | ||
} | ||
|
||
func test_encodingOutputFormattingPrettyPrintedSortedKeys() { | ||
let expectedJSON = "{\n \"email\" : \"[email protected]\",\n \"name\" : \"Johnny Appleseed\"\n}".data(using: .utf8)! | ||
let person = Person.testValue | ||
func test_encodingOutputFormattingPrettyPrintedSortedKeys() throws { | ||
let expectedJSON = try XCTUnwrap(""" | ||
{ | ||
"2" : "2", | ||
"7" : "7", | ||
"25" : "25", | ||
"alice" : "alice", | ||
"bob" : "bob", | ||
"Charlie" : "Charlie", | ||
"中国" : "中国", | ||
"日本" : "日本", | ||
"韓国" : "韓国" | ||
} | ||
""".data(using: .utf8)) | ||
let testValue = [ | ||
"2": "2", | ||
"25": "25", | ||
"7": "7", | ||
"alice": "alice", | ||
"bob": "bob", | ||
"Charlie": "Charlie", | ||
"日本": "日本", | ||
"中国": "中国", | ||
"韓国": "韓国", | ||
] | ||
#if os(macOS) || DARWIN_COMPATIBILITY_TESTS | ||
if #available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) { | ||
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys]) | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | ||
let payload = try encoder.encode(testValue) | ||
XCTAssertEqual(expectedJSON, payload) | ||
} | ||
#else | ||
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys]) | ||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | ||
let payload = try encoder.encode(testValue) | ||
XCTAssertEqual(expectedJSON, payload) | ||
#endif | ||
} | ||
|
||
|