ensure context exists for JSONDecoder within extension method
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
9e9421f66a
commit
071cb16870
@ -28,6 +28,9 @@ extension Data {
|
|||||||
extension JSONDecoder {
|
extension JSONDecoder {
|
||||||
/// Decodes a top-level value of the given type from the given JSON representation, and adds the delegate object if provided.
|
/// Decodes a top-level value of the given type from the given JSON representation, and adds the delegate object if provided.
|
||||||
func decode<T>(_ type: T.Type, from data: Data, delegateObject: DelegateObject?) throws -> T where T : Decodable {
|
func decode<T>(_ type: T.Type, from data: Data, delegateObject: DelegateObject?) throws -> T where T : Decodable {
|
||||||
|
if context == nil {
|
||||||
|
add(value: DecodingContext(), for: .contextKey)
|
||||||
|
}
|
||||||
if let delegateObject = delegateObject {
|
if let delegateObject = delegateObject {
|
||||||
add(value: delegateObject, for: .delegateObjectKey)
|
add(value: delegateObject, for: .delegateObjectKey)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user