Encoding, Decoding, and Serialization

Serialize and deserialize instances of your types with implicit or customized encoding.

Topics

Custom Encoding and Decoding

Encoding and Decoding Custom Types

Make your data types encodable and decodable for compatibility with external representations such as JSON.

typealias Codable

A type that can convert itself into and out of an external representation.

Beta
protocol Encodable

A type that can encode itself to an external representation.

Beta
protocol Decodable

A type that can decode itself from an external representation.

Beta
protocol CodingKey

A type that can be used as a key for encoding and decoding.

Beta
struct CodingUserInfoKey

A user-defined key for providing context during encoding and decoding.

Beta

Encoders and Decoders

protocol Encoder

A type that can encode values into a native format for external representation.

Beta
protocol Decoder

A type that can decode values from a native format into in-memory representations.

Beta
enum EncodingError

An error that occurs during the encoding of a value.

Beta
enum DecodingError

An error that occurs during the decoding of a value.

Beta

Encoding Containers

protocol SingleValueEncodingContainer

A container that can support the storage and direct encoding of a single non-keyed value.

Beta
struct KeyedEncodingContainer

A concrete container that provides a view into an encoder’s storage, making the encoded properties of an encodable type accessible by keys.

Beta
protocol KeyedEncodingContainerProtocol

A type that provides a view into an encoder’s storage and is used to hold the encoded properties of an encodable type in a keyed manner.

Beta
protocol UnkeyedEncodingContainer

A type that provides a view into an encoder’s storage and is used to hold the encoded properties of an encodable type sequentially, without keys.

Beta

Decoding Containers

struct KeyedDecodingContainer

A concrete container that provides a view into an decoder’s storage, making the encoded properties of an decodable type accessible by keys.

Beta
protocol SingleValueDecodingContainer

A SingleValueDecodingContainer is a container which can support the storage and direct decoding of a single non-keyed value.

Beta
protocol KeyedDecodingContainerProtocol

A type that provides a view into a decoder’s storage and is used to hold the encoded properties of a decodable type in a keyed manner.

Beta
protocol UnkeyedDecodingContainer

A type that provides a view into a decoder’s storage and is used to hold the encoded properties of a decodable type sequentially, without keys.

Beta

See Also

Tools for Your Types

Basic Behaviors

Use your custom types in operations that depend on testing for equality or order and as members of sets and dictionaries.

Initialization with Literals

Allow values of your type to be expressed using different kinds of literals.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software