An object that encodes instances of a data type as JSON objects.


Creating Encoders


Creates a new, reusable JSON encoder with the default formatting settings and encoding strategies.

func encode<T>(T)

Returns a JSON-encoded representation of the value you supply.

Customizing Encoding

var outputFormatting: JSONEncoder.OutputFormatting

A value that determines whether the encoded JSON output is compact or pretty-printed.

enum JSONEncoder.OutputFormatting

The output formats that determine whether encoded JSON is optimized for readability or size.

var userInfo: [CodingUserInfoKey : Any]

A dictionary you use to customize the encoding process by providing contextual information.

Encoding Dates

var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy

The strategy used when encoding dates as part of a JSON object.

enum JSONEncoder.DateEncodingStrategy

The formatting strategies available for formatting dates when encoding them as JSON.

Encoding Raw Data

var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy

The strategy that an encoder uses to encode raw data.

enum JSONEncoder.DataEncodingStrategy

The strategies for encoding raw data.

Encoding Exceptional Numbers

var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy

The strategy used by an encoder when it encounters exceptional floating-point values.

enum JSONEncoder.NonConformingFloatEncodingStrategy

The strategies for encoding nonconforming floating-point numbers, also known as IEEE 754 exceptional values.

See Also


Using JSON with Custom Types

Demonstrates approaches for encoding and decoding different kinds of JSON in Swift.

class JSONDecoder

An object that decodes instances of a data type from JSON objects.

class JSONSerialization

An object that converts between JSON and the equivalent Foundation objects.

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