Untitled diff

Created Diff never expires
175 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
532 lines
188 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
559 lines
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
//
//
// This source file is part of the Swift.org open source project
// This source file is part of the Swift.org open source project
//
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
// Licensed under Apache License v2.0 with Runtime Library Exception
//
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//


import CoreFoundation

//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// JSON Encoder
// JSON Encoder
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//


/// `JSONEncoder` facilitates the encoding of `Encodable` values into JSON.
/// `JSONEncoder` facilitates the encoding of `Encodable` values into JSON.
open class JSONEncoder {
open class JSONEncoder {
// MARK: Options
// MARK: Options


/// The formatting of the output JSON data.
/// The formatting of the output JSON data.
public struct OutputFormatting : OptionSet {
public struct OutputFormatting : OptionSet {
/// The format's default value.
/// The format's default value.
public let rawValue: UInt
public let rawValue: UInt


/// Creates an OutputFormatting value with the given raw value.
/// Creates an OutputFormatting value with the given raw value.
public init(rawValue: UInt) {
public init(rawValue: UInt) {
self.rawValue = rawValue
self.rawValue = rawValue
}
}


/// Produce human-readable JSON with indented output.
/// Produce human-readable JSON with indented output.
public static let prettyPrinted = OutputFormatting(rawValue: 1 << 0)
public static let prettyPrinted = OutputFormatting(rawValue: 1 << 0)


/// Produce JSON with dictionary keys sorted in lexicographic order.
/// Produce JSON with dictionary keys sorted in lexicographic order.
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)
}
}


/// The strategy to use for encoding `Date` values.
/// The strategy to use for encoding `Date` values.
public enum DateEncodingStrategy {
public enum DateEncodingStrategy {
/// Defer to `Date` for choosing an encoding. This is the default strategy.
/// Defer to `Date` for choosing an encoding. This is the default strategy.
case deferredToDate
case deferredToDate


/// Encode the `Date` as a UNIX timestamp (as a JSON number).
/// Encode the `Date` as a UNIX timestamp (as a JSON number).
case secondsSince1970
case secondsSince1970


/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
case millisecondsSince1970
case millisecondsSince1970


/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601
case iso8601


/// Encode the `Date` as a string formatted by the given formatter.
/// Encode the `Date` as a string formatted by the given formatter.
case formatted(DateFormatter)
case formatted(DateFormatter)


/// Encode the `Date` as a custom value encoded by the given closure.
/// Encode the `Date` as a custom value encoded by the given closure.
///
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Date, Encoder) throws -> Void)
case custom((Date, Encoder) throws -> Void)
}
}


/// The strategy to use for encoding `Data` values.
/// The strategy to use for encoding `Data` values.
public enum DataEncodingStrategy {
public enum DataEncodingStrategy {
/// Defer to `Data` for choosing an encoding.
/// Defer to `Data` for choosing an encoding.
case deferredToData
case deferredToData

/// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
/// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
case base64
case base64


/// Encode the `Data` as a custom value encoded by the given closure.
/// Encode the `Data` as a custom value encoded by the given closure.
///
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Data, Encoder) throws -> Void)
case custom((Data, Encoder) throws -> Void)
}
}


/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
public enum NonConformingFloatEncodingStrategy {
public enum NonConformingFloatEncodingStrategy {
/// Throw upon encountering non-conforming values. This is the default strategy.
/// Throw upon encountering non-conforming values. This is the default strategy.
case `throw`
case `throw`


/// Encode the values using the given representation strings.
/// Encode the values using the given representation strings.
case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
}
}


/// The strategy to use for automatically changing the value of keys before encoding.
public enum KeyEncodingStrategy {
/// Use the keys specified by each type. This is the default strategy.
case useDefaultKeys
/// Convert from "camelCaseKeys" to "snake_case_keys" before writing a key to JSON payload.
///
/// Capital characters are determined by testing membership in `CharacterSet.uppercaseLetters` and `CharacterSet.lowercaseLetters` (Unicode General Categories Lu and Lt).
/// The conversion to lower case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
///
/// Converting from camel case to snake case:
/// 1. Splits words at the boundary of lower-case to upper-case
/// 2. Inserts `_` between words
/// 3. Lowercases the entire string
/// 4. Preserves starting and ending `_`.
///
/// For example, `oneTwoThree` becomes `one_two_three`. `_oneTwoThree_` becomes `_one_two_three_`.
///
/// - Note: Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.
case convertToSnakeCase
/// 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 custom((_ codingPath: [CodingKey]) -> CodingKey)
fileprivate static func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
var words : [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
//
// myProperty -> my_property
// myURLProperty -> my_url_property
//
// We assume, per Swift naming conventions, that the first character of the key is lowercase.
var wordStart = stringKey.startIndex
var searchRange = stringKey.index(after: wordStart)..<stringKey.endIndex
// Find next uppercase character
while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters, options: [], range: searchRange) {
let untilUpperCase = wordStart..<upperCaseRange.lowerBound
words.append(untilUpperCase)
// Find next lowercase character
searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters, options: [], range: searchRange) else {
// There are no more lower case letters. Just end here.
wordStart = searchRange.lowerBound
break
}
// Is the next lowercase letter more than 1 after the uppercase? If so, we encountered a group of uppercase letters that we should treat as its own word
let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
if lowerCaseRange.lowerBound == nextCharacterAfterCapital {
// The next character after capital is a lower case character and therefore not a word boundary.
// Continue searching for the next upper case for the boundary.
wordStart = upperCaseRange.lowerBound
} else {
// There was a range of >1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
words.append(upperCaseRange.lowerBound..<beforeLowerIndex)
// Next word starts at the capital before the lowercase we just found
wordStart = beforeLowerIndex
}
searchRange = lowerCaseRange.upperBound..<searchRange.upperBound
}
words.append(wordStart..<searchRange.upperBound)
let result = words.map({ (range) in
return stringKey[range].lowercased()
}).joined(separator: "_")
return result
}
}

/// The output format to produce. Defaults to `[]`.
/// The output format to produce. Defaults to `[]`.
open var outputFormatting: OutputFormatting = []
open var outputFormatting: OutputFormatting = []


/// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
/// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate


/// The strategy to use in encoding binary data. Defaults to `.base64`.
/// The strategy to use in encoding binary data. Defaults to `.base64`.
open var dataEncodingStrategy: DataEncodingStrategy = .base64
open var dataEncodingStrategy: DataEncodingStrategy = .base64


/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw


/// The strategy to use for encoding keys. Defaults to `.useDefaultKeys`.
open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys
/// Contextual user-provided information for use during encoding.
/// Contextual user-provided information for use during encoding.
open var userInfo: [CodingUserInfoKey : Any] = [:]
open var userInfo: [CodingUserInfoKey : Any] = [:]


/// Options set on the top-level encoder to pass down the encoding hierarchy.
/// Options set on the top-level encoder to pass down the encoding hierarchy.
fileprivate struct _Options {
fileprivate struct _Options {
let dateEncodingStrategy: DateEncodingStrategy
let dateEncodingStrategy: DateEncodingStrategy
let dataEncodingStrategy: DataEncodingStrategy
let dataEncodingStrategy: DataEncodingStrategy
let nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy
let nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy
let keyEncodingStrategy: KeyEncodingStrategy
let userInfo: [CodingUserInfoKey : Any]
let userInfo: [CodingUserInfoKey : Any]
}
}


/// The options set on the top-level encoder.
/// The options set on the top-level encoder.
fileprivate var options: _Options {
fileprivate var options: _Options {
return _Options(dateEncodingStrategy: dateEncodingStrategy,
return _Options(dateEncodingStrategy: dateEncodingStrategy,
dataEncodingStrategy: dataEncodingStrategy,
dataEncodingStrategy: dataEncodingStrategy,
nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
keyEncodingStrategy: keyEncodingStrategy,
userInfo: userInfo)
userInfo: userInfo)
}
}


// MARK: - Constructing a JSON Encoder
// MARK: - Constructing a JSON Encoder


/// Initializes `self` with default strategies.
/// Initializes `self` with default strategies.
public init() {}
public init() {}


// MARK: - Encoding Values
// MARK: - Encoding Values


/// Encodes the given top-level value and returns its JSON representation.
/// Encodes the given top-level value and returns its JSON representation.
///
///
/// - parameter value: The value to encode.
/// - parameter value: The value to encode.
/// - returns: A new `Data` value containing the encoded JSON data.
/// - 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: `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.
/// - throws: An error if any value throws an error during encoding.
open func encode<T : Encodable>(_ value: T) throws -> Data {
open func encode<T : Encodable>(_ value: T) throws -> Data {
let encoder = _JSONEncoder(options: self.options)
let encoder = _JSONEncoder(options: self.options)
try value.encode(to: encoder)


guard let topLevel = try encoder.box_(value) else {
guard encoder.storage.count > 0 else {
throw EncodingError.invalidValue(value,
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
}
}


let topLevel = encoder.storage.popContainer()
if topLevel is NSNull {
if topLevel is NSNull {
throw EncodingError.invalidValue(value,
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as null JSON fragment."))
EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as null JSON fragment."))
} else if topLevel is NSNumber {
} else if topLevel is NSNumber {
throw EncodingError.invalidValue(value,
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as number JSON fragment."))
EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as number JSON fragment."))
} else if topLevel is NSString {
} else if topLevel is NSString {
throw EncodingError.invalidValue(value,
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as string JSON fragment."))
EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) encoded as string JSON fragment."))
}
}


let writingOptions = JSONSerialization.WritingOptions(rawValue: self.outputFormatting.rawValue)
let writingOptions = JSONSerialization.WritingOptions(rawValue: self.outputFormatting.rawValue)
do {
return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
return try JSONSerialization.data(withJSONObject: topLevel, options: writingOptions)
} catch {
throw EncodingError.invalidValue(value,
EncodingError.Context(codingPath: [], debugDescription: "Unable to encode the given top-level value to JSON.", underlyingError: error))
}
}
}
}
}


// MARK: - _JSONEncoder
// MARK: - _JSONEncoder


fileprivate class _JSONEncoder : Encoder {
fileprivate class _JSONEncoder : Encoder {
// MARK: Properties
// MARK: Properties


/// The encoder's storage.
/// The encoder's storage.
fileprivate var storage: _JSONEncodingStorage
fileprivate var storage: _JSONEncodingStorage


/// Options set on the top-level encoder.
/// Options set on the top-level encoder.
fileprivate let options: JSONEncoder._Options
fileprivate let options: JSONEncoder._Options


/// The path to the current point in encoding.
/// The path to the current point in encoding.
public var codingPath: [CodingKey]
public var codingPath: [CodingKey]


/// Contextual user-provided information for use during encoding.
/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey : Any] {
public var userInfo: [CodingUserInfoKey : Any] {
return self.options.userInfo
return self.options.userInfo
}
}


// MARK: - Initialization
// MARK: - Initialization


/// Initializes `self` with the given top-level encoder options.
/// Initializes `self` with the given top-level encoder options.
fileprivate init(options: JSONEncoder._Options, codingPath: [CodingKey] = []) {
fileprivate init(options: JSONEncoder._Options, codingPath: [CodingKey] = []) {
self.options = options
self.options = options
self.storage = _JSONEncodingStorage()
self.storage = _JSONEncodingStorage()
self.codingPath = codingPath
self.codingPath = codingPath
}
}


/// Returns whether a new element can be encoded at this coding path.
/// Returns whether a new element can be encoded at this coding path.
///
///
/// `true` if an element has not yet been encoded at this coding path; `false` otherwise.
/// `true` if an element has not yet been encoded at this coding path; `false` otherwise.
fileprivate var canEncodeNewValue: Bool {
fileprivate var canEncodeNewValue: Bool {
// Every time a new value gets encoded, the key it's encoded for is pushed onto the coding path (even if it's a nil key from an unkeyed container).
// Every time a new value gets encoded, the key it's encoded for is pushed onto the coding path (even if it's a nil key from an unkeyed container).
// At the same time, every time a container is requested, a new value gets pushed onto the storage stack.
// At the same time, every time a container is requested, a new value gets pushed onto the storage stack.
// If there are more values on the storage stack than on the coding path, it means the value is requesting more than one container, which violates the precondition.
// If there are more values on the storage stack than on the coding path, it means the value is requesting more than one container, which violates the precondition.
//
//
// This means that anytime something that can request a new container goes onto the stack, we MUST push a key onto the coding path.
// This means that anytime something that can request a new container goes onto the stack, we MUST push a key onto the coding path.
// Things which will not request containers do not need to have the coding path extended for them (but it doesn't matter if it is, because they will not reach here).
// Things which will not request containers do not need to have the coding path extended for them (but it doesn't matter if it is, because they will not reach here).
return self.storage.count == self.codingPath.count
return self.storage.count == self.codingPath.count
}
}


// MARK: - Encoder Methods
// MARK: - Encoder Methods
public func container<Key>(keyedBy: Key.Type) -> KeyedEncodingContainer<Key> {
public func container<Key>(keyedBy: Key.Type) -> KeyedEncodingContainer<Key> {
// If an existing keyed container was already requested, return that one.
// If an existing keyed container was already requested, return that one.
let topContainer: NSMutableDictionary
let topContainer: NSMutableDictionary
if self.canEncodeNewValue {
if self.canEncodeNewValue {
// We haven't yet pushed a container at this level; do so here.
// We haven't yet pushed a container at this level; do so here.
topContainer = self.storage.pushKeyedContainer()
topContainer = self.storage.pushKeyedContainer()
} else {
} else {
guard let container = self.storage.containers.last as? NSMutableDictionary else {
guard let container = self.storage.containers.last as? NSMutableDictionary else {
preconditionFailure("Attempt to push new keyed encoding container when already previously encoded at this path.")
preconditionFailure("Attempt to push new keyed encoding container when already previously encoded at this path.")
}
}


topContainer = container
topContainer = container
}
}


let container = _JSONKeyedEncodingContainer<Key>(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
let container = _JSONKeyedEncodingContainer<Key>(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
return KeyedEncodingContainer(container)
return KeyedEncodingContainer(container)
}
}


public func unkeyedContainer() -> UnkeyedEncodingContainer {
public func unkeyedContainer() -> UnkeyedEncodingContainer {
// If an existing unkeyed container was already requested, return that one.
// If an existing unkeyed container was already requested, return that one.
let topContainer: NSMutableArray
let topContainer: NSMutableArray
if self.canEncodeNewValue {
if self.canEncodeNewValue {
// We haven't yet pushed a container at this level; do so here.
// We haven't yet pushed a container at this level; do so here.
topContainer = self.storage.pushUnkeyedContainer()
topContainer = self.storage.pushUnkeyedContainer()
} else {
} else {
guard let container = self.storage.containers.last as? NSMutableArray else {
guard let container = self.storage.containers.last as? NSMutableArray else {
preconditionFailure("Attempt to push new unkeyed encoding container when already previously encoded at this path.")
preconditionFailure("Attempt to push new unkeyed encoding container when already previously encoded at this path.")
}
}


topContainer = container
topContainer = container
}
}


return _JSONUnkeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
return _JSONUnkeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
}
}


public func singleValueContainer() -> SingleValueEncodingContainer {
public func singleValueContainer() -> SingleValueEncodingContainer {
return self
return self
}
}
}
}


// MARK: - Encoding Storage and Containers
// MARK: - Encoding Storage and Containers


fileprivate struct _JSONEncodingStorage {
fileprivate struct _JSONEncodingStorage {
// MARK: Properties
// MARK: Properties


/// The container stack.
/// The container stack.
/// Elements may be any one of the JSON types (NSNull, NSNumber, NSString, NSArray, NSDictionary).
/// Elements may be any one of the JSON types (NSNull, NSNumber, NSString, NSArray, NSDictionary).
private(set) fileprivate var containers: [NSObject] = []
private(set) fileprivate var containers: [NSObject] = []


// MARK: - Initialization
// MARK: - Initialization


/// Initializes `self` with no containers.
/// Initializes `self` with no containers.
fileprivate init() {}
fileprivate init() {}


// MARK: - Modifying the Stack
// MARK: - Modifying the Stack


fileprivate var count: Int {
fileprivate var count: Int {
return self.containers.count
return self.containers.count
}
}


fileprivate mutating func pushKeyedContainer() -> NSMutableDictionary {
fileprivate mutating func pushKeyedContainer() -> NSMutableDictionary {
let dictionary = NSMutableDictionary()
let dictionary = NSMutableDictionary()
self.containers.append(dictionary)
self.containers.append(dictionary)
return dictionary
return dictionary
}
}


fileprivate mutating func pushUnkeyedContainer() -> NSMutableArray {
fileprivate mutating func pushUnkeyedContainer() -> NSMutableArray {
let array = NSMutableArray()
let array = NSMutableArray()
self.containers.append(array)
self.containers.append(array)
return array
return array
}
}


fileprivate mutating func push(container: NSObject) {
fileprivate mutating func push(container: NSObject) {
self.containers.append(container)
self.containers.append(container)
}
}


fileprivate mutating func popContainer() -> NSObject {
fileprivate mutating func popContainer() -> NSObject {
precondition(!self.containers.isEmpty, "Empty container stack.")
precondition(!self.containers.isEmpty, "Empty container stack.")
return self.containers.popLast()!
return self.containers.popLast()!
}
}
}
}


// MARK: - Encoding Containers
// MARK: - Encoding Containers


fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingContainerProtocol {
fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingContainerProtocol {
typealias Key = K
typealias Key = K


// MARK: Properties
// MARK: Properties


/// A reference to the encoder we're writing to.
/// A reference to the encoder we're writing to.
private let encoder: _JSONEncoder
private let encoder: _JSONEncoder


/// A reference to the container we're writing to.
/// A reference to the container we're writing to.
private let container: NSMutableDictionary
private let container: NSMutableDictionary


/// The path of coding keys taken to get to this point in encoding.
/// The path of coding keys taken to get to this point in encoding.
private(set) public var codingPath: [CodingKey]
private(set) public var codingPath: [CodingKey]


// MARK: - Initialization
// MARK: - Initialization


/// Initializes `self` with the given references.
/// Initializes `self` with the given references.
fileprivate init(referencing encoder: _JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableDictionary) {
fileprivate init(referencing encoder: _JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableDictionary) {
self.encoder = encoder
self.encoder = encoder
self.codingPath = codingPath
self.codingPath = codingPath
self.container = container
self.container = container
}
}


// MARK: - Coding Path Operations

private func _converted(_ key: CodingKey) -> CodingKey {
switch encoder.options.keyEncodingStrategy {
case .useDefaultKeys:
return key
case .convertToSnakeCase:
let newKeyString = JSONEncoder.KeyEncodingStrategy._convertToSnakeCase(key.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: key.intValue)
case .custom(let converter):
return converter(codingPath + [key])
}
}
// MARK: - KeyedEncodingContainerProtocol Methods
// MARK: - KeyedEncodingContainerProtocol Methods


public mutating func encodeNil(forKey key: Key) throws {
public mutating func encodeNil(forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = NSNull() }
self.container[_converted(key).stringValue] = NSNull()
public mutating func encode(_ value: Bool, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
}
public mutating func encode(_ value: Int, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
public mutating func encode(_ value: Bool, forKey key: Key) throws {
public mutating func encode(_ value: Int8, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
self.container[_converted(key).stringValue] = self.encoder.box(value)
public mutating func encode(_ value: Int16, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
}
public mutating func encode(_ value: Int32, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
public mutating func encode(_ value: Int, forKey key: Key) throws {
public mutating func encode(_ value: Int64, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
self.container[_converted(key).stringValue] = self.encoder.box(value)
public mutating func encode(_ value: UInt, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
}
public mutating func encode(_ value: UInt8, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
public mutating func encode(_ value: Int8, forKey key: Key) throws {
public mutating func encode(_ value: UInt16, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
self.container[_converted(key).stringValue] = self.encoder.box(value)
public mutating func encode(_ value: UInt32, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
}
public mutating func encode(_ value: UInt64, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
public mutating func encode(_ value: Int16, forKey key: Key) throws {
public mutating func encode(_ value: String, forKey key: Key) throws { self.container[key.stringValue._bridgeToObjectiveC()] = self.encoder.box(value) }
self.container[_converted(key).stringValue] = self.encoder.box(value)

}
public mutating func encode(_ value: Float, forKey key: Key) throws {
public mutating func encode(_ value: Int32, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: Int64, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: UInt, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: UInt8, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: UInt16, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: UInt32, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: UInt64, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: String, forKey key: Key) throws {
self.container[_converted(key).stringValue] = self.encoder.box(value)
}
public mutating func encode(_ value: Float, forKey key: Key) throws {
// Since the float may be invalid and throw, the coding path needs to contain this key.
// Since the float may be invalid and throw, the coding path needs to contain this key.
self.encoder.codingPath.append(key)
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
defer { self.encoder.codingPath.removeLast() }
self.container[_converted(key).stringValue] = try self.encoder.box(value)
self.container[key.stringValue._bridgeToObjectiveC()] = try self.encoder.box(value)
}
}


public mutating func encode(_ value: Double, forKey key: Key) throws {
public mutating func encode(_ value: Double, forKey key: Key) throws {
// Since the double may be invalid and throw, the coding path needs to contain this key.
// Since the double may be invalid and throw, the coding path needs to contain this key.
self.encoder.codingPath.append(key)
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
defer { self.encoder.codingPath.removeLast() }
self.container[_converted(key).stringValue] = try self.encoder.box(value)
self.container[key.stringValue._bridgeToObjectiveC()] = try self.encoder.box(value)
}
}


public mutating func encode<T : Encodable>(_ value: T, forKey key: Key) throws {
public mutating func encode<T : Encodable>(_ value: T, forKey key: Key) throws {
self.encoder.codingPath.append(key)
self.encoder.codingPath.append(key)
defer { self.encoder.codingPath.removeLast() }
defer { self.encoder.codingPath.removeLast() }
self.container[_converted(key).stringValue] = try self.encoder.box(value)
self.container[key.stringValue._bridgeToObjectiveC()] = try self.encoder.box(value)
}
}


public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
let dictionary = NSMutableDictionary()
let dictionary = NSMutableDictionary()
self.container[_converted(key).stringValue] = dictionary
self.container[key.stringValue._bridgeToObjectiveC()] = dictionary


self.codingPath.append(key)
self.codingPath.append(key)
defer { self.codingPath.removeLast() }
defer { self.codingPath.removeLast() }


let container = _JSONKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
let container = _JSONKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
return KeyedEncodingContainer(container)
return KeyedEncodingContainer(container)
}
}


public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
let array = NSMutableArray()
let array = NSMutableArray()
self.container[_converted(key).stringValue] = array
self.container[key.stringValue._bridgeToObjectiveC()] = array


self.codingPath.append(key)
self.codingPath.append(key)
defer { self.codingPath.removeLast() }
defer { self.codingPath.removeLast() }
return _JSONUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
return _JSONUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
}
}


public mutating func superEncoder() -> Encoder {
public mutating func superEncoder() -> Encoder {
return _JSONReferencingEncoder(referencing: self.encoder, key: _JSONKey.super, convertedKey: _converted(_JSONKey.super), wrapping: self.container)
return _JSONReferencingEncoder(referencing: self.encoder, at: _JSONKey.super, wrapping: self.container)
}
}


public mutating func superEncoder(forKey key: Key) -> Encoder {
public mutating func superEncoder(forKey key: Key) -> Encoder {
return _JSONReferencingEncoder(referencing: self.encoder, key: key, convertedKey: _converted(key), wrapping: self.container)
return _JSONReferencingEncoder(referencing: self.encoder, at: key, wrapping: self.container)
}
}
}
}


fileprivate struct _JSONUnkeyedEncodingContainer : UnkeyedEncodingContainer {
fileprivate struct _JSONUnkeyedEncodingContainer : UnkeyedEncodingContainer {
// MARK: Properties
// MARK: Properties


/// A reference to the encoder we're writing to.
/// A reference to the encoder we're writing to.
private let encoder: _JSONEncoder
private let encoder: _JSONEncoder


/// A reference to the container we're writing to.
/// A reference to the container we're writing to.
private let container: NSMutableArray
private let container: NSMutableArray


/// The path of coding keys taken to get to this point in encoding.
/// The path of coding keys taken to get to this point in encoding.
private(set) public var codingPath: [CodingKey]
private(set) public var codingPath: [CodingKey]


/// The number of elements encoded into the container.
/// The number of elements encoded into the container.
public var count: Int {
public var count: Int {
return self.container.count
return self.container.count
}
}


// MARK: - Initialization
// MARK: - Initialization


/// Initializes `self` with the given references.
/// Initializes `self` with the given references.
fileprivate init(referencing encoder: _JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {
fileprivate init(referencing encoder: _JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {
self.encoder = encoder
self.encoder = encoder
self.codingPath = codingPath
self.codingPath = codingPath
self.container = container
self.container = container
}
}


// MARK: - UnkeyedEncodingContainer Methods
// MARK: - UnkeyedEncodingContainer Methods


public mutating func encodeNil() throws { self.container.add(NSNull()) }
public mutating func encodeNil() throws { self.container.add(NSNull()) }
public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt32) throws { self.container.add(se
public mutating func encode(_ value: UInt32) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: UInt64) throws { self.container.add(self.encoder.box(value)) }
public mutating func encode(_ value: String) throws { self.container.add(self.encoder.box(value)) }

public mutating func encode(_ value: Float) throws {
// Since the float may be invalid and throw, the coding path needs to contain this key.
self.encoder.codingPath.append(_JSONKey(index: self.count))
defer { self.encoder.codingPath.removeLast() }
self.container.add(try self.encoder.box(value))
}

public mutating func encode(_ value: Double) throws {
// Since the double may be invalid and throw, the coding path needs to contain this key.
self.encoder.codingPath.append(_JSONKey(index: self.count))
defer { self.encoder.codingPath.removeLast() }
self.container.add(try self.encoder.box(value))
}

public mutating func encode<T : Encodable>(_ value: T) throws {
self.encoder.codingPath.append(_JSONKey(index: self.count))
defer { self.encoder.codingPath.removeLast() }
self.container.add(try self.encoder.box(value))
}

public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> {
let dictionary = NSMutableDictionary()
self.container.add(dictionary)

// self.count - 1 to accommodate the fact that we just pushed a container.
self.codingPath.append(_JSONKey(index: self.count - 1))
defer { self.codingPath.removeLast() }
let container = _JSONKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
return KeyedEncodingContainer(container)
}

public mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
let array = NSMutableArray()
self.container.add(array)

// self.count - 1 to accommodate the fact that we just pushed a container.
self.codingPath.append(_JSONKey(index: self.count - 1))
defer { self.codingPath.removeLast() }

return _JSONUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
}

public mutating func superEncoder() -> Encoder {
return _JSONReferencingEncoder(referencing: self.encoder, at: self.container.count, wrapping: self.container)
}
}

extension _JSONEncoder : SingleValueEncodingContainer {
// MARK: - SingleValueEncodingContainer Methods

fileprivate func assertCanEncodeNewValue() {
precondition(self.canEncodeNewValue, "Attempt to encode value through single value container when previously value already encoded.")
}

public func encodeNil() throws {
assertCanEncodeNewValue()
self.storage.push(container: NSNull())
}

public func encode(_ value: Bool) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Int) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Int8) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Int16) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Int32) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Int64) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: UInt) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: UInt8) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: UInt16) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: UInt32) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: UInt64) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: String) throws {
assertCanEncodeNewValue()
self.storage.push(container: box(value))
}

public func encode(_ value: Float) throws {
assertCanEncodeNewValue()
try self.storage.push(container: box(value))
}

public func encode(_ value: Double) throws {
assertCanEncodeNewValue()
try self.storage.push(container: box(value))
}

public func encode<T : Encodable>(_ value: T) throws {
assertCanEncodeNewValue()
try self.storage.push(container: box(value))
}
}

// MARK: - Concrete Value Representations

extension _JSONEncoder {
/// Returns the given value boxed in a container appropriate for pushing onto the container stack.
fileprivate func box(_ value: Bool) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: Int) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: Int8) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: Int16) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: Int32) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: Int64) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: UInt) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: UInt8) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: UInt16) -> NSObject { return NSNumber(value: value) }
fileprivate func box(_ value: UInt32) -> NSObject { return NSNumber(value: value) }
filepriv