MetaEncoder
open class MetaEncoder : Encoder
An Encoder that constucts a Meta instead of encoding to a concrete format.
-
Declaration
Swift
public let userInfo: [CodingUserInfoKey : Any] -
Declaration
Swift
open var codingPath: [CodingKey]
-
The translator used to get and finally translate Metas
Declaration
Swift
public let metaSupplier: MetaSupplier
-
This encoder’s storage
Declaration
Swift
open var storage: CodingStorage
-
Initalizes a new MetaEncoder with the given values.
Declaration
Swift
public init(at codingPath: [CodingKey] = [], with userInfo: [CodingUserInfoKey : Any] = [:], metaSupplier: MetaSupplier, storage: CodingStorage = LinearCodingStack() )Parameters
codingPathThe coding path this encoder will start at
userInfoadditional information to provide context during encoding
translatorThe translator the encoder will use to translate Metas.
storageA empty CodingStorage that should be used to store metas.
-
Wraps a value to a meta using
metaSupplier.wrapand callingvalue.encodeifmetaSupplier.wrapreturned nil.If value conforms to
DirectlyEncodable, or is anInt,Int8,Int16,Int32,Int64,UInt,UInt8,UInt16,UInt32,UInt64,Float,Double,BoolorStringand not supported directly by meta supplier (this meansmetaSupplier.wrapreturns nil), this method throwsEncodingError.invalidValue. This is required because otherwise, these types would endlessly try to encode themselves into single value containers.Throws
EncodingError and CodingStorageErrorDeclaration
Swift
open func wrap<E>(_ value: E, at key: CodingKey? = nil) throws -> Meta where E : EncodableParameters
valueThe value to wrap
keyThe key at which
valueshould be encoded. This encoders coding path is extended with this key. If key is nil, the coding path isn’t extended.
-
Returns a new KeyedEncodingContainer referencing reference.
Declaration
Swift
public func container<Key>(keyedBy keyType: Key.Type, referencing reference: Reference, at codingPath: [CodingKey], createNewContainer: Bool = true) -> KeyedEncodingContainer<Key> where Key : CodingKeyParameters
keyTypeThe key type of the KeyedEncodingContainer that should be returned.
referenceA reference to which the returned container should point to.
codingPathThe coding path the returned container should have.
createNewContainerWhether to create a new contaienr and set reference.meta to it, or not. Default is yes.
-
Returns a new KeyedEncodingContainer referencing reference.
This method serves as delegate for container(keyedBy:, referencing, at:, createNewContainer).
Declaration
Swift
open func encodingContainer<Key>(keyedBy keyType: Key.Type, referencing reference: Reference, at codingPath: [CodingKey]) -> KeyedEncodingContainer<Key> where Key : CodingKeyParameters
keyTypeThe key type of the KeyedEncodingContainer that should be returned.
referenceA reference to which the returned container should point to.
codingPathThe coding path the returned container should have.
-
Returns a new UnkeyedEncodingContainer referencing reference.
Declaration
Swift
public func unkeyedContainer(referencing reference: Reference, at codingPath: [CodingKey], createNewContainer: Bool = true) -> UnkeyedEncodingContainerParameters
referenceA reference to which the returned container should point to.
codingPathThe coding path the returned container should have.
createNewContainerWhether to create a new contaienr and set reference.meta to it. Default is true.
-
Returns a new UnkeyedEncodingContainer referencing reference.
This method serves as delegate for container(keyedBy:, referencing, at:, createNewContainer).
Declaration
Swift
open func unkeyedEncodingContainer(referencing reference: Reference, at codingPath: [CodingKey]) -> UnkeyedEncodingContainerParameters
referenceA reference to which the returned container should point to.
codingPathThe coding path the returned container should have.
-
Returns a new MetaKeyedEncodingContainer referencing reference.
Declaration
Swift
open func singleValueContainer(referencing reference: Reference, at codingPath: [CodingKey]) -> SingleValueEncodingContainerParameters
referenceA reference to which the returned container should point to.
codingPathThe coding path the returned container should have.
-
Creates a new encoder that encodes to the given reference. This encoder can for example be used as super encoder.
Declaration
Swift
open func encoder(referencing reference: Reference, at codingPath: [CodingKey]) -> Encoder -
Creates a new encoder with the given storage.
This method serves as delegate for encoder(referencing:, at:)‘s default implementation.
Declaration
Swift
open func encoderImplementation(storage: CodingStorage, at codingPath: [CodingKey]) -> Encoder -
Declaration
Swift
func container<Key>(keyedBy keyType: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey -
Declaration
Swift
func unkeyedContainer() -> UnkeyedEncodingContainer -
Declaration
Swift
func singleValueContainer() -> SingleValueEncodingContainer -
Encodes the given value to a meta tree.
Use this method rather than directly calling encode(to:) of
Encodable. encode(to:) will not detect types in the first place that are directly supported by theMetaSupplier.Example: If data is a
ExampleDatainstance and theMetaSuppliersupportesExampleDataobjects directly. Then callingdata.encode(to:)will not fall back to that support, it will be encoded the wayExampleDataencodes itself.Throws
Aside from any errors that are thrown bymetaSupplier.wrapand anyEncodingErrorthat has been thrown by another entity, this function will throwMetaEncoder.Errors.encodingHasNotSucceededif the encoding 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 encoder implementation or a missuse of an encoder.Declaration
Swift
func encode<E>(_ value: E) throws -> Meta where E : Encodable -
Declaration
Swift
enum Errors : Error
View on GitHub
MetaEncoder Class Reference