KeyedDecodingContainer
extension KeyedDecodingContainer
Extensions for KeyedDecodingContainer to support decoding of dynamic dictionaries and arrays
with Sendable values, enabling flexible JSON parsing with unknown structures.
-
Decodes a dictionary with string keys and
Sendablevalues for a given key.Throws
DecodingErrorif the value cannot be decodedDeclaration
Swift
public func decode(_ type: [String : any Sendable].Type, forKey key: K) throws -> [String : any Sendable]Parameters
typeThe type of dictionary to decode
keyThe coding key to decode from
Return Value
A dictionary with string keys and
Sendablevalues -
Decodes an array of dictionaries with string keys and
Sendablevalues for a given key.Throws
DecodingErrorif the value cannot be decodedDeclaration
Swift
public func decode(_ type: [[String : any Sendable]].Type, forKey key: K) throws -> [[String : any Sendable]]Parameters
typeThe type of array of dictionaries to decode
keyThe coding key to decode from
Return Value
An array of dictionaries with string keys and
Sendablevalues -
Decodes a dictionary with string keys and
Sendablevalues if present for a given key.Throws
DecodingErrorif the value cannot be decodedDeclaration
Swift
public func decodeIfPresent(_ type: [String : any Sendable].Type, forKey key: K) throws -> [String : any Sendable]?Parameters
typeThe type of dictionary to decode
keyThe coding key to decode from
Return Value
An optional dictionary with string keys and
Sendablevalues, ornilif the key is not present or the value is null -
Decodes an array of
Sendablevalues for a given key.Throws
DecodingErrorif the value cannot be decodedDeclaration
Swift
public func decode(_ type: [any Sendable].Type, forKey key: K) throws -> [any Sendable]Parameters
typeThe type of array to decode
keyThe coding key to decode from
Return Value
An array of
Sendablevalues -
Decodes an array of
Sendablevalues if present for a given key.Throws
DecodingErrorif the value cannot be decodedDeclaration
Swift
public func decodeIfPresent(_ type: [any Sendable].Type, forKey key: K) throws -> [any Sendable]?Parameters
typeThe type of array to decode
keyThe coding key to decode from
Return Value
An optional array of
Sendablevalues, ornilif the key is not present or the value is null -
Decodes a dictionary with string keys and
Sendablevalues from the current container.This method iterates through all keys in the container and attempts to decode each value as a Bool, String, Int, Double, nested dictionary, or nested array.
Throws
DecodingErrorif the container cannot be processedDeclaration
Swift
public func decode(_ type: [String : any Sendable].Type) throws -> [String : any Sendable]Parameters
typeThe type of dictionary to decode
Return Value
A dictionary with string keys and
Sendablevalues containing all successfully decoded values
View on GitHub