MetaSupplier
public protocol MetaSupplier
The core interface to specify MetaEncoder’s encoding. Implement this protocol to set encoding “primities” (these are usually the types you can represent directly in your serialization format) and container implementations.
-
keyedContainerMeta()
Default implementationReturns an empty
EncodingKeyedContainerMeta
.This method lets you use a custom container type (e.g. NSDictionary), if required. However this method has a default implementation returning a new Dictionary
every time it is called. Attention
You should implement this method or keep the default implementation of it, even if you do not support keyed containers in you framework, because the documentation of Encodable specifies an empty keyed container as substitute if a value did not encode anything. Do not use precondition or assert in this method for that reason. Also keyed containers are an important part of manyencode(to:)
implementations (especially the default generated ones). Therefor it could make sence to implement some transition from keyed containers to other containers you provide. A custom implementation ofEncodingKeyedContainerMeta
is the right place for this.Default Implementation
Declaration
Swift
func keyedContainerMeta() -> EncodingKeyedContainerMeta
Return Value
A new, empty EncodingKeyedMetaContainer.
-
unkeyedContainerMeta()
Default implementationReturns an empty
EncodingUnkeyedMetaContainer
.This method lets you use a custom container type (e.g. NSArray), if required. However this method has a default implementation returning a new Array every time it is called.
Default Implementation
Declaration
Swift
func unkeyedContainerMeta() -> EncodingUnkeyedContainerMeta
Return Value
A new, empty EncodingUnkeyedMetaContainer.
-
Creates, sets and returns a
Meta
for the given value, or returns nil if the value is not supported, respectively can not be represented in your underlying format.This method will be asked to return a Meta for an instance of the type NilMarker from MetaSerialization if encoding a nil value is requested. Return nil if you don’t support nil values. If you support nil values, you are invited to use NilMarker itself as Meta, but you may of course use any meta implementation here.
You may not call Encodables
encode(to:)
in this method directly, neighter one of the container methods. Calling these methods will conflict with the encoding ofvalue
, since you can not access the metas you created with those calls. Callencoder.encode
instead and nest your container requests inside a utility type if necessary.This method is called very frequently.
Declaration
Swift
func wrap<T>(_ value: T, for encoder: MetaEncoder) throws -> Meta? where T : Encodable
Parameters
value
The value for which a wrapping meta should be returned.
encoder
The encoder that requests the wrap. You should use this encoder if you need to encode values yourself inside wrap or to get the current coding path (this an array of coding keys visited up to value in the order they were visited taken up to
value
). The coding path is ment to be used in errors you may throw (e.g.EncodingError
).Return Value
nil or a
Meta
which wrappes value.