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
codingPath
The coding path this encoder will start at
userInfo
additional information to provide context during encoding
translator
The translator the encoder will use to translate Metas.
storage
A empty CodingStorage that should be used to store metas.
-
Wraps a value to a meta using
metaSupplier.wrap
and callingvalue.encode
ifmetaSupplier.wrap
returned nil.If value conforms to
DirectlyEncodable
, or is anInt
,Int8
,Int16
,Int32
,Int64
,UInt
,UInt8
,UInt16
,UInt32
,UInt64
,Float
,Double
,Bool
orString
and not supported directly by meta supplier (this meansmetaSupplier.wrap
returns 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 : Encodable
Parameters
value
The value to wrap
key
The key at which
value
should 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 : CodingKey
Parameters
keyType
The key type of the KeyedEncodingContainer that should be returned.
reference
A reference to which the returned container should point to.
codingPath
The coding path the returned container should have.
createNewContainer
Whether 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 : CodingKey
Parameters
keyType
The key type of the KeyedEncodingContainer that should be returned.
reference
A reference to which the returned container should point to.
codingPath
The 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) -> UnkeyedEncodingContainer
Parameters
reference
A reference to which the returned container should point to.
codingPath
The coding path the returned container should have.
createNewContainer
Whether 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]) -> UnkeyedEncodingContainer
Parameters
reference
A reference to which the returned container should point to.
codingPath
The coding path the returned container should have.
-
Returns a new MetaKeyedEncodingContainer referencing reference.
Declaration
Swift
open func singleValueContainer(referencing reference: Reference, at codingPath: [CodingKey]) -> SingleValueEncodingContainer
Parameters
reference
A reference to which the returned container should point to.
codingPath
The 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
ExampleData
instance and theMetaSupplier
supportesExampleData
objects directly. Then callingdata.encode(to:)
will not fall back to that support, it will be encoded the wayExampleData
encodes itself.Throws
Aside from any errors that are thrown bymetaSupplier.wrap
and anyEncodingError
that has been thrown by another entity, this function will throwMetaEncoder.Errors.encodingHasNotSucceeded
if 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