Skip to content

Commit 19e3979

Browse files
authored
JSONEncoder: fixed _asDirectArrayEncodable function (#1593)
* _asDirectArrayEncodable fixed * add array encoding benchmark
1 parent c921a20 commit 19e3979

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

Benchmarks/Benchmarks/JSON/JSONBenchmark.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ let benchmarks = {
8686
let twitterData = try! _Data(contentsOf: twitterPath!)
8787
let twitter = try! _JSONDecoder().decode(TwitterArchive.self, from: twitterData)
8888

89+
let arrayPath = path(forResource: "array.json")
90+
let arrayData = try! _Data(contentsOf: arrayPath!)
91+
let array = try! _JSONDecoder().decode([Int].self, from: arrayData)
92+
8993
Benchmark("Canada-decodeFromJSON") { benchmark in
9094
let result = try _JSONDecoder().decode(FeatureCollection.self, from: canadaData)
9195
blackHole(result)
@@ -105,5 +109,9 @@ let benchmarks = {
105109
let result = try _JSONEncoder().encode(twitter)
106110
blackHole(result)
107111
}
108-
}
109112

113+
Benchmark("IntArray-encodeToJSON") { benchmark in
114+
let result = try _JSONEncoder().encode(array)
115+
blackHole(result)
116+
}
117+
}

Benchmarks/Benchmarks/JSON/Resources/array.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ private extension __JSONEncoder {
12161216
return .number(decimal.description)
12171217
} else if !options.keyEncodingStrategy.isDefault, let encodable = value as? _JSONStringDictionaryEncodableMarker {
12181218
return try self.wrap(encodable as! [String:Encodable], for: additionalKey)
1219-
} else if let array = _asDirectArrayEncodable(value, for: additionalKey) {
1219+
} else if let array = _asDirectArrayEncodable(value) {
12201220
if options.outputFormatting.contains(.prettyPrinted) {
12211221
let (bytes, lengths) = try array.individualElementRepresentation(encoder: self, additionalKey)
12221222
return .directArray(bytes, lengths: lengths)
@@ -1246,36 +1246,36 @@ private extension __JSONEncoder {
12461246
return encoder.takeValue()
12471247
}
12481248

1249-
func _asDirectArrayEncodable<T: Encodable>(_ value: T, for additionalKey: (some CodingKey)? = _CodingKey?.none) -> _JSONDirectArrayEncodable? {
1250-
return if let array = _specializingCast(array, to: [Int8].self) {
1249+
func _asDirectArrayEncodable<T: Encodable>(_ value: T) -> _JSONDirectArrayEncodable? {
1250+
return if let array = _specializingCast(value, to: [Int8].self) {
12511251
array
1252-
} else if let array = _specializingCast(array, to: [Int16].self) {
1252+
} else if let array = _specializingCast(value, to: [Int16].self) {
12531253
array
1254-
} else if let array = _specializingCast(array, to: [Int32].self) {
1254+
} else if let array = _specializingCast(value, to: [Int32].self) {
12551255
array
1256-
} else if let array = _specializingCast(array, to: [Int64].self) {
1256+
} else if let array = _specializingCast(value, to: [Int64].self) {
12571257
array
1258-
} else if let array = _specializingCast(array, to: [Int128].self) {
1258+
} else if let array = _specializingCast(value, to: [Int128].self) {
12591259
array
1260-
} else if let array = _specializingCast(array, to: [Int].self) {
1260+
} else if let array = _specializingCast(value, to: [Int].self) {
12611261
array
1262-
} else if let array = _specializingCast(array, to: [UInt8].self) {
1262+
} else if let array = _specializingCast(value, to: [UInt8].self) {
12631263
array
1264-
} else if let array = _specializingCast(array, to: [UInt16].self) {
1264+
} else if let array = _specializingCast(value, to: [UInt16].self) {
12651265
array
1266-
} else if let array = _specializingCast(array, to: [UInt32].self) {
1266+
} else if let array = _specializingCast(value, to: [UInt32].self) {
12671267
array
1268-
} else if let array = _specializingCast(array, to: [UInt64].self) {
1268+
} else if let array = _specializingCast(value, to: [UInt64].self) {
12691269
array
1270-
} else if let array = _specializingCast(array, to: [UInt128].self) {
1270+
} else if let array = _specializingCast(value, to: [UInt128].self) {
12711271
array
1272-
} else if let array = _specializingCast(array, to: [UInt].self) {
1272+
} else if let array = _specializingCast(value, to: [UInt].self) {
12731273
array
1274-
} else if let array = _specializingCast(array, to: [String].self) {
1274+
} else if let array = _specializingCast(value, to: [String].self) {
12751275
array
1276-
} else if let array = _specializingCast(array, to: [Float].self) {
1276+
} else if let array = _specializingCast(value, to: [Float].self) {
12771277
array
1278-
} else if let array = _specializingCast(array, to: [Double].self) {
1278+
} else if let array = _specializingCast(value, to: [Double].self) {
12791279
array
12801280
} else {
12811281
nil

0 commit comments

Comments
 (0)