KeyEncodingStrategy
The strategy to use for automatically changing the value of keys before encoding.
- iOS
- 8.0+
- macOS
- 10.10+
- tvOS
- 9.0+
- watchOS
- 2.0+
enum KeyEncodingStrategy
The strategy to use for automatically changing the value of keys before encoding.
enum KeyEncodingStrategy
s20FoundationEssentials11JSONEncoderC19KeyEncodingStrategyO
What are these?1HO4
import FoundationEssentials
class JSONEncoder
JSONEncoder
facilitates the encoding of Encodable
values into JSON.
case convertToSnakeCase
Convert from “camelCaseKeys” to “snake_case_keys” before writing a key to JSON payload.
@preconcurrency case custom(([any CodingKey]) -> any CodingKey)
Provide a custom conversion to the key in the encoded JSON from the keys specified by the encoded types. The full path to the current encoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before encoding. If the result of the conversion is a duplicate key, then only one value will be present in the result.
case useDefaultKeys
Use the keys specified by each type. This is the default strategy.
init()
Initializes self
with default strategies.
var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get set }
The strategy to use in encoding binary data. Defaults to .base64
.
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get set }
The strategy to use in encoding dates. Defaults to .deferredToDate
.
var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get set }
The strategy to use for encoding keys. Defaults to .useDefaultKeys
.
var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get set }
The strategy to use in encoding non-conforming numbers. Defaults to .throw
.
var outputFormatting: JSONEncoder.OutputFormatting { get set }
The output format to produce. Defaults to []
.
var userInfo: [CodingUserInfoKey : Any] { get set }
Contextual user-provided information for use during encoding.
func encode<T>(_ value: T) throws -> Data where T : Encodable
Encodes the given top-level value and returns its JSON representation.
func encode<T>(_ value: T, configuration: T.EncodingConfiguration) throws -> Data where T : EncodableWithConfiguration
func encode<T, C>(_ value: T, configuration: C.Type) throws -> Data where T : EncodableWithConfiguration, C : EncodingConfigurationProviding, T.EncodingConfiguration == C.EncodingConfiguration
enum DataEncodingStrategy
The strategy to use for encoding Data
values.
enum DateEncodingStrategy
The strategy to use for encoding Date
values.
enum NonConformingFloatEncodingStrategy
The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
struct OutputFormatting
The formatting of the output JSON data.
protocol Sendable