KeyDecodingStrategy
The strategy to use for automatically changing the value of keys before decoding.
- iOS
- 8.0+
- macOS
- 10.10+
- tvOS
- 9.0+
- watchOS
- 2.0+
enum KeyDecodingStrategy
The strategy to use for automatically changing the value of keys before decoding.
enum KeyDecodingStrategy
s20FoundationEssentials11JSONDecoderC19KeyDecodingStrategyO
What are these?9J7KC
import FoundationEssentials
class JSONDecoder
JSONDecoder
facilitates the decoding of JSON into semantic Decodable
types.
case convertFromSnakeCase
Convert from “snake_case_keys” to “camelCaseKeys” before attempting to match a key with the one specified by each type.
@preconcurrency case custom(([any CodingKey]) -> any CodingKey)
Provide a custom conversion from the key in the encoded JSON to the keys specified by the decoded types. The full path to the current decoding 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 decoding. If the result of the conversion is a duplicate key, then only one value will be present in the container for the type to decode from.
case useDefaultKeys
Use the keys specified by each type. This is the default strategy.
init()
Initializes self
with default strategies.
var allowsJSON5: Bool { get set }
Set to true
to allow parsing of JSON5. Defaults to false
.
var assumesTopLevelDictionary: Bool { get set }
Set to true
to assume the data is a top level Dictionary (no surrounding “{ }” required). Defaults to false
. Compatible with both JSON5 and non-JSON5 mode.
var dataDecodingStrategy: JSONDecoder.DataDecodingStrategy { get set }
The strategy to use in decoding binary data. Defaults to .base64
.
var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy { get set }
The strategy to use in decoding dates. Defaults to .deferredToDate
.
var keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy { get set }
The strategy to use for decoding keys. Defaults to .useDefaultKeys
.
var nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy { get set }
The strategy to use in decoding non-conforming numbers. Defaults to .throw
.
var userInfo: [CodingUserInfoKey : Any] { get set }
Contextual user-provided information for use during decoding.
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable
Decodes a top-level value of the given type from the given JSON representation.
func decode<T>(_ type: T.Type, from data: Data, configuration: T.DecodingConfiguration) throws -> T where T : DecodableWithConfiguration
func decode<T, C>(_ type: T.Type, from data: Data, configuration: C.Type) throws -> T where T : DecodableWithConfiguration, C : DecodingConfigurationProviding, T.DecodingConfiguration == C.DecodingConfiguration
enum DataDecodingStrategy
The strategy to use for decoding Data
values.
enum DateDecodingStrategy
The strategy to use for decoding Date
values.
enum NonConformingFloatDecodingStrategy
The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
protocol Sendable