Class

JSONEncoder

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

Topics

Encoding

init()

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.

struct JSONEncoder.OutputFormatting

The output formatting options that determine the readability, size, and element order of the encoded JSON.

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 a date 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

JSON

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.