Skip to content

Commit 4f0fb9c

Browse files
committed
SR-11410: Pretty printed, JSON encoded empty array or dictionary is different on Darwin vs Linux
- If the array or dictionary is empty, do not add indention spacing.
1 parent c326918 commit 4f0fb9c

File tree

2 files changed

+119
-5
lines changed

2 files changed

+119
-5
lines changed

Diff for: Foundation/JSONSerialization.swift

+13-4
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ private struct JSONWriter {
416416
writer("[")
417417
if pretty {
418418
writer("\n")
419-
incAndWriteIndent()
419+
incIndent()
420420
}
421421

422422
var first = true
@@ -425,10 +425,12 @@ private struct JSONWriter {
425425
first = false
426426
} else if pretty {
427427
writer(",\n")
428-
writeIndent()
429428
} else {
430429
writer(",")
431430
}
431+
if pretty {
432+
writeIndent()
433+
}
432434
try serializeJSON(elem)
433435
}
434436
if pretty {
@@ -442,7 +444,10 @@ private struct JSONWriter {
442444
writer("{")
443445
if pretty {
444446
writer("\n")
445-
incAndWriteIndent()
447+
incIndent()
448+
if dict.count > 0 {
449+
writeIndent()
450+
}
446451
}
447452

448453
var first = true
@@ -499,7 +504,11 @@ private struct JSONWriter {
499504
}
500505

501506
let indentAmount = 2
502-
507+
508+
mutating func incIndent() {
509+
indent += indentAmount
510+
}
511+
503512
mutating func incAndWriteIndent() {
504513
indent += indentAmount
505514
writeIndent()

Diff for: TestFoundation/TestJSONEncoder.swift

+106-1
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,115 @@ class TestJSONEncoder : XCTestCase {
9090
_testRoundTrip(of: person, expectedJSON: expectedJSON)
9191
}
9292

93-
func test_encodingOutputFormattingPrettyPrinted() {
93+
func test_encodingOutputFormattingPrettyPrinted() throws {
9494
let expectedJSON = "{\n \"name\" : \"Johnny Appleseed\",\n \"email\" : \"appleseed@apple.com\"\n}".data(using: .utf8)!
9595
let person = Person.testValue
9696
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted])
97+
98+
let encoder = JSONEncoder()
99+
if #available(OSX 10.13, *) {
100+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
101+
} else {
102+
// Fallback on earlier versions
103+
encoder.outputFormatting = [.prettyPrinted]
104+
}
105+
106+
let emptyArray: [Int] = []
107+
let arrayOutput = try encoder.encode(emptyArray)
108+
XCTAssertEqual(String.init(decoding: arrayOutput, as: UTF8.self), "[\n\n]")
109+
110+
let emptyDictionary: [String: Int] = [:]
111+
let dictionaryOutput = try encoder.encode(emptyDictionary)
112+
XCTAssertEqual(String.init(decoding: dictionaryOutput, as: UTF8.self), "{\n\n}")
113+
114+
struct DataType: Encodable {
115+
let array = [1, 2, 3]
116+
let dictionary: [String: Int] = [:]
117+
let emptyAray: [Int] = []
118+
let secondArray: [Int] = [4, 5, 6]
119+
let secondDictionary: [String: Int] = [ "one": 1, "two": 2, "three": 3]
120+
let singleElement: [Int] = [1]
121+
let subArray: [String: [Int]] = [ "array": [] ]
122+
let subDictionary: [String: [String: Int]] = [ "dictionary": [:] ]
123+
}
124+
125+
let dataOutput = try encoder.encode([DataType(), DataType()])
126+
XCTAssertEqual(String.init(decoding: dataOutput, as: UTF8.self), """
127+
[
128+
{
129+
"array" : [
130+
1,
131+
2,
132+
3
133+
],
134+
"dictionary" : {
135+
136+
},
137+
"emptyAray" : [
138+
139+
],
140+
"secondArray" : [
141+
4,
142+
5,
143+
6
144+
],
145+
"secondDictionary" : {
146+
"one" : 1,
147+
"three" : 3,
148+
"two" : 2
149+
},
150+
"singleElement" : [
151+
1
152+
],
153+
"subArray" : {
154+
"array" : [
155+
156+
]
157+
},
158+
"subDictionary" : {
159+
"dictionary" : {
160+
161+
}
162+
}
163+
},
164+
{
165+
"array" : [
166+
1,
167+
2,
168+
3
169+
],
170+
"dictionary" : {
171+
172+
},
173+
"emptyAray" : [
174+
175+
],
176+
"secondArray" : [
177+
4,
178+
5,
179+
6
180+
],
181+
"secondDictionary" : {
182+
"one" : 1,
183+
"three" : 3,
184+
"two" : 2
185+
},
186+
"singleElement" : [
187+
1
188+
],
189+
"subArray" : {
190+
"array" : [
191+
192+
]
193+
},
194+
"subDictionary" : {
195+
"dictionary" : {
196+
197+
}
198+
}
199+
}
200+
]
201+
""")
97202
}
98203

99204
func test_encodingOutputFormattingSortedKeys() {

0 commit comments

Comments
 (0)