Skip to content

Commit 692a893

Browse files
author
Itai Ferber
committed
Allow superDecoder to wrap null values
* Update {JSON,PropertyList}Decoder to allow superDecoder()s to wrap null value so current implementations of collections can decode contained objects from nil
1 parent bf1d2a7 commit 692a893

File tree

2 files changed

+2
-24
lines changed

2 files changed

+2
-24
lines changed

stdlib/public/SDK/Foundation/JSONEncoder.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,12 +1281,7 @@ fileprivate struct _JSONKeyedDecodingContainer<K : CodingKey> : KeyedDecodingCon
12811281

12821282
private func _superDecoder(forKey key: CodingKey) throws -> Decoder {
12831283
return try self.decoder.with(pushedKey: key) {
1284-
guard let value = self.container[key.stringValue] else {
1285-
throw DecodingError.keyNotFound(key,
1286-
DecodingError.Context(codingPath: self.codingPath,
1287-
debugDescription: "Cannot get superDecoder() -- no value found for key \"\(key.stringValue)\""))
1288-
}
1289-
1284+
let value = self.container[key.stringValue]
12901285
return _JSONDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
12911286
}
12921287
}
@@ -1631,12 +1626,6 @@ fileprivate struct _JSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
16311626
}
16321627

16331628
let value = self.container[self.currentIndex]
1634-
guard !(value is NSNull) else {
1635-
throw DecodingError.valueNotFound(Decoder.self,
1636-
DecodingError.Context(codingPath: self.codingPath,
1637-
debugDescription: "Cannot get superDecoder() -- found null value instead."))
1638-
}
1639-
16401629
self.currentIndex += 1
16411630
return _JSONDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
16421631
}

stdlib/public/SDK/Foundation/PlistEncoder.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,12 +1053,7 @@ fileprivate struct _PlistKeyedDecodingContainer<K : CodingKey> : KeyedDecodingCo
10531053

10541054
private func _superDecoder(forKey key: CodingKey) throws -> Decoder {
10551055
return try self.decoder.with(pushedKey: key) {
1056-
guard let value = self.container[key.stringValue] else {
1057-
throw DecodingError.valueNotFound(Decoder.self,
1058-
DecodingError.Context(codingPath: self.codingPath,
1059-
debugDescription: "Cannot get superDecoder() -- no value found for key \"\(key.stringValue)\""))
1060-
}
1061-
1056+
let value = self.container[key.stringValue]
10621057
return _PlistDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
10631058
}
10641059
}
@@ -1402,12 +1397,6 @@ fileprivate struct _PlistUnkeyedDecodingContainer : UnkeyedDecodingContainer {
14021397
}
14031398

14041399
let value = self.container[self.currentIndex]
1405-
guard !(value is NSNull) else {
1406-
throw DecodingError.valueNotFound(Decoder.self,
1407-
DecodingError.Context(codingPath: self.codingPath,
1408-
debugDescription: "Cannot get superDecoder() -- found null value instead."))
1409-
}
1410-
14111400
self.currentIndex += 1
14121401
return _PlistDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
14131402
}

0 commit comments

Comments
 (0)