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


The example below shows how to encode an instance of a simple GroceryProduct type from a JSON object. The type adopts Codable so that it's encodable as JSON using a JSONEncoder instance.

struct GroceryProduct: Codable {
    var name: String
    var points: Int
    var description: String?

let pear = GroceryProduct(name: "Pear", points: 250, description: "A ripe pear.")

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(pear)
print(String(data: data, encoding: .utf8)!)

/* Prints:
   "name" : "Pear",
   "points" : 250,
   "description" : "A ripe pear."


First Steps


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 the readability, size, and element order of the encoded JSON object.

struct JSONEncoder.OutputFormatting

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

var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy

A value that determines how a type's coding keys are encoded as JSON keys.

enum JSONEncoder.KeyEncodingStrategy

The values that determine how a type's coding keys are encoded as JSON keys.

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


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.