Skip to content

Commit 837bfaa

Browse files
committed
Make JSON pretty-printing consistent with Darwin.
On macOS, pretty-printing JSON results in a space before the colon, e.g.: { "key" : 4 }
1 parent 77f821e commit 837bfaa

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Foundation/JSONSerialization.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private struct JSONWriter {
512512
} else {
513513
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "NSDictionary key must be NSString"])
514514
}
515-
pretty ? writer(": ") : writer(":")
515+
pretty ? writer(" : ") : writer(":")
516516
try serializeJSON(value)
517517
}
518518

TestFoundation/TestJSONSerialization.swift

+6
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ extension TestJSONSerialization {
989989
("test_serialize_dictionaryWithDecimal", test_serialize_dictionaryWithDecimal),
990990
("test_serializeDecimalNumberJSONObject", test_serializeDecimalNumberJSONObject),
991991
("test_serializeSortedKeys", test_serializeSortedKeys),
992+
("test_colonPrettyPrintingMatchesDarwin", test_colonPrettyPrintingMatchesDarwin),
992993
]
993994
}
994995

@@ -1479,6 +1480,11 @@ extension TestJSONSerialization {
14791480
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
14801481
}
14811482

1483+
func test_colonPrettyPrintingMatchesDarwin() {
1484+
let dictionary = ["key": 4]
1485+
XCTAssertEqual(try trySerialize(dictionary, options: .prettyPrinted), "{\n \"key\" : 4\n}")
1486+
}
1487+
14821488
fileprivate func createTestFile(_ path: String,_contents: Data) -> String? {
14831489
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
14841490
do {

0 commit comments

Comments
 (0)