One Logo Pool Ball
Foundation · IOS API REFERENCE

JSONEncoder

class · iOS 8.0+ · SDK 27.1

class JSONEncoder

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 <doc://com.apple.documentation/documentation/swift/codable> so that it's encodable as JSON using a ``JSONEncoder`` instance. ```swift 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." } */ ```

Members (14)

get · iOS 8.0+

outputFormatting

TypeScript
get outputFormatting(): JSONEncoder_OutputFormatting
Swift declaration
var outputFormatting: JSONEncoder.OutputFormatting { get set }

A value that determines the readability, size, and element order of the encoded JSON object. Defaults to `[]`.

set · iOS 8.0+

outputFormatting

TypeScript
set outputFormatting(value: JSONEncoder_OutputFormatting)
Swift declaration
var outputFormatting: JSONEncoder.OutputFormatting { get set }

A value that determines the readability, size, and element order of the encoded JSON object. Defaults to `[]`.

get · iOS 8.0+

keyEncodingStrategy

TypeScript
get keyEncodingStrategy(): JSONEncoder_KeyEncodingStrategy
Swift declaration
var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get set }

A value that determines how to encode a type's coding keys as JSON keys. Defaults to `.useDefaultKeys`.

set · iOS 8.0+

keyEncodingStrategy

TypeScript
set keyEncodingStrategy(value: JSONEncoder_KeyEncodingStrategy)
Swift declaration
var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get set }

A value that determines how to encode a type's coding keys as JSON keys. Defaults to `.useDefaultKeys`.

get · iOS 8.0+

dataEncodingStrategy

TypeScript
get dataEncodingStrategy(): JSONEncoder_DataEncodingStrategy
Swift declaration
var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get set }

The strategy that an encoder uses to encode raw data. Defaults to `.base64`.

set · iOS 8.0+

dataEncodingStrategy

TypeScript
set dataEncodingStrategy(value: JSONEncoder_DataEncodingStrategy)
Swift declaration
var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get set }

The strategy that an encoder uses to encode raw data. Defaults to `.base64`.

get · iOS 8.0+

dateEncodingStrategy

TypeScript
get dateEncodingStrategy(): JSONEncoder_DateEncodingStrategy
Swift declaration
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get set }

The strategy used when encoding dates as part of a JSON object. Defaults to `.deferredToDate`.

set · iOS 8.0+

dateEncodingStrategy

TypeScript
set dateEncodingStrategy(value: JSONEncoder_DateEncodingStrategy)
Swift declaration
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get set }

The strategy used when encoding dates as part of a JSON object. Defaults to `.deferredToDate`.

get · iOS 8.0+

nonConformingFloatEncodingStrategy

TypeScript
get nonConformingFloatEncodingStrategy(): JSONEncoder_NonConformingFloatEncodingStrategy
Swift declaration
var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get set }

The strategy used by an encoder when it encounters exceptional floating-point values. Defaults to `.throw`.

set · iOS 8.0+

nonConformingFloatEncodingStrategy

TypeScript
set nonConformingFloatEncodingStrategy(value: JSONEncoder_NonConformingFloatEncodingStrategy)
Swift declaration
var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get set }

The strategy used by an encoder when it encounters exceptional floating-point values. Defaults to `.throw`.

method · iOS 8.0+

encode

TypeScript
encode(value: number): ArrayBuffer
Swift declaration
func encode<T>(_ value: T) throws -> Data where T : Encodable

Returns a JSON-encoded representation of the value you supply. - parameter value: The value to encode. - returns: A new `Data` value containing the encoded JSON data. - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`. - throws: An error if any value throws an error during encoding.

get · iOS 8.0+

userInfo

TypeScript
get userInfo(): NativeMap<SDKHandle, SDKHandle>
Swift declaration
@preconcurrency var userInfo: [CodingUserInfoKey : any Sendable] { get set }

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

set · iOS 8.0+

userInfo

TypeScript
set userInfo(value: NativeMap<SDKHandle, SDKHandle>)
Swift declaration
@preconcurrency var userInfo: [CodingUserInfoKey : any Sendable] { get set }

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

init · iOS 8.0+

init

TypeScript
static init(): JSONEncoder
Swift declaration
init()

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

Skipped declarations

2 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

JSONEncoder.encode(_:configuration:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

JSONEncoder.encode(_:configuration:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

All Foundation types →