MetaDecoder
open class MetaDecoder : Decoder
A Decoder
that decodes from a Meta
instead from a concrete format.
-
Declaration
Swift
public let userInfo: [CodingUserInfoKey : Any]
-
Declaration
Swift
open var codingPath: [CodingKey]
-
A StorageAccessor to this decoder’s private storage
Declaration
Swift
open var storage: CodingStorage
-
Initalizes a new MetaDecoder with the given values.
Declaration
Swift
public init(at codingPath: [CodingKey] = [], with userInfo: [CodingUserInfoKey : Any] = [:], unwrapper: Unwrapper, storage: CodingStorage = LinearCodingStack())
Parameters
codingPath
The coding path this decoder will start at.
userInfo
additional information to provide context during decoding.
unwrapper
The
Unwrapper
the decoder will use to unwrap metas. If options contains the.dynamicallyUnwindMetaTree
option, this unwrapper needs to conform toContainerUnwrapper
additionally. If it does not, this initalizer will crash.storage
A empty CodingStorage that should be used to store metas.
-
Unwraps a meta to a real value using
unwrapper.unwrap
and callingtype.init(from:)
ifunwrapper.unwrap
returned nil.Throws
DecodingError and CodingStorageErrorDeclaration
Swift
open func unwrap<D: Decodable>(_ meta: Meta? = nil, toType type: D.Type, for key: CodingKey? = nil) throws -> D
Parameters
meta
the Meta to unwrap, if you pass nil, the meta at this decoders current coding path will be used.
type
The type to unwrap to
key
The key at which meta should be decoded. This decoders coding path is extended with this key.
-
Checks whether the given meta represents a nil meta. Tries to dynamically unwrap meta if specified.
Declaration
Swift
open func representsNil(meta passedMeta: Meta) -> Bool
-
Create a new KeyedDecodingContainer for the given meta, if it is a DecodingKeyedContainerMeta.
If it is not, throw DecodingError.typeMissmatch.
Declaration
Swift
public func container<Key>(keyedBy keyType: Key.Type, for passedMeta: Meta, at codingPath: [CodingKey]) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
-
Declaration
Swift
open func decodingContainer<Key>(keyedBy keyType: Key.Type, for keyedMeta: DecodingKeyedContainerMeta, at codingPath: [CodingKey]) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
-
Create a new UnkeyedDecodingContainer for the given meta, if it is a DecodingUnkeyedContainerMeta.
If it is not, throw DecodingError.typeMissmatch.
Declaration
Swift
public func unkeyedContainer(for passedMeta: Meta, at codingPath: [CodingKey]) throws -> UnkeyedDecodingContainer
-
Declaration
Swift
open func unkeyedDecodingContainer(for unkeyedMeta: DecodingUnkeyedContainerMeta, at codingPath: [CodingKey]) throws -> UnkeyedDecodingContainer
-
Create a new SingleValueDecodingContainer for the given meta.
Declaration
Swift
open func singleValueContainer(for meta: Meta, at codingPath: [CodingKey]) throws -> SingleValueDecodingContainer
-
Creates a new decoder that decodes the given meta. This decoder can for example be used as super decoder.
Declaration
Swift
open func decoder(for meta: Meta, at codingPath: [CodingKey]) throws -> Decoder
-
Creates a new decoder with the given storage.
This method serves as delegate for decoder(for:, at:)‘s default implementation.
Declaration
Swift
open func decoderImplementation(storage: CodingStorage, at codingPath: [CodingKey]) throws -> Decoder
-
Declaration
Swift
func container<Key>(keyedBy keyType: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
-
Declaration
Swift
func unkeyedContainer() throws -> UnkeyedDecodingContainer
-
Declaration
Swift
func singleValueContainer() throws -> SingleValueDecodingContainer
-
Decodes a value of type D from the given meta tree.
Use this method rather than directly calling Decodable.init(from:). init(from:) will not detect types that are directly supported by the unwrapper.
There are some conditions
meta
and the metas contained inmeta
(refered to as “the meta tree”) must meet:- Any metas that should be seen as nil values by the decoder (for which
decodeNil
on e.g.KeyedDecodingContainer
should return true) must conform toNilMeta
. If you didn’t set the
MetaDecoder.Options.dynamicallyUnwindMetaTree
option on this decoder, all metas that can be seen as keyed or unkeyed containers must conform toDecodingKeyedContainerMeta
/DecodingUnkeyedContainerMeta
, othwise these containers can not be detected. Alternatively consider setting.dynamicallyUnwindMetaTree
and implementing the required container unwraping inunwrap
of yourUnwrapper
implementation.Throws
Aside from any errors that are thrown of unwrapper.unwrap and anyDecodingError
that has been thrown by another entity, this function will throwMetaDecoder.Errors.decodingHasNotSucceeded
if the decoding could not succeed. In general, this error will never be thrown in code running unter the debug (and not the release) configuration. Instead the call will fail with an assertion failure. Such an error usually indicates an invalid decoder implementation or a missuse of a decoder.
Declaration
Swift
func decode<D>(type: D.Type, from meta: Meta) throws -> D where D : Decodable
Parameters
type
The type that should decoded.
meta
The meta tree the decoder should decode from.
- Any metas that should be seen as nil values by the decoder (for which
-
Declaration
Swift
enum Errors : Error