MetaDecoder

open class MetaDecoder : Decoder

A Decoder that decodes from a Meta instead from a concrete format.

general properties

  • Declaration

    Swift

    public let userInfo: [CodingUserInfoKey : Any]
  • Declaration

    Swift

    open var codingPath: [CodingKey]

translator

storage

  • A StorageAccessor to this decoder’s private storage

    Declaration

    Swift

    open var storage: CodingStorage

initalization

  • 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 to ContainerUnwrapper additionally. If it does not, this initalizer will crash.

    storage

    A empty CodingStorage that should be used to store metas.

upwrap

  • Unwraps a meta to a real value using unwrapper.unwrap and calling type.init(from:) if unwrapper.unwrap returned nil.

    Throws

    DecodingError and CodingStorageError

    Declaration

    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.

nil values

  • 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

container(for: meta) methods

  • 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

super decoder

  • 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 in meta (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 to NilMeta.
    • 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 to DecodingKeyedContainerMeta/DecodingUnkeyedContainerMeta, othwise these containers can not be detected. Alternatively consider setting .dynamicallyUnwindMetaTree and implementing the required container unwraping in unwrap of your Unwrapper implementation.

      Throws

      Aside from any errors that are thrown of unwrapper.unwrap and any DecodingError that has been thrown by another entity, this function will throw MetaDecoder.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.

  • Declaration

    Swift

    enum Errors : Error