ensure context exists for JSONDecoder within extension method

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-05-24 12:07:03 -05:00
parent 9e9421f66a
commit 071cb16870

View File

@ -28,6 +28,9 @@ extension Data {
extension JSONDecoder {
/// 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 {
if context == nil {
add(value: DecodingContext(), for: .contextKey)
}
if let delegateObject = delegateObject {
add(value: delegateObject, for: .delegateObjectKey)
}