FirebaseDataEncoder.swift 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. // This file is derived from swift/stdlib/public/Darwin/Foundation/JSONEncoder.swift
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // This source file is part of the Swift.org open source project
  5. //
  6. // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
  7. // Licensed under Apache License v2.0 with Runtime Library Exception
  8. //
  9. // See https://swift.org/LICENSE.txt for license information
  10. // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  11. //
  12. //===----------------------------------------------------------------------===//
  13. import Foundation
  14. public protocol StructureCodingPassthroughTypeResolver {
  15. static func isPassthroughType<T>(_ t: T) -> Bool
  16. }
  17. private struct NoPassthroughTypes: StructureCodingPassthroughTypeResolver {
  18. static func isPassthroughType<T>(_ t: T) -> Bool {
  19. return false
  20. }
  21. }
  22. public protocol StructureCodingUncodedUnkeyed {}
  23. extension DecodingError {
  24. /// Returns a `.typeMismatch` error describing the expected type.
  25. ///
  26. /// - parameter path: The path of `CodingKey`s taken to decode a value of this type.
  27. /// - parameter expectation: The type expected to be encountered.
  28. /// - parameter reality: The value that was encountered instead of the expected type.
  29. /// - returns: A `DecodingError` with the appropriate path and debug description.
  30. internal static func _typeMismatch(at path: [CodingKey], expectation: Any.Type, reality: Any) -> DecodingError {
  31. let description = "Expected to decode \(expectation) but found \(_typeDescription(of: reality)) instead."
  32. return .typeMismatch(expectation, Context(codingPath: path, debugDescription: description))
  33. }
  34. /// Returns a description of the type of `value` appropriate for an error message.
  35. ///
  36. /// - parameter value: The value whose type to describe.
  37. /// - returns: A string describing `value`.
  38. /// - precondition: `value` is one of the types below.
  39. fileprivate static func _typeDescription(of value: Any) -> String {
  40. if value is NSNull {
  41. return "a null value"
  42. } else if value is NSNumber /* FIXME: If swift-corelibs-foundation isn't updated to use NSNumber, this check will be necessary: || value is Int || value is Double */ {
  43. return "a number"
  44. } else if value is String {
  45. return "a string/data"
  46. } else if value is [Any] {
  47. return "an array"
  48. } else if value is [String : Any] {
  49. return "a dictionary"
  50. } else {
  51. return "\(type(of: value))"
  52. }
  53. }
  54. }
  55. /// A marker protocol used to determine whether a value is a `String`-keyed `Dictionary`
  56. /// containing `Encodable` values (in which case it should be exempt from key conversion strategies).
  57. ///
  58. /// NOTE: The architecture and environment check is due to a bug in the current (2018-08-08) Swift 4.2
  59. /// runtime when running on i386 simulator. The issue is tracked in https://bugs.swift.org/browse/SR-8276
  60. /// Making the protocol `internal` instead of `fileprivate` works around this issue.
  61. /// Once SR-8276 is fixed, this check can be removed and the protocol always be made fileprivate.
  62. #if arch(i386) || arch(arm)
  63. internal protocol _JSONStringDictionaryEncodableMarker { }
  64. #else
  65. fileprivate protocol _JSONStringDictionaryEncodableMarker { }
  66. #endif
  67. extension Dictionary : _JSONStringDictionaryEncodableMarker where Key == String, Value: Encodable { }
  68. /// A marker protocol used to determine whether a value is a `String`-keyed `Dictionary`
  69. /// containing `Decodable` values (in which case it should be exempt from key conversion strategies).
  70. ///
  71. /// The marker protocol also provides access to the type of the `Decodable` values,
  72. /// which is needed for the implementation of the key conversion strategy exemption.
  73. ///
  74. /// NOTE: Please see comment above regarding SR-8276
  75. #if arch(i386) || arch(arm)
  76. internal protocol _JSONStringDictionaryDecodableMarker {
  77. static var elementType: Decodable.Type { get }
  78. }
  79. #else
  80. fileprivate protocol _JSONStringDictionaryDecodableMarker {
  81. static var elementType: Decodable.Type { get }
  82. }
  83. #endif
  84. extension Dictionary : _JSONStringDictionaryDecodableMarker where Key == String, Value: Decodable {
  85. static var elementType: Decodable.Type { return Value.self }
  86. }
  87. //===----------------------------------------------------------------------===//
  88. // JSON Encoder
  89. //===----------------------------------------------------------------------===//
  90. /// `JSONEncoder` facilitates the encoding of `Encodable` values into JSON.
  91. // NOTE: older overlays had Foundation.JSONEncoder as the ObjC name.
  92. // The two must coexist, so it was renamed. The old name must not be
  93. // used in the new runtime. _TtC10Foundation13__JSONEncoder is the
  94. // mangled name for Foundation.__JSONEncoder.
  95. public class FirebaseDataEncoder {
  96. // MARK: Options
  97. /// The strategy to use for encoding `Date` values.
  98. public enum DateEncodingStrategy {
  99. /// Defer to `Date` for choosing an encoding. This is the default strategy.
  100. case deferredToDate
  101. /// Encode the `Date` as a UNIX timestamp (as a JSON number).
  102. case secondsSince1970
  103. /// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
  104. case millisecondsSince1970
  105. /// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
  106. @available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
  107. case iso8601
  108. /// Encode the `Date` as a string formatted by the given formatter.
  109. case formatted(DateFormatter)
  110. /// Encode the `Date` as a custom value encoded by the given closure.
  111. ///
  112. /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
  113. case custom((Date, Swift.Encoder) throws -> Void)
  114. }
  115. /// The strategy to use for encoding `Data` values.
  116. public enum DataEncodingStrategy {
  117. /// Defer to `Data` for choosing an encoding.
  118. case deferredToData
  119. /// Encode the `Data` as a Base64-encoded string. This is the default strategy.
  120. case base64
  121. /// Encode the `Data` as an `NSData` blob.
  122. case blob
  123. /// Encode the `Data` as a custom value encoded by the given closure.
  124. ///
  125. /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
  126. case custom((Data, Swift.Encoder) throws -> Void)
  127. }
  128. /// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
  129. public enum NonConformingFloatEncodingStrategy {
  130. /// Throw upon encountering non-conforming values. This is the default strategy.
  131. case `throw`
  132. /// Encode the values using the given representation strings.
  133. case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
  134. }
  135. /// The strategy to use for automatically changing the value of keys before encoding.
  136. public enum KeyEncodingStrategy {
  137. /// Use the keys specified by each type. This is the default strategy.
  138. case useDefaultKeys
  139. /// Convert from "camelCaseKeys" to "snake_case_keys" before writing a key to JSON payload.
  140. ///
  141. /// Capital characters are determined by testing membership in `CharacterSet.uppercaseLetters` and `CharacterSet.lowercaseLetters` (Unicode General Categories Lu and Lt).
  142. /// 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.
  143. ///
  144. /// Converting from camel case to snake case:
  145. /// 1. Splits words at the boundary of lower-case to upper-case
  146. /// 2. Inserts `_` between words
  147. /// 3. Lowercases the entire string
  148. /// 4. Preserves starting and ending `_`.
  149. ///
  150. /// For example, `oneTwoThree` becomes `one_two_three`. `_oneTwoThree_` becomes `_one_two_three_`.
  151. ///
  152. /// - Note: Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.
  153. case convertToSnakeCase
  154. /// Provide a custom conversion to the key in the encoded JSON from the keys specified by the encoded types.
  155. /// 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.
  156. /// If the result of the conversion is a duplicate key, then only one value will be present in the result.
  157. case custom((_ codingPath: [CodingKey]) -> CodingKey)
  158. fileprivate static func _convertToSnakeCase(_ stringKey: String) -> String {
  159. guard !stringKey.isEmpty else { return stringKey }
  160. var words : [Range<String.Index>] = []
  161. // 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
  162. //
  163. // myProperty -> my_property
  164. // myURLProperty -> my_url_property
  165. //
  166. // We assume, per Swift naming conventions, that the first character of the key is lowercase.
  167. var wordStart = stringKey.startIndex
  168. var searchRange = stringKey.index(after: wordStart)..<stringKey.endIndex
  169. // Find next uppercase character
  170. while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters, options: [], range: searchRange) {
  171. let untilUpperCase = wordStart..<upperCaseRange.lowerBound
  172. words.append(untilUpperCase)
  173. // Find next lowercase character
  174. searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
  175. guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters, options: [], range: searchRange) else {
  176. // There are no more lower case letters. Just end here.
  177. wordStart = searchRange.lowerBound
  178. break
  179. }
  180. // 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
  181. let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
  182. if lowerCaseRange.lowerBound == nextCharacterAfterCapital {
  183. // The next character after capital is a lower case character and therefore not a word boundary.
  184. // Continue searching for the next upper case for the boundary.
  185. wordStart = upperCaseRange.lowerBound
  186. } else {
  187. // There was a range of >1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
  188. let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
  189. words.append(upperCaseRange.lowerBound..<beforeLowerIndex)
  190. // Next word starts at the capital before the lowercase we just found
  191. wordStart = beforeLowerIndex
  192. }
  193. searchRange = lowerCaseRange.upperBound..<searchRange.upperBound
  194. }
  195. words.append(wordStart..<searchRange.upperBound)
  196. let result = words.map({ (range) in
  197. return stringKey[range].lowercased()
  198. }).joined(separator: "_")
  199. return result
  200. }
  201. }
  202. /// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
  203. open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate
  204. /// The strategy to use in encoding binary data. Defaults to `.base64`.
  205. open var dataEncodingStrategy: DataEncodingStrategy = .base64
  206. /// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
  207. open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw
  208. /// The strategy to use for encoding keys. Defaults to `.useDefaultKeys`.
  209. open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys
  210. /// A type that can resolve which types to 'pass through' - or leave alone while encoding. Defaults to not passing any types through.
  211. open var passthroughTypeResolver: StructureCodingPassthroughTypeResolver.Type = NoPassthroughTypes.self
  212. /// Contextual user-provided information for use during encoding.
  213. open var userInfo: [CodingUserInfoKey : Any] = [:]
  214. /// Options set on the top-level encoder to pass down the encoding hierarchy.
  215. fileprivate struct _Options {
  216. let dateEncodingStrategy: DateEncodingStrategy
  217. let dataEncodingStrategy: DataEncodingStrategy
  218. let nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy
  219. let keyEncodingStrategy: KeyEncodingStrategy
  220. let passthroughTypeResolver: StructureCodingPassthroughTypeResolver.Type
  221. let userInfo: [CodingUserInfoKey : Any]
  222. }
  223. /// The options set on the top-level encoder.
  224. fileprivate var options: _Options {
  225. return _Options(dateEncodingStrategy: dateEncodingStrategy,
  226. dataEncodingStrategy: dataEncodingStrategy,
  227. nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
  228. keyEncodingStrategy: keyEncodingStrategy,
  229. passthroughTypeResolver: passthroughTypeResolver,
  230. userInfo: userInfo)
  231. }
  232. // MARK: - Constructing a JSON Encoder
  233. /// Initializes `self` with default strategies.
  234. public init() {}
  235. // MARK: - Encoding Values
  236. /// Encodes the given top-level value and returns its JSON representation.
  237. ///
  238. /// - parameter value: The value to encode.
  239. /// - returns: A new `Data` value containing the encoded JSON data.
  240. /// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
  241. /// - throws: An error if any value throws an error during encoding.
  242. open func encode<T : Encodable>(_ value: T) throws -> Any {
  243. let encoder = __JSONEncoder(options: self.options)
  244. guard let topLevel = try encoder.box_(value) else {
  245. throw Swift.EncodingError.invalidValue(value,
  246. Swift.EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
  247. }
  248. return topLevel
  249. }
  250. }
  251. // MARK: - __JSONEncoder
  252. // NOTE: older overlays called this class _JSONEncoder.
  253. // The two must coexist without a conflicting ObjC class name, so it
  254. // was renamed. The old name must not be used in the new runtime.
  255. fileprivate class __JSONEncoder : Encoder {
  256. // MARK: Properties
  257. /// The encoder's storage.
  258. fileprivate var storage: _JSONEncodingStorage
  259. /// Options set on the top-level encoder.
  260. fileprivate let options: FirebaseDataEncoder._Options
  261. /// The path to the current point in encoding.
  262. public var codingPath: [CodingKey]
  263. /// Contextual user-provided information for use during encoding.
  264. public var userInfo: [CodingUserInfoKey : Any] {
  265. return self.options.userInfo
  266. }
  267. // MARK: - Initialization
  268. /// Initializes `self` with the given top-level encoder options.
  269. fileprivate init(options: FirebaseDataEncoder._Options, codingPath: [CodingKey] = []) {
  270. self.options = options
  271. self.storage = _JSONEncodingStorage()
  272. self.codingPath = codingPath
  273. }
  274. /// Returns whether a new element can be encoded at this coding path.
  275. ///
  276. /// `true` if an element has not yet been encoded at this coding path; `false` otherwise.
  277. fileprivate var canEncodeNewValue: Bool {
  278. // 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).
  279. // At the same time, every time a container is requested, a new value gets pushed onto the storage stack.
  280. // 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.
  281. //
  282. // This means that anytime something that can request a new container goes onto the stack, we MUST push a key onto the coding path.
  283. // 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).
  284. return self.storage.count == self.codingPath.count
  285. }
  286. // MARK: - Encoder Methods
  287. public func container<Key>(keyedBy: Key.Type) -> KeyedEncodingContainer<Key> {
  288. // If an existing keyed container was already requested, return that one.
  289. let topContainer: NSMutableDictionary
  290. if self.canEncodeNewValue {
  291. // We haven't yet pushed a container at this level; do so here.
  292. topContainer = self.storage.pushKeyedContainer()
  293. } else {
  294. guard let container = self.storage.containers.last as? NSMutableDictionary else {
  295. preconditionFailure("Attempt to push new keyed encoding container when already previously encoded at this path.")
  296. }
  297. topContainer = container
  298. }
  299. let container = _JSONKeyedEncodingContainer<Key>(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
  300. return KeyedEncodingContainer(container)
  301. }
  302. public func unkeyedContainer() -> UnkeyedEncodingContainer {
  303. // If an existing unkeyed container was already requested, return that one.
  304. let topContainer: NSMutableArray
  305. if self.canEncodeNewValue {
  306. // We haven't yet pushed a container at this level; do so here.
  307. topContainer = self.storage.pushUnkeyedContainer()
  308. } else {
  309. guard let container = self.storage.containers.last as? NSMutableArray else {
  310. preconditionFailure("Attempt to push new unkeyed encoding container when already previously encoded at this path.")
  311. }
  312. topContainer = container
  313. }
  314. return _JSONUnkeyedEncodingContainer(referencing: self, codingPath: self.codingPath, wrapping: topContainer)
  315. }
  316. public func singleValueContainer() -> SingleValueEncodingContainer {
  317. return self
  318. }
  319. }
  320. // MARK: - Encoding Storage and Containers
  321. fileprivate struct _JSONEncodingStorage {
  322. // MARK: Properties
  323. /// The container stack.
  324. /// Elements may be any one of the JSON types (NSNull, NSNumber, NSString, NSArray, NSDictionary).
  325. private(set) fileprivate var containers: [NSObject] = []
  326. // MARK: - Initialization
  327. /// Initializes `self` with no containers.
  328. fileprivate init() {}
  329. // MARK: - Modifying the Stack
  330. fileprivate var count: Int {
  331. return self.containers.count
  332. }
  333. fileprivate mutating func pushKeyedContainer() -> NSMutableDictionary {
  334. let dictionary = NSMutableDictionary()
  335. self.containers.append(dictionary)
  336. return dictionary
  337. }
  338. fileprivate mutating func pushUnkeyedContainer() -> NSMutableArray {
  339. let array = NSMutableArray()
  340. self.containers.append(array)
  341. return array
  342. }
  343. fileprivate mutating func push(container: __owned NSObject) {
  344. self.containers.append(container)
  345. }
  346. fileprivate mutating func popContainer() -> NSObject {
  347. precondition(!self.containers.isEmpty, "Empty container stack.")
  348. return self.containers.popLast()!
  349. }
  350. }
  351. // MARK: - Encoding Containers
  352. fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingContainerProtocol {
  353. typealias Key = K
  354. // MARK: Properties
  355. /// A reference to the encoder we're writing to.
  356. private let encoder: __JSONEncoder
  357. /// A reference to the container we're writing to.
  358. private let container: NSMutableDictionary
  359. /// The path of coding keys taken to get to this point in encoding.
  360. private(set) public var codingPath: [CodingKey]
  361. // MARK: - Initialization
  362. /// Initializes `self` with the given references.
  363. fileprivate init(referencing encoder: __JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableDictionary) {
  364. self.encoder = encoder
  365. self.codingPath = codingPath
  366. self.container = container
  367. }
  368. // MARK: - Coding Path Operations
  369. private func _converted(_ key: CodingKey) -> CodingKey {
  370. switch encoder.options.keyEncodingStrategy {
  371. case .useDefaultKeys:
  372. return key
  373. case .convertToSnakeCase:
  374. let newKeyString = FirebaseDataEncoder.KeyEncodingStrategy._convertToSnakeCase(key.stringValue)
  375. return _JSONKey(stringValue: newKeyString, intValue: key.intValue)
  376. case .custom(let converter):
  377. return converter(codingPath + [key])
  378. }
  379. }
  380. // MARK: - KeyedEncodingContainerProtocol Methods
  381. public mutating func encodeNil(forKey key: Key) throws {
  382. self.container[_converted(key).stringValue] = NSNull()
  383. }
  384. public mutating func encode(_ value: Bool, forKey key: Key) throws {
  385. self.container[_converted(key).stringValue] = self.encoder.box(value)
  386. }
  387. public mutating func encode(_ value: Int, forKey key: Key) throws {
  388. self.container[_converted(key).stringValue] = self.encoder.box(value)
  389. }
  390. public mutating func encode(_ value: Int8, forKey key: Key) throws {
  391. self.container[_converted(key).stringValue] = self.encoder.box(value)
  392. }
  393. public mutating func encode(_ value: Int16, forKey key: Key) throws {
  394. self.container[_converted(key).stringValue] = self.encoder.box(value)
  395. }
  396. public mutating func encode(_ value: Int32, forKey key: Key) throws {
  397. self.container[_converted(key).stringValue] = self.encoder.box(value)
  398. }
  399. public mutating func encode(_ value: Int64, forKey key: Key) throws {
  400. self.container[_converted(key).stringValue] = self.encoder.box(value)
  401. }
  402. public mutating func encode(_ value: UInt, forKey key: Key) throws {
  403. self.container[_converted(key).stringValue] = self.encoder.box(value)
  404. }
  405. public mutating func encode(_ value: UInt8, forKey key: Key) throws {
  406. self.container[_converted(key).stringValue] = self.encoder.box(value)
  407. }
  408. public mutating func encode(_ value: UInt16, forKey key: Key) throws {
  409. self.container[_converted(key).stringValue] = self.encoder.box(value)
  410. }
  411. public mutating func encode(_ value: UInt32, forKey key: Key) throws {
  412. self.container[_converted(key).stringValue] = self.encoder.box(value)
  413. }
  414. public mutating func encode(_ value: UInt64, forKey key: Key) throws {
  415. self.container[_converted(key).stringValue] = self.encoder.box(value)
  416. }
  417. public mutating func encode(_ value: String, forKey key: Key) throws {
  418. self.container[_converted(key).stringValue] = self.encoder.box(value)
  419. }
  420. public mutating func encode(_ value: Float, forKey key: Key) throws {
  421. // Since the float may be invalid and throw, the coding path needs to contain this key.
  422. self.encoder.codingPath.append(key)
  423. defer { self.encoder.codingPath.removeLast() }
  424. self.container[_converted(key).stringValue] = try self.encoder.box(value)
  425. }
  426. public mutating func encode(_ value: Double, forKey key: Key) throws {
  427. // Since the double may be invalid and throw, the coding path needs to contain this key.
  428. self.encoder.codingPath.append(key)
  429. defer { self.encoder.codingPath.removeLast() }
  430. self.container[_converted(key).stringValue] = try self.encoder.box(value)
  431. }
  432. public mutating func encode<T : Encodable>(_ value: T, forKey key: Key) throws {
  433. if T.self is StructureCodingUncodedUnkeyed.Type { return }
  434. self.encoder.codingPath.append(key)
  435. defer { self.encoder.codingPath.removeLast() }
  436. self.container[_converted(key).stringValue] = try self.encoder.box(value)
  437. }
  438. public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
  439. let containerKey = _converted(key).stringValue
  440. let dictionary: NSMutableDictionary
  441. if let existingContainer = self.container[containerKey] {
  442. precondition(
  443. existingContainer is NSMutableDictionary,
  444. "Attempt to re-encode into nested KeyedEncodingContainer<\(Key.self)> for key \"\(containerKey)\" is invalid: non-keyed container already encoded for this key"
  445. )
  446. dictionary = existingContainer as! NSMutableDictionary
  447. } else {
  448. dictionary = NSMutableDictionary()
  449. self.container[containerKey] = dictionary
  450. }
  451. self.codingPath.append(key)
  452. defer { self.codingPath.removeLast() }
  453. let container = _JSONKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
  454. return KeyedEncodingContainer(container)
  455. }
  456. public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
  457. let containerKey = _converted(key).stringValue
  458. let array: NSMutableArray
  459. if let existingContainer = self.container[containerKey] {
  460. precondition(
  461. existingContainer is NSMutableArray,
  462. "Attempt to re-encode into nested UnkeyedEncodingContainer for key \"\(containerKey)\" is invalid: keyed container/single value already encoded for this key"
  463. )
  464. array = existingContainer as! NSMutableArray
  465. } else {
  466. array = NSMutableArray()
  467. self.container[containerKey] = array
  468. }
  469. self.codingPath.append(key)
  470. defer { self.codingPath.removeLast() }
  471. return _JSONUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
  472. }
  473. public mutating func superEncoder() -> Encoder {
  474. return __JSONReferencingEncoder(referencing: self.encoder, key: _JSONKey.super, convertedKey: _converted(_JSONKey.super), wrapping: self.container)
  475. }
  476. public mutating func superEncoder(forKey key: Key) -> Encoder {
  477. return __JSONReferencingEncoder(referencing: self.encoder, key: key, convertedKey: _converted(key), wrapping: self.container)
  478. }
  479. }
  480. fileprivate struct _JSONUnkeyedEncodingContainer : UnkeyedEncodingContainer {
  481. // MARK: Properties
  482. /// A reference to the encoder we're writing to.
  483. private let encoder: __JSONEncoder
  484. /// A reference to the container we're writing to.
  485. private let container: NSMutableArray
  486. /// The path of coding keys taken to get to this point in encoding.
  487. private(set) public var codingPath: [CodingKey]
  488. /// The number of elements encoded into the container.
  489. public var count: Int {
  490. return self.container.count
  491. }
  492. // MARK: - Initialization
  493. /// Initializes `self` with the given references.
  494. fileprivate init(referencing encoder: __JSONEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {
  495. self.encoder = encoder
  496. self.codingPath = codingPath
  497. self.container = container
  498. }
  499. // MARK: - UnkeyedEncodingContainer Methods
  500. public mutating func encodeNil() throws { self.container.add(NSNull()) }
  501. public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }
  502. public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }
  503. public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }
  504. public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }
  505. public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }
  506. public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }
  507. public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }
  508. public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }
  509. public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }
  510. public mutating func encode(_ value: UInt32) throws { self.container.add(self.encoder.box(value)) }
  511. public mutating func encode(_ value: UInt64) throws { self.container.add(self.encoder.box(value)) }
  512. public mutating func encode(_ value: String) throws { self.container.add(self.encoder.box(value)) }
  513. public mutating func encode(_ value: Float) throws {
  514. // Since the float may be invalid and throw, the coding path needs to contain this key.
  515. self.encoder.codingPath.append(_JSONKey(index: self.count))
  516. defer { self.encoder.codingPath.removeLast() }
  517. self.container.add(try self.encoder.box(value))
  518. }
  519. public mutating func encode(_ value: Double) throws {
  520. // Since the double may be invalid and throw, the coding path needs to contain this key.
  521. self.encoder.codingPath.append(_JSONKey(index: self.count))
  522. defer { self.encoder.codingPath.removeLast() }
  523. self.container.add(try self.encoder.box(value))
  524. }
  525. public mutating func encode<T : Encodable>(_ value: T) throws {
  526. self.encoder.codingPath.append(_JSONKey(index: self.count))
  527. defer { self.encoder.codingPath.removeLast() }
  528. self.container.add(try self.encoder.box(value))
  529. }
  530. public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> {
  531. self.codingPath.append(_JSONKey(index: self.count))
  532. defer { self.codingPath.removeLast() }
  533. let dictionary = NSMutableDictionary()
  534. self.container.add(dictionary)
  535. let container = _JSONKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)
  536. return KeyedEncodingContainer(container)
  537. }
  538. public mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
  539. self.codingPath.append(_JSONKey(index: self.count))
  540. defer { self.codingPath.removeLast() }
  541. let array = NSMutableArray()
  542. self.container.add(array)
  543. return _JSONUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)
  544. }
  545. public mutating func superEncoder() -> Encoder {
  546. return __JSONReferencingEncoder(referencing: self.encoder, at: self.container.count, wrapping: self.container)
  547. }
  548. }
  549. extension __JSONEncoder : SingleValueEncodingContainer {
  550. // MARK: - SingleValueEncodingContainer Methods
  551. fileprivate func assertCanEncodeNewValue() {
  552. precondition(self.canEncodeNewValue, "Attempt to encode value through single value container when previously value already encoded.")
  553. }
  554. public func encodeNil() throws {
  555. assertCanEncodeNewValue()
  556. self.storage.push(container: NSNull())
  557. }
  558. public func encode(_ value: Bool) throws {
  559. assertCanEncodeNewValue()
  560. self.storage.push(container: self.box(value))
  561. }
  562. public func encode(_ value: Int) throws {
  563. assertCanEncodeNewValue()
  564. self.storage.push(container: self.box(value))
  565. }
  566. public func encode(_ value: Int8) throws {
  567. assertCanEncodeNewValue()
  568. self.storage.push(container: self.box(value))
  569. }
  570. public func encode(_ value: Int16) throws {
  571. assertCanEncodeNewValue()
  572. self.storage.push(container: self.box(value))
  573. }
  574. public func encode(_ value: Int32) throws {
  575. assertCanEncodeNewValue()
  576. self.storage.push(container: self.box(value))
  577. }
  578. public func encode(_ value: Int64) throws {
  579. assertCanEncodeNewValue()
  580. self.storage.push(container: self.box(value))
  581. }
  582. public func encode(_ value: UInt) throws {
  583. assertCanEncodeNewValue()
  584. self.storage.push(container: self.box(value))
  585. }
  586. public func encode(_ value: UInt8) throws {
  587. assertCanEncodeNewValue()
  588. self.storage.push(container: self.box(value))
  589. }
  590. public func encode(_ value: UInt16) throws {
  591. assertCanEncodeNewValue()
  592. self.storage.push(container: self.box(value))
  593. }
  594. public func encode(_ value: UInt32) throws {
  595. assertCanEncodeNewValue()
  596. self.storage.push(container: self.box(value))
  597. }
  598. public func encode(_ value: UInt64) throws {
  599. assertCanEncodeNewValue()
  600. self.storage.push(container: self.box(value))
  601. }
  602. public func encode(_ value: String) throws {
  603. assertCanEncodeNewValue()
  604. self.storage.push(container: self.box(value))
  605. }
  606. public func encode(_ value: Float) throws {
  607. assertCanEncodeNewValue()
  608. try self.storage.push(container: self.box(value))
  609. }
  610. public func encode(_ value: Double) throws {
  611. assertCanEncodeNewValue()
  612. try self.storage.push(container: self.box(value))
  613. }
  614. public func encode<T : Encodable>(_ value: T) throws {
  615. assertCanEncodeNewValue()
  616. try self.storage.push(container: self.box(value))
  617. }
  618. }
  619. // MARK: - Concrete Value Representations
  620. extension __JSONEncoder {
  621. /// Returns the given value boxed in a container appropriate for pushing onto the container stack.
  622. fileprivate func box(_ value: Bool) -> NSObject { return NSNumber(value: value) }
  623. fileprivate func box(_ value: Int) -> NSObject { return NSNumber(value: value) }
  624. fileprivate func box(_ value: Int8) -> NSObject { return NSNumber(value: value) }
  625. fileprivate func box(_ value: Int16) -> NSObject { return NSNumber(value: value) }
  626. fileprivate func box(_ value: Int32) -> NSObject { return NSNumber(value: value) }
  627. fileprivate func box(_ value: Int64) -> NSObject { return NSNumber(value: value) }
  628. fileprivate func box(_ value: UInt) -> NSObject { return NSNumber(value: value) }
  629. fileprivate func box(_ value: UInt8) -> NSObject { return NSNumber(value: value) }
  630. fileprivate func box(_ value: UInt16) -> NSObject { return NSNumber(value: value) }
  631. fileprivate func box(_ value: UInt32) -> NSObject { return NSNumber(value: value) }
  632. fileprivate func box(_ value: UInt64) -> NSObject { return NSNumber(value: value) }
  633. fileprivate func box(_ value: String) -> NSObject { return NSString(string: value) }
  634. fileprivate func box(_ float: Float) throws -> NSObject {
  635. guard !float.isInfinite && !float.isNaN else {
  636. guard case let .convertToString(positiveInfinity: posInfString,
  637. negativeInfinity: negInfString,
  638. nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
  639. throw EncodingError._invalidFloatingPointValue(float, at: codingPath)
  640. }
  641. if float == Float.infinity {
  642. return NSString(string: posInfString)
  643. } else if float == -Float.infinity {
  644. return NSString(string: negInfString)
  645. } else {
  646. return NSString(string: nanString)
  647. }
  648. }
  649. return NSNumber(value: float)
  650. }
  651. fileprivate func box(_ double: Double) throws -> NSObject {
  652. guard !double.isInfinite && !double.isNaN else {
  653. guard case let .convertToString(positiveInfinity: posInfString,
  654. negativeInfinity: negInfString,
  655. nan: nanString) = self.options.nonConformingFloatEncodingStrategy else {
  656. throw EncodingError._invalidFloatingPointValue(double, at: codingPath)
  657. }
  658. if double == Double.infinity {
  659. return NSString(string: posInfString)
  660. } else if double == -Double.infinity {
  661. return NSString(string: negInfString)
  662. } else {
  663. return NSString(string: nanString)
  664. }
  665. }
  666. return NSNumber(value: double)
  667. }
  668. fileprivate func box(_ date: Date) throws -> NSObject {
  669. switch self.options.dateEncodingStrategy {
  670. case .deferredToDate:
  671. // Must be called with a surrounding with(pushedKey:) call.
  672. // Dates encode as single-value objects; this can't both throw and push a container, so no need to catch the error.
  673. try date.encode(to: self)
  674. return self.storage.popContainer()
  675. case .secondsSince1970:
  676. return NSNumber(value: date.timeIntervalSince1970)
  677. case .millisecondsSince1970:
  678. return NSNumber(value: 1000.0 * date.timeIntervalSince1970)
  679. case .iso8601:
  680. if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
  681. return NSString(string: _iso8601Formatter.string(from: date))
  682. } else {
  683. fatalError("ISO8601DateFormatter is unavailable on this platform.")
  684. }
  685. case .formatted(let formatter):
  686. return NSString(string: formatter.string(from: date))
  687. case .custom(let closure):
  688. let depth = self.storage.count
  689. do {
  690. try closure(date, self)
  691. } catch {
  692. // If the value pushed a container before throwing, pop it back off to restore state.
  693. if self.storage.count > depth {
  694. let _ = self.storage.popContainer()
  695. }
  696. throw error
  697. }
  698. guard self.storage.count > depth else {
  699. // The closure didn't encode anything. Return the default keyed container.
  700. return NSDictionary()
  701. }
  702. // We can pop because the closure encoded something.
  703. return self.storage.popContainer()
  704. }
  705. }
  706. fileprivate func box(_ data: Data) throws -> NSObject {
  707. switch self.options.dataEncodingStrategy {
  708. case .deferredToData:
  709. // Must be called with a surrounding with(pushedKey:) call.
  710. let depth = self.storage.count
  711. do {
  712. try data.encode(to: self)
  713. } catch {
  714. // If the value pushed a container before throwing, pop it back off to restore state.
  715. // This shouldn't be possible for Data (which encodes as an array of bytes), but it can't hurt to catch a failure.
  716. if self.storage.count > depth {
  717. let _ = self.storage.popContainer()
  718. }
  719. throw error
  720. }
  721. return self.storage.popContainer()
  722. case .base64:
  723. return NSString(string: data.base64EncodedString())
  724. case .blob:
  725. return data as NSData
  726. case .custom(let closure):
  727. let depth = self.storage.count
  728. do {
  729. try closure(data, self)
  730. } catch {
  731. // If the value pushed a container before throwing, pop it back off to restore state.
  732. if self.storage.count > depth {
  733. let _ = self.storage.popContainer()
  734. }
  735. throw error
  736. }
  737. guard self.storage.count > depth else {
  738. // The closure didn't encode anything. Return the default keyed container.
  739. return NSDictionary()
  740. }
  741. // We can pop because the closure encoded something.
  742. return self.storage.popContainer()
  743. }
  744. }
  745. fileprivate func box(_ dict: [String : Encodable]) throws -> NSObject? {
  746. let depth = self.storage.count
  747. let result = self.storage.pushKeyedContainer()
  748. do {
  749. for (key, value) in dict {
  750. self.codingPath.append(_JSONKey(stringValue: key, intValue: nil))
  751. defer { self.codingPath.removeLast() }
  752. result[key] = try box(value)
  753. }
  754. } catch {
  755. // If the value pushed a container before throwing, pop it back off to restore state.
  756. if self.storage.count > depth {
  757. let _ = self.storage.popContainer()
  758. }
  759. throw error
  760. }
  761. // The top container should be a new container.
  762. guard self.storage.count > depth else {
  763. return nil
  764. }
  765. return self.storage.popContainer()
  766. }
  767. fileprivate func box(_ value: Encodable) throws -> NSObject {
  768. return try self.box_(value) ?? NSDictionary()
  769. }
  770. // This method is called "box_" instead of "box" to disambiguate it from the overloads. Because the return type here is different from all of the "box" overloads (and is more general), any "box" calls in here would call back into "box" recursively instead of calling the appropriate overload, which is not what we want.
  771. fileprivate func box_(_ value: Encodable) throws -> NSObject? {
  772. // Disambiguation between variable and function is required due to
  773. // issue tracked at: https://bugs.swift.org/browse/SR-1846
  774. let type = Swift.type(of: value)
  775. if type == Date.self || type == NSDate.self {
  776. // Respect Date encoding strategy
  777. return try self.box((value as! Date))
  778. } else if type == Data.self || type == NSData.self {
  779. // Respect Data encoding strategy
  780. return try self.box((value as! Data))
  781. } else if type == URL.self || type == NSURL.self {
  782. // Encode URLs as single strings.
  783. return self.box((value as! URL).absoluteString)
  784. } else if type == Decimal.self || type == NSDecimalNumber.self {
  785. // JSONSerialization can natively handle NSDecimalNumber.
  786. return (value as! NSDecimalNumber)
  787. } else if value is _JSONStringDictionaryEncodableMarker {
  788. return try self.box(value as! [String : Encodable])
  789. } else if let object = value as? NSObject, self.options.passthroughTypeResolver.isPassthroughType(value) {
  790. return object
  791. }
  792. // The value should request a container from the __JSONEncoder.
  793. let depth = self.storage.count
  794. do {
  795. try value.encode(to: self)
  796. } catch {
  797. // If the value pushed a container before throwing, pop it back off to restore state.
  798. if self.storage.count > depth {
  799. let _ = self.storage.popContainer()
  800. }
  801. throw error
  802. }
  803. // The top container should be a new container.
  804. guard self.storage.count > depth else {
  805. return nil
  806. }
  807. return self.storage.popContainer()
  808. }
  809. }
  810. // MARK: - __JSONReferencingEncoder
  811. /// __JSONReferencingEncoder is a special subclass of __JSONEncoder which has its own storage, but references the contents of a different encoder.
  812. /// It's used in superEncoder(), which returns a new encoder for encoding a superclass -- the lifetime of the encoder should not escape the scope it's created in, but it doesn't necessarily know when it's done being used (to write to the original container).
  813. // NOTE: older overlays called this class _JSONReferencingEncoder.
  814. // The two must coexist without a conflicting ObjC class name, so it
  815. // was renamed. The old name must not be used in the new runtime.
  816. fileprivate class __JSONReferencingEncoder : __JSONEncoder {
  817. // MARK: Reference types.
  818. /// The type of container we're referencing.
  819. private enum Reference {
  820. /// Referencing a specific index in an array container.
  821. case array(NSMutableArray, Int)
  822. /// Referencing a specific key in a dictionary container.
  823. case dictionary(NSMutableDictionary, String)
  824. }
  825. // MARK: - Properties
  826. /// The encoder we're referencing.
  827. fileprivate let encoder: __JSONEncoder
  828. /// The container reference itself.
  829. private let reference: Reference
  830. // MARK: - Initialization
  831. /// Initializes `self` by referencing the given array container in the given encoder.
  832. fileprivate init(referencing encoder: __JSONEncoder, at index: Int, wrapping array: NSMutableArray) {
  833. self.encoder = encoder
  834. self.reference = .array(array, index)
  835. super.init(options: encoder.options, codingPath: encoder.codingPath)
  836. self.codingPath.append(_JSONKey(index: index))
  837. }
  838. /// Initializes `self` by referencing the given dictionary container in the given encoder.
  839. fileprivate init(referencing encoder: __JSONEncoder,
  840. key: CodingKey, convertedKey: __shared CodingKey, wrapping dictionary: NSMutableDictionary) {
  841. self.encoder = encoder
  842. self.reference = .dictionary(dictionary, convertedKey.stringValue)
  843. super.init(options: encoder.options, codingPath: encoder.codingPath)
  844. self.codingPath.append(key)
  845. }
  846. // MARK: - Coding Path Operations
  847. fileprivate override var canEncodeNewValue: Bool {
  848. // With a regular encoder, the storage and coding path grow together.
  849. // A referencing encoder, however, inherits its parents coding path, as well as the key it was created for.
  850. // We have to take this into account.
  851. return self.storage.count == self.codingPath.count - self.encoder.codingPath.count - 1
  852. }
  853. // MARK: - Deinitialization
  854. // Finalizes `self` by writing the contents of our storage to the referenced encoder's storage.
  855. deinit {
  856. let value: Any
  857. switch self.storage.count {
  858. case 0: value = NSDictionary()
  859. case 1: value = self.storage.popContainer()
  860. default: fatalError("Referencing encoder deallocated with multiple containers on stack.")
  861. }
  862. switch self.reference {
  863. case .array(let array, let index):
  864. array.insert(value, at: index)
  865. case .dictionary(let dictionary, let key):
  866. dictionary[NSString(string: key)] = value
  867. }
  868. }
  869. }
  870. //===----------------------------------------------------------------------===//
  871. // JSON Decoder
  872. //===----------------------------------------------------------------------===//
  873. /// `JSONDecoder` facilitates the decoding of JSON into semantic `Decodable` types.
  874. // NOTE: older overlays had Foundation.JSONDecoder as the ObjC name.
  875. // The two must coexist, so it was renamed. The old name must not be
  876. // used in the new runtime. _TtC10Foundation13__JSONDecoder is the
  877. // mangled name for Foundation.__JSONDecoder.
  878. public class FirebaseDataDecoder {
  879. // MARK: Options
  880. /// The strategy to use for decoding `Date` values.
  881. public enum DateDecodingStrategy {
  882. /// Defer to `Date` for decoding. This is the default strategy.
  883. case deferredToDate
  884. /// Decode the `Date` as a UNIX timestamp from a JSON number.
  885. case secondsSince1970
  886. /// Decode the `Date` as UNIX millisecond timestamp from a JSON number.
  887. case millisecondsSince1970
  888. /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
  889. @available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
  890. case iso8601
  891. /// Decode the `Date` as a string parsed by the given formatter.
  892. case formatted(DateFormatter)
  893. /// Decode the `Date` as a custom value decoded by the given closure.
  894. case custom((_ decoder: Swift.Decoder) throws -> Date)
  895. }
  896. /// The strategy to use for decoding `Data` values.
  897. public enum DataDecodingStrategy {
  898. /// Defer to `Data` for decoding.
  899. case deferredToData
  900. /// Decode the `Data` from a Base64-encoded string. This is the default strategy.
  901. case base64
  902. /// Decode the `Data` as an `NSData` blob.
  903. case blob
  904. /// Decode the `Data` as a custom value decoded by the given closure.
  905. case custom((_ decoder: Swift.Decoder) throws -> Data)
  906. }
  907. /// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
  908. public enum NonConformingFloatDecodingStrategy {
  909. /// Throw upon encountering non-conforming values. This is the default strategy.
  910. case `throw`
  911. /// Decode the values from the given representation strings.
  912. case convertFromString(positiveInfinity: String, negativeInfinity: String, nan: String)
  913. }
  914. /// The strategy to use for automatically changing the value of keys before decoding.
  915. public enum KeyDecodingStrategy {
  916. /// Use the keys specified by each type. This is the default strategy.
  917. case useDefaultKeys
  918. /// Convert from "snake_case_keys" to "camelCaseKeys" before attempting to match a key with the one specified by each type.
  919. ///
  920. /// The conversion to upper 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.
  921. ///
  922. /// Converting from snake case to camel case:
  923. /// 1. Capitalizes the word starting after each `_`
  924. /// 2. Removes all `_`
  925. /// 3. Preserves starting and ending `_` (as these are often used to indicate private variables or other metadata).
  926. /// For example, `one_two_three` becomes `oneTwoThree`. `_one_two_three_` becomes `_oneTwoThree_`.
  927. ///
  928. /// - Note: Using a key decoding strategy has a nominal performance cost, as each string key has to be inspected for the `_` character.
  929. case convertFromSnakeCase
  930. /// Provide a custom conversion from the key in the encoded JSON to the keys specified by the decoded types.
  931. /// 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.
  932. /// 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.
  933. case custom((_ codingPath: [CodingKey]) -> CodingKey)
  934. fileprivate static func _convertFromSnakeCase(_ stringKey: String) -> String {
  935. guard !stringKey.isEmpty else { return stringKey }
  936. // Find the first non-underscore character
  937. guard let firstNonUnderscore = stringKey.firstIndex(where: { $0 != "_" }) else {
  938. // Reached the end without finding an _
  939. return stringKey
  940. }
  941. // Find the last non-underscore character
  942. var lastNonUnderscore = stringKey.index(before: stringKey.endIndex)
  943. while lastNonUnderscore > firstNonUnderscore && stringKey[lastNonUnderscore] == "_" {
  944. stringKey.formIndex(before: &lastNonUnderscore)
  945. }
  946. let keyRange = firstNonUnderscore...lastNonUnderscore
  947. let leadingUnderscoreRange = stringKey.startIndex..<firstNonUnderscore
  948. let trailingUnderscoreRange = stringKey.index(after: lastNonUnderscore)..<stringKey.endIndex
  949. let components = stringKey[keyRange].split(separator: "_")
  950. let joinedString : String
  951. if components.count == 1 {
  952. // No underscores in key, leave the word as is - maybe already camel cased
  953. joinedString = String(stringKey[keyRange])
  954. } else {
  955. joinedString = ([components[0].lowercased()] + components[1...].map { $0.capitalized }).joined()
  956. }
  957. // Do a cheap isEmpty check before creating and appending potentially empty strings
  958. let result : String
  959. if (leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty) {
  960. result = joinedString
  961. } else if (!leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty) {
  962. // Both leading and trailing underscores
  963. result = String(stringKey[leadingUnderscoreRange]) + joinedString + String(stringKey[trailingUnderscoreRange])
  964. } else if (!leadingUnderscoreRange.isEmpty) {
  965. // Just leading
  966. result = String(stringKey[leadingUnderscoreRange]) + joinedString
  967. } else {
  968. // Just trailing
  969. result = joinedString + String(stringKey[trailingUnderscoreRange])
  970. }
  971. return result
  972. }
  973. }
  974. /// The strategy to use in decoding dates. Defaults to `.deferredToDate`.
  975. open var dateDecodingStrategy: DateDecodingStrategy = .deferredToDate
  976. /// The strategy to use in decoding binary data. Defaults to `.base64`.
  977. open var dataDecodingStrategy: DataDecodingStrategy = .base64
  978. /// The strategy to use in decoding non-conforming numbers. Defaults to `.throw`.
  979. open var nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy = .throw
  980. /// The strategy to use for decoding keys. Defaults to `.useDefaultKeys`.
  981. open var keyDecodingStrategy: KeyDecodingStrategy = .useDefaultKeys
  982. /// A type that can resolve which types to 'pass through' - or leave alone while decoding. Defaults to not passing any types through.
  983. open var passthroughTypeResolver: StructureCodingPassthroughTypeResolver.Type = NoPassthroughTypes.self
  984. /// Contextual user-provided information for use during decoding.
  985. open var userInfo: [CodingUserInfoKey : Any] = [:]
  986. /// Options set on the top-level encoder to pass down the decoding hierarchy.
  987. fileprivate struct _Options {
  988. let dateDecodingStrategy: DateDecodingStrategy
  989. let dataDecodingStrategy: DataDecodingStrategy
  990. let nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy
  991. let keyDecodingStrategy: KeyDecodingStrategy
  992. let passthroughTypeResolver: StructureCodingPassthroughTypeResolver.Type
  993. let userInfo: [CodingUserInfoKey : Any]
  994. }
  995. /// The options set on the top-level decoder.
  996. fileprivate var options: _Options {
  997. return _Options(dateDecodingStrategy: dateDecodingStrategy,
  998. dataDecodingStrategy: dataDecodingStrategy,
  999. nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy,
  1000. keyDecodingStrategy: keyDecodingStrategy,
  1001. passthroughTypeResolver: passthroughTypeResolver,
  1002. userInfo: userInfo)
  1003. }
  1004. // MARK: - Constructing a JSON Decoder
  1005. /// Initializes `self` with default strategies.
  1006. public init() {}
  1007. // MARK: - Decoding Values
  1008. /// Decodes a top-level value of the given type from the given JSON representation.
  1009. ///
  1010. /// - parameter type: The type of the value to decode.
  1011. /// - parameter data: The data to decode from.
  1012. /// - returns: A value of the requested type.
  1013. /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
  1014. /// - throws: An error if any value throws an error during decoding.
  1015. open func decode<T : Decodable>(_ type: T.Type, from structure: Any) throws -> T {
  1016. let decoder = __JSONDecoder(referencing: structure, options: self.options)
  1017. guard let value = try decoder.unbox(structure, as: type) else {
  1018. throw Swift.DecodingError.valueNotFound(type, Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value."))
  1019. }
  1020. return value
  1021. }
  1022. }
  1023. // MARK: - __JSONDecoder
  1024. // NOTE: older overlays called this class _JSONDecoder. The two must
  1025. // coexist without a conflicting ObjC class name, so it was renamed.
  1026. // The old name must not be used in the new runtime.
  1027. fileprivate class __JSONDecoder : Decoder {
  1028. // MARK: Properties
  1029. /// The decoder's storage.
  1030. fileprivate var storage: _JSONDecodingStorage
  1031. /// Options set on the top-level decoder.
  1032. fileprivate let options: FirebaseDataDecoder._Options
  1033. /// The path to the current point in encoding.
  1034. fileprivate(set) public var codingPath: [CodingKey]
  1035. /// Contextual user-provided information for use during encoding.
  1036. public var userInfo: [CodingUserInfoKey : Any] {
  1037. return self.options.userInfo
  1038. }
  1039. // MARK: - Initialization
  1040. /// Initializes `self` with the given top-level container and options.
  1041. fileprivate init(referencing container: Any, at codingPath: [CodingKey] = [], options: FirebaseDataDecoder._Options) {
  1042. self.storage = _JSONDecodingStorage()
  1043. self.storage.push(container: container)
  1044. self.codingPath = codingPath
  1045. self.options = options
  1046. }
  1047. // MARK: - Decoder Methods
  1048. public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
  1049. guard !(self.storage.topContainer is NSNull) else {
  1050. throw DecodingError.valueNotFound(KeyedDecodingContainer<Key>.self,
  1051. DecodingError.Context(codingPath: self.codingPath,
  1052. debugDescription: "Cannot get keyed decoding container -- found null value instead."))
  1053. }
  1054. var topContainer : [String : Any]
  1055. if let rcValue = self.storage.topContainer as? FirebaseRemoteConfigValueDecoding {
  1056. guard let top = rcValue.dictionaryValue() else {
  1057. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self,
  1058. reality: rcValue)
  1059. }
  1060. topContainer = top
  1061. } else {
  1062. guard let top = self.storage.topContainer as? [String : Any] else {
  1063. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)
  1064. }
  1065. topContainer = top
  1066. }
  1067. let container = _JSONKeyedDecodingContainer<Key>(referencing: self, wrapping: topContainer)
  1068. return KeyedDecodingContainer(container)
  1069. }
  1070. public func unkeyedContainer() throws -> UnkeyedDecodingContainer {
  1071. guard !(self.storage.topContainer is NSNull) else {
  1072. throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
  1073. DecodingError.Context(codingPath: self.codingPath,
  1074. debugDescription: "Cannot get unkeyed decoding container -- found null value instead."))
  1075. }
  1076. if let rcValue = self.storage.topContainer as? FirebaseRemoteConfigValueDecoding {
  1077. guard let arrayValue = rcValue.arrayValue() else {
  1078. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: rcValue)
  1079. }
  1080. return _JSONUnkeyedDecodingContainer(referencing: self, wrapping: arrayValue )
  1081. }
  1082. guard let topContainer = self.storage.topContainer as? [Any] else {
  1083. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: self.storage.topContainer)
  1084. }
  1085. return _JSONUnkeyedDecodingContainer(referencing: self, wrapping: topContainer)
  1086. }
  1087. public func singleValueContainer() throws -> SingleValueDecodingContainer {
  1088. return self
  1089. }
  1090. }
  1091. // MARK: - Decoding Storage
  1092. fileprivate struct _JSONDecodingStorage {
  1093. // MARK: Properties
  1094. /// The container stack.
  1095. /// Elements may be any one of the JSON types (NSNull, NSNumber, String, Array, [String : Any]).
  1096. private(set) fileprivate var containers: [Any] = []
  1097. // MARK: - Initialization
  1098. /// Initializes `self` with no containers.
  1099. fileprivate init() {}
  1100. // MARK: - Modifying the Stack
  1101. fileprivate var count: Int {
  1102. return self.containers.count
  1103. }
  1104. fileprivate var topContainer: Any {
  1105. precondition(!self.containers.isEmpty, "Empty container stack.")
  1106. return self.containers.last!
  1107. }
  1108. fileprivate mutating func push(container: __owned Any) {
  1109. self.containers.append(container)
  1110. }
  1111. fileprivate mutating func popContainer() {
  1112. precondition(!self.containers.isEmpty, "Empty container stack.")
  1113. self.containers.removeLast()
  1114. }
  1115. }
  1116. // MARK: Decoding Containers
  1117. fileprivate struct _JSONKeyedDecodingContainer<K : CodingKey> : KeyedDecodingContainerProtocol {
  1118. typealias Key = K
  1119. // MARK: Properties
  1120. /// A reference to the decoder we're reading from.
  1121. private let decoder: __JSONDecoder
  1122. /// A reference to the container we're reading from.
  1123. private let container: [String : Any]
  1124. /// The path of coding keys taken to get to this point in decoding.
  1125. private(set) public var codingPath: [CodingKey]
  1126. // MARK: - Initialization
  1127. /// Initializes `self` by referencing the given decoder and container.
  1128. fileprivate init(referencing decoder: __JSONDecoder, wrapping container: [String : Any]) {
  1129. self.decoder = decoder
  1130. switch decoder.options.keyDecodingStrategy {
  1131. case .useDefaultKeys:
  1132. self.container = container
  1133. case .convertFromSnakeCase:
  1134. // Convert the snake case keys in the container to camel case.
  1135. // If we hit a duplicate key after conversion, then we'll use the first one we saw. Effectively an undefined behavior with JSON dictionaries.
  1136. self.container = Dictionary(container.map {
  1137. key, value in (FirebaseDataDecoder.KeyDecodingStrategy._convertFromSnakeCase(key), value)
  1138. }, uniquingKeysWith: { (first, _) in first })
  1139. case .custom(let converter):
  1140. self.container = Dictionary(container.map {
  1141. key, value in (converter(decoder.codingPath + [_JSONKey(stringValue: key, intValue: nil)]).stringValue, value)
  1142. }, uniquingKeysWith: { (first, _) in first })
  1143. }
  1144. self.codingPath = decoder.codingPath
  1145. }
  1146. // MARK: - KeyedDecodingContainerProtocol Methods
  1147. public var allKeys: [Key] {
  1148. return self.container.keys.compactMap { Key(stringValue: $0) }
  1149. }
  1150. public func contains(_ key: Key) -> Bool {
  1151. return self.container[key.stringValue] != nil
  1152. }
  1153. private func _errorDescription(of key: CodingKey) -> String {
  1154. switch decoder.options.keyDecodingStrategy {
  1155. case .convertFromSnakeCase:
  1156. // In this case we can attempt to recover the original value by reversing the transform
  1157. let original = key.stringValue
  1158. let converted = FirebaseDataEncoder.KeyEncodingStrategy._convertToSnakeCase(original)
  1159. let roundtrip = FirebaseDataDecoder.KeyDecodingStrategy._convertFromSnakeCase(converted)
  1160. if converted == original {
  1161. return "\(key) (\"\(original)\")"
  1162. } else if roundtrip == original {
  1163. return "\(key) (\"\(original)\"), converted to \(converted)"
  1164. } else {
  1165. return "\(key) (\"\(original)\"), with divergent representation \(roundtrip), converted to \(converted)"
  1166. }
  1167. default:
  1168. // Otherwise, just report the converted string
  1169. return "\(key) (\"\(key.stringValue)\")"
  1170. }
  1171. }
  1172. public func decodeNil(forKey key: Key) throws -> Bool {
  1173. guard let entry = self.container[key.stringValue] else {
  1174. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1175. }
  1176. return entry is NSNull
  1177. }
  1178. public func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
  1179. guard let entry = self.container[key.stringValue] else {
  1180. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1181. }
  1182. self.decoder.codingPath.append(key)
  1183. defer { self.decoder.codingPath.removeLast() }
  1184. guard let value = try self.decoder.unbox(entry, as: Bool.self) else {
  1185. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1186. }
  1187. return value
  1188. }
  1189. public func decode(_ type: Int.Type, forKey key: Key) throws -> Int {
  1190. guard let entry = self.container[key.stringValue] else {
  1191. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1192. }
  1193. self.decoder.codingPath.append(key)
  1194. defer { self.decoder.codingPath.removeLast() }
  1195. guard let value = try self.decoder.unbox(entry, as: Int.self) else {
  1196. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1197. }
  1198. return value
  1199. }
  1200. public func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {
  1201. guard let entry = self.container[key.stringValue] else {
  1202. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1203. }
  1204. self.decoder.codingPath.append(key)
  1205. defer { self.decoder.codingPath.removeLast() }
  1206. guard let value = try self.decoder.unbox(entry, as: Int8.self) else {
  1207. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1208. }
  1209. return value
  1210. }
  1211. public func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 {
  1212. guard let entry = self.container[key.stringValue] else {
  1213. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1214. }
  1215. self.decoder.codingPath.append(key)
  1216. defer { self.decoder.codingPath.removeLast() }
  1217. guard let value = try self.decoder.unbox(entry, as: Int16.self) else {
  1218. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1219. }
  1220. return value
  1221. }
  1222. public func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
  1223. guard let entry = self.container[key.stringValue] else {
  1224. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1225. }
  1226. self.decoder.codingPath.append(key)
  1227. defer { self.decoder.codingPath.removeLast() }
  1228. guard let value = try self.decoder.unbox(entry, as: Int32.self) else {
  1229. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1230. }
  1231. return value
  1232. }
  1233. public func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
  1234. guard let entry = self.container[key.stringValue] else {
  1235. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1236. }
  1237. self.decoder.codingPath.append(key)
  1238. defer { self.decoder.codingPath.removeLast() }
  1239. guard let value = try self.decoder.unbox(entry, as: Int64.self) else {
  1240. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1241. }
  1242. return value
  1243. }
  1244. public func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt {
  1245. guard let entry = self.container[key.stringValue] else {
  1246. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1247. }
  1248. self.decoder.codingPath.append(key)
  1249. defer { self.decoder.codingPath.removeLast() }
  1250. guard let value = try self.decoder.unbox(entry, as: UInt.self) else {
  1251. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1252. }
  1253. return value
  1254. }
  1255. public func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {
  1256. guard let entry = self.container[key.stringValue] else {
  1257. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1258. }
  1259. self.decoder.codingPath.append(key)
  1260. defer { self.decoder.codingPath.removeLast() }
  1261. guard let value = try self.decoder.unbox(entry, as: UInt8.self) else {
  1262. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1263. }
  1264. return value
  1265. }
  1266. public func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 {
  1267. guard let entry = self.container[key.stringValue] else {
  1268. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1269. }
  1270. self.decoder.codingPath.append(key)
  1271. defer { self.decoder.codingPath.removeLast() }
  1272. guard let value = try self.decoder.unbox(entry, as: UInt16.self) else {
  1273. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1274. }
  1275. return value
  1276. }
  1277. public func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 {
  1278. guard let entry = self.container[key.stringValue] else {
  1279. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1280. }
  1281. self.decoder.codingPath.append(key)
  1282. defer { self.decoder.codingPath.removeLast() }
  1283. guard let value = try self.decoder.unbox(entry, as: UInt32.self) else {
  1284. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1285. }
  1286. return value
  1287. }
  1288. public func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 {
  1289. guard let entry = self.container[key.stringValue] else {
  1290. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1291. }
  1292. self.decoder.codingPath.append(key)
  1293. defer { self.decoder.codingPath.removeLast() }
  1294. guard let value = try self.decoder.unbox(entry, as: UInt64.self) else {
  1295. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1296. }
  1297. return value
  1298. }
  1299. public func decode(_ type: Float.Type, forKey key: Key) throws -> Float {
  1300. guard let entry = self.container[key.stringValue] else {
  1301. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1302. }
  1303. self.decoder.codingPath.append(key)
  1304. defer { self.decoder.codingPath.removeLast() }
  1305. guard let value = try self.decoder.unbox(entry, as: Float.self) else {
  1306. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1307. }
  1308. return value
  1309. }
  1310. public func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
  1311. guard let entry = self.container[key.stringValue] else {
  1312. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1313. }
  1314. self.decoder.codingPath.append(key)
  1315. defer { self.decoder.codingPath.removeLast() }
  1316. guard let value = try self.decoder.unbox(entry, as: Double.self) else {
  1317. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1318. }
  1319. return value
  1320. }
  1321. public func decode(_ type: String.Type, forKey key: Key) throws -> String {
  1322. guard let entry = self.container[key.stringValue] else {
  1323. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1324. }
  1325. self.decoder.codingPath.append(key)
  1326. defer { self.decoder.codingPath.removeLast() }
  1327. guard let value = try self.decoder.unbox(entry, as: String.self) else {
  1328. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1329. }
  1330. return value
  1331. }
  1332. public func decode<T : Decodable>(_ type: T.Type, forKey key: Key) throws -> T {
  1333. if type is StructureCodingUncodedUnkeyed.Type {
  1334. // Note: not pushing and popping key to codingPath since the key is
  1335. // not part of the decoded structure.
  1336. return try T.init(from: self.decoder)
  1337. }
  1338. guard let entry = self.container[key.stringValue] else {
  1339. throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(_errorDescription(of: key))."))
  1340. }
  1341. self.decoder.codingPath.append(key)
  1342. defer { self.decoder.codingPath.removeLast() }
  1343. guard let value = try self.decoder.unbox(entry, as: type) else {
  1344. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))
  1345. }
  1346. return value
  1347. }
  1348. public func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {
  1349. self.decoder.codingPath.append(key)
  1350. defer { self.decoder.codingPath.removeLast() }
  1351. guard let value = self.container[key.stringValue] else {
  1352. throw DecodingError.keyNotFound(key,
  1353. DecodingError.Context(codingPath: self.codingPath,
  1354. debugDescription: "Cannot get \(KeyedDecodingContainer<NestedKey>.self) -- no value found for key \(_errorDescription(of: key))"))
  1355. }
  1356. guard let dictionary = value as? [String : Any] else {
  1357. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)
  1358. }
  1359. let container = _JSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary)
  1360. return KeyedDecodingContainer(container)
  1361. }
  1362. public func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
  1363. self.decoder.codingPath.append(key)
  1364. defer { self.decoder.codingPath.removeLast() }
  1365. guard let value = self.container[key.stringValue] else {
  1366. throw DecodingError.keyNotFound(key,
  1367. DecodingError.Context(codingPath: self.codingPath,
  1368. debugDescription: "Cannot get UnkeyedDecodingContainer -- no value found for key \(_errorDescription(of: key))"))
  1369. }
  1370. guard let array = value as? [Any] else {
  1371. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)
  1372. }
  1373. return _JSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)
  1374. }
  1375. private func _superDecoder(forKey key: __owned CodingKey) throws -> Decoder {
  1376. self.decoder.codingPath.append(key)
  1377. defer { self.decoder.codingPath.removeLast() }
  1378. let value: Any = self.container[key.stringValue] ?? NSNull()
  1379. return __JSONDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
  1380. }
  1381. public func superDecoder() throws -> Decoder {
  1382. return try _superDecoder(forKey: _JSONKey.super)
  1383. }
  1384. public func superDecoder(forKey key: Key) throws -> Decoder {
  1385. return try _superDecoder(forKey: key)
  1386. }
  1387. }
  1388. fileprivate struct _JSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
  1389. // MARK: Properties
  1390. /// A reference to the decoder we're reading from.
  1391. private let decoder: __JSONDecoder
  1392. /// A reference to the container we're reading from.
  1393. private let container: [Any]
  1394. /// The path of coding keys taken to get to this point in decoding.
  1395. private(set) public var codingPath: [CodingKey]
  1396. /// The index of the element we're about to decode.
  1397. private(set) public var currentIndex: Int
  1398. // MARK: - Initialization
  1399. /// Initializes `self` by referencing the given decoder and container.
  1400. fileprivate init(referencing decoder: __JSONDecoder, wrapping container: [Any]) {
  1401. self.decoder = decoder
  1402. self.container = container
  1403. self.codingPath = decoder.codingPath
  1404. self.currentIndex = 0
  1405. }
  1406. // MARK: - UnkeyedDecodingContainer Methods
  1407. public var count: Int? {
  1408. return self.container.count
  1409. }
  1410. public var isAtEnd: Bool {
  1411. return self.currentIndex >= self.count!
  1412. }
  1413. public mutating func decodeNil() throws -> Bool {
  1414. guard !self.isAtEnd else {
  1415. throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1416. }
  1417. if self.container[self.currentIndex] is NSNull {
  1418. self.currentIndex += 1
  1419. return true
  1420. } else {
  1421. return false
  1422. }
  1423. }
  1424. public mutating func decode(_ type: Bool.Type) throws -> Bool {
  1425. guard !self.isAtEnd else {
  1426. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1427. }
  1428. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1429. defer { self.decoder.codingPath.removeLast() }
  1430. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Bool.self) else {
  1431. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1432. }
  1433. self.currentIndex += 1
  1434. return decoded
  1435. }
  1436. public mutating func decode(_ type: Int.Type) throws -> Int {
  1437. guard !self.isAtEnd else {
  1438. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1439. }
  1440. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1441. defer { self.decoder.codingPath.removeLast() }
  1442. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else {
  1443. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1444. }
  1445. self.currentIndex += 1
  1446. return decoded
  1447. }
  1448. public mutating func decode(_ type: Int8.Type) throws -> Int8 {
  1449. guard !self.isAtEnd else {
  1450. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1451. }
  1452. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1453. defer { self.decoder.codingPath.removeLast() }
  1454. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int8.self) else {
  1455. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1456. }
  1457. self.currentIndex += 1
  1458. return decoded
  1459. }
  1460. public mutating func decode(_ type: Int16.Type) throws -> Int16 {
  1461. guard !self.isAtEnd else {
  1462. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1463. }
  1464. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1465. defer { self.decoder.codingPath.removeLast() }
  1466. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int16.self) else {
  1467. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1468. }
  1469. self.currentIndex += 1
  1470. return decoded
  1471. }
  1472. public mutating func decode(_ type: Int32.Type) throws -> Int32 {
  1473. guard !self.isAtEnd else {
  1474. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1475. }
  1476. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1477. defer { self.decoder.codingPath.removeLast() }
  1478. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int32.self) else {
  1479. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1480. }
  1481. self.currentIndex += 1
  1482. return decoded
  1483. }
  1484. public mutating func decode(_ type: Int64.Type) throws -> Int64 {
  1485. guard !self.isAtEnd else {
  1486. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1487. }
  1488. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1489. defer { self.decoder.codingPath.removeLast() }
  1490. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int64.self) else {
  1491. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1492. }
  1493. self.currentIndex += 1
  1494. return decoded
  1495. }
  1496. public mutating func decode(_ type: UInt.Type) throws -> UInt {
  1497. guard !self.isAtEnd else {
  1498. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1499. }
  1500. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1501. defer { self.decoder.codingPath.removeLast() }
  1502. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt.self) else {
  1503. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1504. }
  1505. self.currentIndex += 1
  1506. return decoded
  1507. }
  1508. public mutating func decode(_ type: UInt8.Type) throws -> UInt8 {
  1509. guard !self.isAtEnd else {
  1510. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1511. }
  1512. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1513. defer { self.decoder.codingPath.removeLast() }
  1514. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt8.self) else {
  1515. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1516. }
  1517. self.currentIndex += 1
  1518. return decoded
  1519. }
  1520. public mutating func decode(_ type: UInt16.Type) throws -> UInt16 {
  1521. guard !self.isAtEnd else {
  1522. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1523. }
  1524. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1525. defer { self.decoder.codingPath.removeLast() }
  1526. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt16.self) else {
  1527. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1528. }
  1529. self.currentIndex += 1
  1530. return decoded
  1531. }
  1532. public mutating func decode(_ type: UInt32.Type) throws -> UInt32 {
  1533. guard !self.isAtEnd else {
  1534. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1535. }
  1536. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1537. defer { self.decoder.codingPath.removeLast() }
  1538. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt32.self) else {
  1539. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1540. }
  1541. self.currentIndex += 1
  1542. return decoded
  1543. }
  1544. public mutating func decode(_ type: UInt64.Type) throws -> UInt64 {
  1545. guard !self.isAtEnd else {
  1546. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1547. }
  1548. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1549. defer { self.decoder.codingPath.removeLast() }
  1550. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt64.self) else {
  1551. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1552. }
  1553. self.currentIndex += 1
  1554. return decoded
  1555. }
  1556. public mutating func decode(_ type: Float.Type) throws -> Float {
  1557. guard !self.isAtEnd else {
  1558. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1559. }
  1560. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1561. defer { self.decoder.codingPath.removeLast() }
  1562. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Float.self) else {
  1563. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1564. }
  1565. self.currentIndex += 1
  1566. return decoded
  1567. }
  1568. public mutating func decode(_ type: Double.Type) throws -> Double {
  1569. guard !self.isAtEnd else {
  1570. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1571. }
  1572. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1573. defer { self.decoder.codingPath.removeLast() }
  1574. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Double.self) else {
  1575. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1576. }
  1577. self.currentIndex += 1
  1578. return decoded
  1579. }
  1580. public mutating func decode(_ type: String.Type) throws -> String {
  1581. guard !self.isAtEnd else {
  1582. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1583. }
  1584. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1585. defer { self.decoder.codingPath.removeLast() }
  1586. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: String.self) else {
  1587. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1588. }
  1589. self.currentIndex += 1
  1590. return decoded
  1591. }
  1592. public mutating func decode<T : Decodable>(_ type: T.Type) throws -> T {
  1593. guard !self.isAtEnd else {
  1594. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))
  1595. }
  1596. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1597. defer { self.decoder.codingPath.removeLast() }
  1598. guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: type) else {
  1599. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
  1600. }
  1601. self.currentIndex += 1
  1602. return decoded
  1603. }
  1604. public mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> {
  1605. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1606. defer { self.decoder.codingPath.removeLast() }
  1607. guard !self.isAtEnd else {
  1608. throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,
  1609. DecodingError.Context(codingPath: self.codingPath,
  1610. debugDescription: "Cannot get nested keyed container -- unkeyed container is at end."))
  1611. }
  1612. let value = self.container[self.currentIndex]
  1613. guard !(value is NSNull) else {
  1614. throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,
  1615. DecodingError.Context(codingPath: self.codingPath,
  1616. debugDescription: "Cannot get keyed decoding container -- found null value instead."))
  1617. }
  1618. guard let dictionary = value as? [String : Any] else {
  1619. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)
  1620. }
  1621. self.currentIndex += 1
  1622. let container = _JSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary)
  1623. return KeyedDecodingContainer(container)
  1624. }
  1625. public mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
  1626. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1627. defer { self.decoder.codingPath.removeLast() }
  1628. guard !self.isAtEnd else {
  1629. throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
  1630. DecodingError.Context(codingPath: self.codingPath,
  1631. debugDescription: "Cannot get nested keyed container -- unkeyed container is at end."))
  1632. }
  1633. let value = self.container[self.currentIndex]
  1634. guard !(value is NSNull) else {
  1635. throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,
  1636. DecodingError.Context(codingPath: self.codingPath,
  1637. debugDescription: "Cannot get keyed decoding container -- found null value instead."))
  1638. }
  1639. guard let array = value as? [Any] else {
  1640. throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)
  1641. }
  1642. self.currentIndex += 1
  1643. return _JSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)
  1644. }
  1645. public mutating func superDecoder() throws -> Decoder {
  1646. self.decoder.codingPath.append(_JSONKey(index: self.currentIndex))
  1647. defer { self.decoder.codingPath.removeLast() }
  1648. guard !self.isAtEnd else {
  1649. throw DecodingError.valueNotFound(Decoder.self,
  1650. DecodingError.Context(codingPath: self.codingPath,
  1651. debugDescription: "Cannot get superDecoder() -- unkeyed container is at end."))
  1652. }
  1653. let value = self.container[self.currentIndex]
  1654. self.currentIndex += 1
  1655. return __JSONDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)
  1656. }
  1657. }
  1658. extension __JSONDecoder : SingleValueDecodingContainer {
  1659. // MARK: SingleValueDecodingContainer Methods
  1660. private func expectNonNull<T>(_ type: T.Type) throws {
  1661. guard !self.decodeNil() else {
  1662. throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected \(type) but found null value instead."))
  1663. }
  1664. }
  1665. public func decodeNil() -> Bool {
  1666. return self.storage.topContainer is NSNull
  1667. }
  1668. public func decode(_ type: Bool.Type) throws -> Bool {
  1669. try expectNonNull(Bool.self)
  1670. return try self.unbox(self.storage.topContainer, as: Bool.self)!
  1671. }
  1672. public func decode(_ type: Int.Type) throws -> Int {
  1673. try expectNonNull(Int.self)
  1674. return try self.unbox(self.storage.topContainer, as: Int.self)!
  1675. }
  1676. public func decode(_ type: Int8.Type) throws -> Int8 {
  1677. try expectNonNull(Int8.self)
  1678. return try self.unbox(self.storage.topContainer, as: Int8.self)!
  1679. }
  1680. public func decode(_ type: Int16.Type) throws -> Int16 {
  1681. try expectNonNull(Int16.self)
  1682. return try self.unbox(self.storage.topContainer, as: Int16.self)!
  1683. }
  1684. public func decode(_ type: Int32.Type) throws -> Int32 {
  1685. try expectNonNull(Int32.self)
  1686. return try self.unbox(self.storage.topContainer, as: Int32.self)!
  1687. }
  1688. public func decode(_ type: Int64.Type) throws -> Int64 {
  1689. try expectNonNull(Int64.self)
  1690. return try self.unbox(self.storage.topContainer, as: Int64.self)!
  1691. }
  1692. public func decode(_ type: UInt.Type) throws -> UInt {
  1693. try expectNonNull(UInt.self)
  1694. return try self.unbox(self.storage.topContainer, as: UInt.self)!
  1695. }
  1696. public func decode(_ type: UInt8.Type) throws -> UInt8 {
  1697. try expectNonNull(UInt8.self)
  1698. return try self.unbox(self.storage.topContainer, as: UInt8.self)!
  1699. }
  1700. public func decode(_ type: UInt16.Type) throws -> UInt16 {
  1701. try expectNonNull(UInt16.self)
  1702. return try self.unbox(self.storage.topContainer, as: UInt16.self)!
  1703. }
  1704. public func decode(_ type: UInt32.Type) throws -> UInt32 {
  1705. try expectNonNull(UInt32.self)
  1706. return try self.unbox(self.storage.topContainer, as: UInt32.self)!
  1707. }
  1708. public func decode(_ type: UInt64.Type) throws -> UInt64 {
  1709. try expectNonNull(UInt64.self)
  1710. return try self.unbox(self.storage.topContainer, as: UInt64.self)!
  1711. }
  1712. public func decode(_ type: Float.Type) throws -> Float {
  1713. try expectNonNull(Float.self)
  1714. return try self.unbox(self.storage.topContainer, as: Float.self)!
  1715. }
  1716. public func decode(_ type: Double.Type) throws -> Double {
  1717. try expectNonNull(Double.self)
  1718. return try self.unbox(self.storage.topContainer, as: Double.self)!
  1719. }
  1720. public func decode(_ type: String.Type) throws -> String {
  1721. try expectNonNull(String.self)
  1722. return try self.unbox(self.storage.topContainer, as: String.self)!
  1723. }
  1724. public func decode<T : Decodable>(_ type: T.Type) throws -> T {
  1725. try expectNonNull(type)
  1726. return try self.unbox(self.storage.topContainer, as: type)!
  1727. }
  1728. }
  1729. // MARK: - Concrete Value Representations
  1730. extension __JSONDecoder {
  1731. /// Returns the given value unboxed from a container.
  1732. fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
  1733. guard !(value is NSNull) else { return nil }
  1734. if let rcValue = value as? FirebaseRemoteConfigValueDecoding {
  1735. return rcValue.boolValue()
  1736. }
  1737. if let number = value as? NSNumber {
  1738. // TODO: Add a flag to coerce non-boolean numbers into Bools?
  1739. if number === kCFBooleanTrue as NSNumber {
  1740. return true
  1741. } else if number === kCFBooleanFalse as NSNumber {
  1742. return false
  1743. }
  1744. /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
  1745. } else if let bool = value as? Bool {
  1746. return bool
  1747. */
  1748. }
  1749. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1750. }
  1751. fileprivate func rcValNumberAdaptor(_ value: Any) -> Any {
  1752. if let rcValue = value as? FirebaseRemoteConfigValueDecoding {
  1753. return rcValue.numberValue()
  1754. }
  1755. return value
  1756. }
  1757. fileprivate func getNumber(_ value: Any, as type: Any.Type) throws -> NSNumber {
  1758. let val = rcValNumberAdaptor(value)
  1759. guard let number = val as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse else {
  1760. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: val)
  1761. }
  1762. return number
  1763. }
  1764. fileprivate func unbox(_ value: Any, as type: Int.Type) throws -> Int? {
  1765. guard !(value is NSNull) else { return nil }
  1766. let number = try getNumber(value, as: type)
  1767. let int = number.intValue
  1768. guard NSNumber(value: int) == number else {
  1769. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1770. }
  1771. return int
  1772. }
  1773. fileprivate func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? {
  1774. guard !(value is NSNull) else { return nil }
  1775. let number = try getNumber(value, as: type)
  1776. let int8 = number.int8Value
  1777. guard NSNumber(value: int8) == number else {
  1778. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1779. }
  1780. return int8
  1781. }
  1782. fileprivate func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? {
  1783. guard !(value is NSNull) else { return nil }
  1784. let number = try getNumber(value, as: type)
  1785. let int16 = number.int16Value
  1786. guard NSNumber(value: int16) == number else {
  1787. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1788. }
  1789. return int16
  1790. }
  1791. fileprivate func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? {
  1792. guard !(value is NSNull) else { return nil }
  1793. let number = try getNumber(value, as: type)
  1794. let int32 = number.int32Value
  1795. guard NSNumber(value: int32) == number else {
  1796. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1797. }
  1798. return int32
  1799. }
  1800. fileprivate func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? {
  1801. guard !(value is NSNull) else { return nil }
  1802. let number = try getNumber(value, as: type)
  1803. let int64 = number.int64Value
  1804. guard NSNumber(value: int64) == number else {
  1805. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1806. }
  1807. return int64
  1808. }
  1809. fileprivate func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? {
  1810. guard !(value is NSNull) else { return nil }
  1811. let number = try getNumber(value, as: type)
  1812. let uint = number.uintValue
  1813. guard NSNumber(value: uint) == number else {
  1814. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1815. }
  1816. return uint
  1817. }
  1818. fileprivate func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? {
  1819. guard !(value is NSNull) else { return nil }
  1820. let number = try getNumber(value, as: type)
  1821. let uint8 = number.uint8Value
  1822. guard NSNumber(value: uint8) == number else {
  1823. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1824. }
  1825. return uint8
  1826. }
  1827. fileprivate func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? {
  1828. guard !(value is NSNull) else { return nil }
  1829. let number = try getNumber(value, as: type)
  1830. let uint16 = number.uint16Value
  1831. guard NSNumber(value: uint16) == number else {
  1832. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1833. }
  1834. return uint16
  1835. }
  1836. fileprivate func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? {
  1837. guard !(value is NSNull) else { return nil }
  1838. let number = try getNumber(value, as: type)
  1839. let uint32 = number.uint32Value
  1840. guard NSNumber(value: uint32) == number else {
  1841. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1842. }
  1843. return uint32
  1844. }
  1845. fileprivate func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? {
  1846. guard !(value is NSNull) else { return nil }
  1847. let number = try getNumber(value, as: type)
  1848. let uint64 = number.uint64Value
  1849. guard NSNumber(value: uint64) == number else {
  1850. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number <\(number)> does not fit in \(type)."))
  1851. }
  1852. return uint64
  1853. }
  1854. fileprivate func unbox(_ value: Any, as type: Float.Type) throws -> Float? {
  1855. guard !(value is NSNull) else { return nil }
  1856. let val = rcValNumberAdaptor(value)
  1857. if let number = val as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse {
  1858. // We are willing to return a Float by losing precision:
  1859. // * If the original value was integral,
  1860. // * and the integral value was > Float.greatestFiniteMagnitude, we will fail
  1861. // * and the integral value was <= Float.greatestFiniteMagnitude, we are willing to lose precision past 2^24
  1862. // * If it was a Float, you will get back the precise value
  1863. // * If it was a Double or Decimal, you will get back the nearest approximation if it will fit
  1864. let double = number.doubleValue
  1865. guard abs(double) <= Double(Float.greatestFiniteMagnitude) else {
  1866. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed JSON number \(number) does not fit in \(type)."))
  1867. }
  1868. return Float(double)
  1869. /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
  1870. } else if let double = value as? Double {
  1871. if abs(double) <= Double(Float.max) {
  1872. return Float(double)
  1873. }
  1874. overflow = true
  1875. } else if let int = value as? Int {
  1876. if let float = Float(exactly: int) {
  1877. return float
  1878. }
  1879. overflow = true
  1880. */
  1881. } else if let string = val as? String,
  1882. case .convertFromString(let posInfString, let negInfString, let nanString) = self.options.nonConformingFloatDecodingStrategy {
  1883. if string == posInfString {
  1884. return Float.infinity
  1885. } else if string == negInfString {
  1886. return -Float.infinity
  1887. } else if string == nanString {
  1888. return Float.nan
  1889. }
  1890. }
  1891. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1892. }
  1893. fileprivate func unbox(_ value: Any, as type: Double.Type) throws -> Double? {
  1894. guard !(value is NSNull) else { return nil }
  1895. let val = rcValNumberAdaptor(value)
  1896. if let number = val as? NSNumber, number !== kCFBooleanTrue, number !== kCFBooleanFalse {
  1897. // We are always willing to return the number as a Double:
  1898. // * If the original value was integral, it is guaranteed to fit in a Double; we are willing to lose precision past 2^53 if you encoded a UInt64 but requested a Double
  1899. // * If it was a Float or Double, you will get back the precise value
  1900. // * If it was Decimal, you will get back the nearest approximation
  1901. return number.doubleValue
  1902. /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
  1903. } else if let double = value as? Double {
  1904. return double
  1905. } else if let int = value as? Int {
  1906. if let double = Double(exactly: int) {
  1907. return double
  1908. }
  1909. overflow = true
  1910. */
  1911. } else if let string = val as? String,
  1912. case .convertFromString(let posInfString, let negInfString, let nanString) = self.options.nonConformingFloatDecodingStrategy {
  1913. if string == posInfString {
  1914. return Double.infinity
  1915. } else if string == negInfString {
  1916. return -Double.infinity
  1917. } else if string == nanString {
  1918. return Double.nan
  1919. }
  1920. }
  1921. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1922. }
  1923. fileprivate func unbox(_ value: Any, as type: String.Type) throws -> String? {
  1924. guard !(value is NSNull) else { return nil }
  1925. if let rcValue = value as? FirebaseRemoteConfigValueDecoding {
  1926. return rcValue.stringValue()
  1927. }
  1928. guard let string = value as? String else {
  1929. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1930. }
  1931. return string
  1932. }
  1933. fileprivate func unbox(_ value: Any, as type: Date.Type) throws -> Date? {
  1934. guard !(value is NSNull) else { return nil }
  1935. switch self.options.dateDecodingStrategy {
  1936. case .deferredToDate:
  1937. self.storage.push(container: value)
  1938. defer { self.storage.popContainer() }
  1939. return try Date(from: self)
  1940. case .secondsSince1970:
  1941. let double = try self.unbox(value, as: Double.self)!
  1942. return Date(timeIntervalSince1970: double)
  1943. case .millisecondsSince1970:
  1944. let double = try self.unbox(value, as: Double.self)!
  1945. return Date(timeIntervalSince1970: double / 1000.0)
  1946. case .iso8601:
  1947. if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
  1948. let string = try self.unbox(value, as: String.self)!
  1949. guard let date = _iso8601Formatter.date(from: string) else {
  1950. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
  1951. }
  1952. return date
  1953. } else {
  1954. fatalError("ISO8601DateFormatter is unavailable on this platform.")
  1955. }
  1956. case .formatted(let formatter):
  1957. let string = try self.unbox(value, as: String.self)!
  1958. guard let date = formatter.date(from: string) else {
  1959. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter."))
  1960. }
  1961. return date
  1962. case .custom(let closure):
  1963. self.storage.push(container: value)
  1964. defer { self.storage.popContainer() }
  1965. return try closure(self)
  1966. }
  1967. }
  1968. fileprivate func unbox(_ value: Any, as type: Data.Type) throws -> Data? {
  1969. guard !(value is NSNull) else { return nil }
  1970. if let rcValue = value as? FirebaseRemoteConfigValueDecoding {
  1971. return rcValue.dataValue()
  1972. }
  1973. switch self.options.dataDecodingStrategy {
  1974. case .deferredToData:
  1975. self.storage.push(container: value)
  1976. defer { self.storage.popContainer() }
  1977. return try Data(from: self)
  1978. case .base64:
  1979. guard let string = value as? String else {
  1980. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1981. }
  1982. guard let data = Data(base64Encoded: string) else {
  1983. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64."))
  1984. }
  1985. return data
  1986. case .blob:
  1987. if let data = value as? Data {
  1988. return data
  1989. } else if let string = value as? String, let data = Data(base64Encoded: string) {
  1990. // Support implicit migration of data that was written with .base64 (String type) using
  1991. // Firestore 10.0 through 10.3.
  1992. return data
  1993. }
  1994. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  1995. case .custom(let closure):
  1996. self.storage.push(container: value)
  1997. defer { self.storage.popContainer() }
  1998. return try closure(self)
  1999. }
  2000. }
  2001. fileprivate func unbox(_ value: Any, as type: Decimal.Type) throws -> Decimal? {
  2002. guard !(value is NSNull) else { return nil }
  2003. let val = rcValNumberAdaptor(value)
  2004. // Attempt to bridge from NSDecimalNumber.
  2005. if let decimal = val as? Decimal {
  2006. return decimal
  2007. } else {
  2008. let doubleValue = try self.unbox(val, as: Double.self)!
  2009. return Decimal(doubleValue)
  2010. }
  2011. }
  2012. fileprivate func unbox<T>(_ value: Any, as type: _JSONStringDictionaryDecodableMarker.Type) throws -> T? {
  2013. guard !(value is NSNull) else { return nil }
  2014. if let rcValue = value as? FirebaseRemoteConfigValueDecoding {
  2015. guard let dictionaryValue = rcValue.dictionaryValue() else {
  2016. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: rcValue)
  2017. }
  2018. return dictionaryValue as? T
  2019. }
  2020. var result = [String : Any]()
  2021. guard let dict = value as? NSDictionary else {
  2022. throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
  2023. }
  2024. let elementType = type.elementType
  2025. for (key, value) in dict {
  2026. let key = key as! String
  2027. self.codingPath.append(_JSONKey(stringValue: key, intValue: nil))
  2028. defer { self.codingPath.removeLast() }
  2029. result[key] = try unbox_(value, as: elementType)
  2030. }
  2031. return result as? T
  2032. }
  2033. fileprivate func unbox<T : Decodable>(_ value: Any, as type: T.Type) throws -> T? {
  2034. return try unbox_(value, as: type) as? T
  2035. }
  2036. fileprivate func unbox_(_ value: Any, as _type: Decodable.Type) throws -> Any? {
  2037. if _type == Date.self || _type == NSDate.self {
  2038. return try self.unbox(value, as: Date.self)
  2039. } else if _type == Data.self || _type == NSData.self {
  2040. return try self.unbox(value, as: Data.self)
  2041. } else if _type == URL.self || _type == NSURL.self {
  2042. guard let urlString = try self.unbox(value, as: String.self) else {
  2043. return nil
  2044. }
  2045. guard let url = URL(string: urlString) else {
  2046. throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath,
  2047. debugDescription: "Invalid URL string."))
  2048. }
  2049. return url
  2050. } else if _type == Decimal.self || _type == NSDecimalNumber.self {
  2051. return try self.unbox(value, as: Decimal.self)
  2052. } else if let stringKeyedDictType = _type as? _JSONStringDictionaryDecodableMarker.Type {
  2053. return try self.unbox(value, as: stringKeyedDictType)
  2054. } else {
  2055. self.storage.push(container: value)
  2056. defer { self.storage.popContainer() }
  2057. if self.options.passthroughTypeResolver.isPassthroughType(value) && type(of: value) == _type {
  2058. return value
  2059. }
  2060. return try _type.init(from: self)
  2061. }
  2062. }
  2063. }
  2064. //===----------------------------------------------------------------------===//
  2065. // Shared Key Types
  2066. //===----------------------------------------------------------------------===//
  2067. fileprivate struct _JSONKey : CodingKey {
  2068. public var stringValue: String
  2069. public var intValue: Int?
  2070. public init?(stringValue: String) {
  2071. self.stringValue = stringValue
  2072. self.intValue = nil
  2073. }
  2074. public init?(intValue: Int) {
  2075. self.stringValue = "\(intValue)"
  2076. self.intValue = intValue
  2077. }
  2078. public init(stringValue: String, intValue: Int?) {
  2079. self.stringValue = stringValue
  2080. self.intValue = intValue
  2081. }
  2082. fileprivate init(index: Int) {
  2083. self.stringValue = "Index \(index)"
  2084. self.intValue = index
  2085. }
  2086. fileprivate static let `super` = _JSONKey(stringValue: "super")!
  2087. }
  2088. //===----------------------------------------------------------------------===//
  2089. // Shared ISO8601 Date Formatter
  2090. //===----------------------------------------------------------------------===//
  2091. // NOTE: This value is implicitly lazy and _must_ be lazy. We're compiled against the latest SDK (w/ ISO8601DateFormatter), but linked against whichever Foundation the user has. ISO8601DateFormatter might not exist, so we better not hit this code path on an older OS.
  2092. @available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
  2093. fileprivate var _iso8601Formatter: ISO8601DateFormatter = {
  2094. let formatter = ISO8601DateFormatter()
  2095. formatter.formatOptions = .withInternetDateTime
  2096. return formatter
  2097. }()
  2098. //===----------------------------------------------------------------------===//
  2099. // Error Utilities
  2100. //===----------------------------------------------------------------------===//
  2101. extension EncodingError {
  2102. /// Returns a `.invalidValue` error describing the given invalid floating-point value.
  2103. ///
  2104. ///
  2105. /// - parameter value: The value that was invalid to encode.
  2106. /// - parameter path: The path of `CodingKey`s taken to encode this value.
  2107. /// - returns: An `EncodingError` with the appropriate path and debug description.
  2108. fileprivate static func _invalidFloatingPointValue<T : FloatingPoint>(_ value: T, at codingPath: [CodingKey]) -> EncodingError {
  2109. let valueDescription: String
  2110. if value == T.infinity {
  2111. valueDescription = "\(T.self).infinity"
  2112. } else if value == -T.infinity {
  2113. valueDescription = "-\(T.self).infinity"
  2114. } else {
  2115. valueDescription = "\(T.self).nan"
  2116. }
  2117. let debugDescription = "Unable to encode \(valueDescription) directly in JSON. Use JSONEncoder.NonConformingFloatEncodingStrategy.convertToString to specify how the value should be encoded."
  2118. return .invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: debugDescription))
  2119. }
  2120. }