/
JSONDecoder.swift
1016 lines (839 loc) · 39.1 KB
/
JSONDecoder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CoreFoundation
/// A marker protocol used to determine whether a value is a `String`-keyed `Dictionary`
/// containing `Decodable` values (in which case it should be exempt from key conversion strategies).
///
/// The marker protocol also provides access to the type of the `Decodable` values,
/// which is needed for the implementation of the key conversion strategy exemption.
///
fileprivate protocol _JSONStringDictionaryDecodableMarker {
static var elementType: Decodable.Type { get }
}
extension Dictionary: _JSONStringDictionaryDecodableMarker where Key == String, Value: Decodable {
static var elementType: Decodable.Type { return Value.self }
}
//===----------------------------------------------------------------------===//
// JSON Decoder
//===----------------------------------------------------------------------===//
/// `JSONDecoder` facilitates the decoding of JSON into semantic `Decodable` types.
open class JSONDecoder {
// MARK: Options
/// The strategy to use for decoding `Date` values.
public enum DateDecodingStrategy {
/// Defer to `Date` for decoding. This is the default strategy.
case deferredToDate
/// Decode the `Date` as a UNIX timestamp from a JSON number.
case secondsSince1970
/// Decode the `Date` as UNIX millisecond timestamp from a JSON number.
case millisecondsSince1970
/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601
/// Decode the `Date` as a string parsed by the given formatter.
case formatted(DateFormatter)
/// Decode the `Date` as a custom value decoded by the given closure.
case custom((_ decoder: Decoder) throws -> Date)
}
/// The strategy to use for decoding `Data` values.
public enum DataDecodingStrategy {
/// Defer to `Data` for decoding.
case deferredToData
/// Decode the `Data` from a Base64-encoded string. This is the default strategy.
case base64
/// Decode the `Data` as a custom value decoded by the given closure.
case custom((_ decoder: Decoder) throws -> Data)
}
/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
public enum NonConformingFloatDecodingStrategy {
/// Throw upon encountering non-conforming values. This is the default strategy.
case `throw`
/// Decode the values from the given representation strings.
case convertFromString(positiveInfinity: String, negativeInfinity: String, nan: String)
}
/// The strategy to use for automatically changing the value of keys before decoding.
public enum KeyDecodingStrategy {
/// Use the keys specified by each type. This is the default strategy.
case useDefaultKeys
/// Convert from "snake_case_keys" to "camelCaseKeys" before attempting to match a key with the one specified by each type.
///
/// 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.
///
/// Converting from snake case to camel case:
/// 1. Capitalizes the word starting after each `_`
/// 2. Removes all `_`
/// 3. Preserves starting and ending `_` (as these are often used to indicate private variables or other metadata).
/// For example, `one_two_three` becomes `oneTwoThree`. `_one_two_three_` becomes `_oneTwoThree_`.
///
/// - Note: Using a key decoding strategy has a nominal performance cost, as each string key has to be inspected for the `_` character.
case convertFromSnakeCase
/// Provide a custom conversion from the key in the encoded JSON to the keys specified by the decoded types.
/// The full path to the current decoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before decoding.
/// If the result of the conversion is a duplicate key, then only one value will be present in the container for the type to decode from.
case custom((_ codingPath: [CodingKey]) -> CodingKey)
fileprivate static func _convertFromSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
// Find the first non-underscore character
guard let firstNonUnderscore = stringKey.firstIndex(where: { $0 != "_" }) else {
// Reached the end without finding an _
return stringKey
}
// Find the last non-underscore character
var lastNonUnderscore = stringKey.index(before: stringKey.endIndex)
while lastNonUnderscore > firstNonUnderscore && stringKey[lastNonUnderscore] == "_" {
stringKey.formIndex(before: &lastNonUnderscore)
}
let keyRange = firstNonUnderscore...lastNonUnderscore
let leadingUnderscoreRange = stringKey.startIndex..<firstNonUnderscore
let trailingUnderscoreRange = stringKey.index(after: lastNonUnderscore)..<stringKey.endIndex
let components = stringKey[keyRange].split(separator: "_")
let joinedString: String
if components.count == 1 {
// No underscores in key, leave the word as is - maybe already camel cased
joinedString = String(stringKey[keyRange])
} else {
joinedString = ([components[0].lowercased()] + components[1...].map { $0.capitalized }).joined()
}
// Do a cheap isEmpty check before creating and appending potentially empty strings
let result: String
if (leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty) {
result = joinedString
} else if (!leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty) {
// Both leading and trailing underscores
result = String(stringKey[leadingUnderscoreRange]) + joinedString + String(stringKey[trailingUnderscoreRange])
} else if (!leadingUnderscoreRange.isEmpty) {
// Just leading
result = String(stringKey[leadingUnderscoreRange]) + joinedString
} else {
// Just trailing
result = joinedString + String(stringKey[trailingUnderscoreRange])
}
return result
}
}
/// The strategy to use in decoding dates. Defaults to `.deferredToDate`.
open var dateDecodingStrategy: DateDecodingStrategy = .deferredToDate
/// The strategy to use in decoding binary data. Defaults to `.base64`.
open var dataDecodingStrategy: DataDecodingStrategy = .base64
/// The strategy to use in decoding non-conforming numbers. Defaults to `.throw`.
open var nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy = .throw
/// The strategy to use for decoding keys. Defaults to `.useDefaultKeys`.
open var keyDecodingStrategy: KeyDecodingStrategy = .useDefaultKeys
/// Contextual user-provided information for use during decoding.
open var userInfo: [CodingUserInfoKey: Any] = [:]
/// Options set on the top-level encoder to pass down the decoding hierarchy.
fileprivate struct _Options {
let dateDecodingStrategy: DateDecodingStrategy
let dataDecodingStrategy: DataDecodingStrategy
let nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy
let keyDecodingStrategy: KeyDecodingStrategy
let userInfo: [CodingUserInfoKey: Any]
}
/// The options set on the top-level decoder.
fileprivate var options: _Options {
return _Options(dateDecodingStrategy: dateDecodingStrategy,
dataDecodingStrategy: dataDecodingStrategy,
nonConformingFloatDecodingStrategy: nonConformingFloatDecodingStrategy,
keyDecodingStrategy: keyDecodingStrategy,
userInfo: userInfo)
}
// MARK: - Constructing a JSON Decoder
/// Initializes `self` with default strategies.
public init() {}
// MARK: - Decoding Values
/// Decodes a top-level value of the given type from the given JSON representation.
///
/// - parameter type: The type of the value to decode.
/// - parameter data: The data to decode from.
/// - returns: A value of the requested type.
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
/// - throws: An error if any value throws an error during decoding.
open func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
do {
var parser = JSONParser(bytes: Array(data))
let json = try parser.parse()
return try JSONDecoderImpl(userInfo: self.userInfo, from: json, codingPath: [], options: self.options).unwrap(as: T.self)
} catch let error as JSONError {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error))
} catch {
throw error
}
}
}
// MARK: - _JSONDecoder
fileprivate struct JSONDecoderImpl {
let codingPath: [CodingKey]
let userInfo: [CodingUserInfoKey: Any]
let json: JSONValue
let options: JSONDecoder._Options
init(userInfo: [CodingUserInfoKey: Any], from json: JSONValue, codingPath: [CodingKey], options: JSONDecoder._Options) {
self.userInfo = userInfo
self.codingPath = codingPath
self.json = json
self.options = options
}
}
extension JSONDecoderImpl: Decoder {
@usableFromInline func container<Key>(keyedBy _: Key.Type) throws ->
KeyedDecodingContainer<Key> where Key: CodingKey
{
switch self.json {
case .object(let dictionary):
let container = KeyedContainer<Key>(
impl: self,
codingPath: codingPath,
dictionary: dictionary
)
return KeyedDecodingContainer(container)
case .null:
throw DecodingError.valueNotFound([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Cannot get keyed decoding container -- found null value instead"
))
default:
throw DecodingError.typeMismatch([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Expected to decode \([String: JSONValue].self) but found \(self.json.debugDataTypeDescription) instead."
))
}
}
@usableFromInline func unkeyedContainer() throws -> UnkeyedDecodingContainer {
switch self.json {
case .array(let array):
return UnkeyedContainer(
impl: self,
codingPath: self.codingPath,
array: array
)
case .null:
throw DecodingError.valueNotFound([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Cannot get unkeyed decoding container -- found null value instead"
))
default:
throw DecodingError.typeMismatch([JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Expected to decode \([JSONValue].self) but found \(self.json.debugDataTypeDescription) instead."
))
}
}
@usableFromInline func singleValueContainer() throws -> SingleValueDecodingContainer {
SingleValueContainer(
impl: self,
codingPath: self.codingPath,
json: self.json
)
}
// MARK: Special case handling
func unwrap<T: Decodable>(as type: T.Type) throws -> T {
if type == Date.self {
return try self.unwrapDate() as! T
}
if type == Data.self {
return try self.unwrapData() as! T
}
if type == URL.self {
return try self.unwrapURL() as! T
}
if type == Decimal.self {
return try self.unwrapDecimal() as! T
}
if T.self is _JSONStringDictionaryDecodableMarker.Type {
return try self.unwrapDictionary(as: T.self)
}
return try T(from: self)
}
private func unwrapDate() throws -> Date {
switch self.options.dateDecodingStrategy {
case .deferredToDate:
return try Date(from: self)
case .secondsSince1970:
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let double = try container.decode(Double.self)
return Date(timeIntervalSince1970: double)
case .millisecondsSince1970:
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let double = try container.decode(Double.self)
return Date(timeIntervalSince1970: double / 1000.0)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let string = try container.decode(String.self)
guard let date = _iso8601Formatter.date(from: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected date string to be ISO8601-formatted."))
}
return date
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .formatted(let formatter):
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let string = try container.decode(String.self)
guard let date = formatter.date(from: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Date string does not match format expected by formatter."))
}
return date
case .custom(let closure):
return try closure(self)
}
}
private func unwrapData() throws -> Data {
switch self.options.dataDecodingStrategy {
case .deferredToData:
return try Data(from: self)
case .base64:
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let string = try container.decode(String.self)
guard let data = Data(base64Encoded: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Encountered Data is not valid Base64."))
}
return data
case .custom(let closure):
return try closure(self)
}
}
private func unwrapURL() throws -> URL {
let container = SingleValueContainer(impl: self, codingPath: self.codingPath, json: self.json)
let string = try container.decode(String.self)
guard let url = URL(string: string) else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath,
debugDescription: "Invalid URL string."))
}
return url
}
private func unwrapDecimal() throws -> Decimal {
guard case .number(let numberString) = self.json else {
throw DecodingError.typeMismatch(Decimal.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
guard let decimal = Decimal(string: numberString) else {
throw DecodingError.dataCorrupted(.init(
codingPath: self.codingPath,
debugDescription: "Parsed JSON number <\(numberString)> does not fit in \(Decimal.self)."))
}
return decimal
}
private func unwrapDictionary<T: Decodable>(as: T.Type) throws -> T {
guard let dictType = T.self as? (_JSONStringDictionaryDecodableMarker & Decodable).Type else {
preconditionFailure("Must only be called of T implements _JSONStringDictionaryDecodableMarker")
}
guard case .object(let object) = self.json else {
throw DecodingError.typeMismatch([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Expected to decode \([String: JSONValue].self) but found \(self.json.debugDataTypeDescription) instead."
))
}
var result = [String: Any]()
for (key, value) in object {
var newPath = self.codingPath
newPath.append(_JSONKey(stringValue: key)!)
let newDecoder = JSONDecoderImpl(userInfo: self.userInfo, from: value, codingPath: newPath, options: self.options)
result[key] = try dictType.elementType.createByDirectlyUnwrapping(from: newDecoder)
}
return result as! T
}
private func unwrapFloatingPoint<T: LosslessStringConvertible & BinaryFloatingPoint>(
from value: JSONValue,
for additionalKey: CodingKey? = nil,
as type: T.Type) throws -> T
{
if case .number(let number) = value {
guard let floatingPoint = T(number), floatingPoint.isFinite else {
var path = self.codingPath
if let additionalKey = additionalKey {
path.append(additionalKey)
}
throw DecodingError.dataCorrupted(.init(
codingPath: path,
debugDescription: "Parsed JSON number <\(number)> does not fit in \(T.self)."))
}
return floatingPoint
}
if case .string(let string) = value,
case .convertFromString(let posInfString, let negInfString, let nanString) =
self.options.nonConformingFloatDecodingStrategy
{
if string == posInfString {
return T.infinity
} else if string == negInfString {
return -T.infinity
} else if string == nanString {
return T.nan
}
}
throw self.createTypeMismatchError(type: T.self, for: additionalKey, value: value)
}
private func unwrapFixedWidthInteger<T: FixedWidthInteger>(
from value: JSONValue,
for additionalKey: CodingKey? = nil,
as type: T.Type) throws -> T
{
guard case .number(let number) = value else {
throw self.createTypeMismatchError(type: T.self, for: additionalKey, value: value)
}
// this is the fast pass. Number directly convertible to Integer
if let integer = T(number) {
return integer
}
// this is the really slow path... If the fast path has failed. For example for "34.0" as
// an integer, we try to go through NSNumber
if let nsNumber = NSNumber.fromJSONNumber(number) {
if type == UInt8.self, NSNumber(value: nsNumber.uint8Value) == nsNumber {
return nsNumber.uint8Value as! T
}
if type == Int8.self, NSNumber(value: nsNumber.int8Value) == nsNumber {
return nsNumber.int8Value as! T
}
if type == UInt16.self, NSNumber(value: nsNumber.uint16Value) == nsNumber {
return nsNumber.uint16Value as! T
}
if type == Int16.self, NSNumber(value: nsNumber.int16Value) == nsNumber {
return nsNumber.int16Value as! T
}
if type == UInt32.self, NSNumber(value: nsNumber.uint32Value) == nsNumber {
return nsNumber.uint32Value as! T
}
if type == Int32.self, NSNumber(value: nsNumber.int32Value) == nsNumber {
return nsNumber.int32Value as! T
}
if type == UInt64.self, NSNumber(value: nsNumber.uint64Value) == nsNumber {
return nsNumber.uint64Value as! T
}
if type == Int64.self, NSNumber(value: nsNumber.int64Value) == nsNumber {
return nsNumber.int64Value as! T
}
if type == UInt.self, NSNumber(value: nsNumber.uintValue) == nsNumber {
return nsNumber.uintValue as! T
}
if type == Int.self, NSNumber(value: nsNumber.intValue) == nsNumber {
return nsNumber.intValue as! T
}
}
var path = self.codingPath
if let additionalKey = additionalKey {
path.append(additionalKey)
}
throw DecodingError.dataCorrupted(.init(
codingPath: path,
debugDescription: "Parsed JSON number <\(number)> does not fit in \(T.self)."))
}
private func createTypeMismatchError(type: Any.Type, for additionalKey: CodingKey? = nil, value: JSONValue) -> DecodingError {
var path = self.codingPath
if let additionalKey = additionalKey {
path.append(additionalKey)
}
return DecodingError.typeMismatch(type, .init(
codingPath: path,
debugDescription: "Expected to decode \(type) but found \(value.debugDataTypeDescription) instead."
))
}
}
extension Decodable {
fileprivate static func createByDirectlyUnwrapping(from decoder: JSONDecoderImpl) throws -> Self {
if Self.self == URL.self
|| Self.self == Date.self
|| Self.self == Data.self
|| Self.self == Decimal.self
|| Self.self is _JSONStringDictionaryDecodableMarker.Type
{
return try decoder.unwrap(as: Self.self)
}
return try Self.init(from: decoder)
}
}
extension JSONDecoderImpl {
struct SingleValueContainer: SingleValueDecodingContainer {
let impl: JSONDecoderImpl
let value: JSONValue
let codingPath: [CodingKey]
init(impl: JSONDecoderImpl, codingPath: [CodingKey], json: JSONValue) {
self.impl = impl
self.codingPath = codingPath
self.value = json
}
func decodeNil() -> Bool {
self.value == .null
}
func decode(_: Bool.Type) throws -> Bool {
guard case .bool(let bool) = self.value else {
throw self.impl.createTypeMismatchError(type: Bool.self, value: self.value)
}
return bool
}
func decode(_: String.Type) throws -> String {
guard case .string(let string) = self.value else {
throw self.impl.createTypeMismatchError(type: String.self, value: self.value)
}
return string
}
func decode(_: Double.Type) throws -> Double {
try decodeFloatingPoint()
}
func decode(_: Float.Type) throws -> Float {
try decodeFloatingPoint()
}
func decode(_: Int.Type) throws -> Int {
try decodeFixedWidthInteger()
}
func decode(_: Int8.Type) throws -> Int8 {
try decodeFixedWidthInteger()
}
func decode(_: Int16.Type) throws -> Int16 {
try decodeFixedWidthInteger()
}
func decode(_: Int32.Type) throws -> Int32 {
try decodeFixedWidthInteger()
}
func decode(_: Int64.Type) throws -> Int64 {
try decodeFixedWidthInteger()
}
func decode(_: UInt.Type) throws -> UInt {
try decodeFixedWidthInteger()
}
func decode(_: UInt8.Type) throws -> UInt8 {
try decodeFixedWidthInteger()
}
func decode(_: UInt16.Type) throws -> UInt16 {
try decodeFixedWidthInteger()
}
func decode(_: UInt32.Type) throws -> UInt32 {
try decodeFixedWidthInteger()
}
func decode(_: UInt64.Type) throws -> UInt64 {
try decodeFixedWidthInteger()
}
func decode<T>(_: T.Type) throws -> T where T: Decodable {
try self.impl.unwrap(as: T.self)
}
@inline(__always) private func decodeFixedWidthInteger<T: FixedWidthInteger>() throws -> T {
try self.impl.unwrapFixedWidthInteger(from: self.value, as: T.self)
}
@inline(__always) private func decodeFloatingPoint<T: LosslessStringConvertible & BinaryFloatingPoint>() throws -> T {
try self.impl.unwrapFloatingPoint(from: self.value, as: T.self)
}
}
}
extension JSONDecoderImpl {
struct KeyedContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
typealias Key = K
let impl: JSONDecoderImpl
let codingPath: [CodingKey]
let dictionary: [String: JSONValue]
init(impl: JSONDecoderImpl, codingPath: [CodingKey], dictionary: [String: JSONValue]) {
self.impl = impl
self.codingPath = codingPath
switch impl.options.keyDecodingStrategy {
case .useDefaultKeys:
self.dictionary = dictionary
case .convertFromSnakeCase:
// Convert the snake case keys in the container to camel case.
// If we hit a duplicate key after conversion, then we'll use the first one we saw.
// Effectively an undefined behavior with JSON dictionaries.
var converted = [String: JSONValue]()
converted.reserveCapacity(dictionary.count)
dictionary.forEach { (key, value) in
converted[JSONDecoder.KeyDecodingStrategy._convertFromSnakeCase(key)] = value
}
self.dictionary = converted
case .custom(let converter):
var converted = [String: JSONValue]()
converted.reserveCapacity(dictionary.count)
dictionary.forEach { (key, value) in
var pathForKey = codingPath
pathForKey.append(_JSONKey(stringValue: key)!)
converted[converter(pathForKey).stringValue] = value
}
self.dictionary = converted
}
}
var allKeys: [K] {
self.dictionary.keys.compactMap { K(stringValue: $0) }
}
func contains(_ key: K) -> Bool {
if let _ = dictionary[key.stringValue] {
return true
}
return false
}
func decodeNil(forKey key: K) throws -> Bool {
let value = try getValue(forKey: key)
return value == .null
}
func decode(_ type: Bool.Type, forKey key: K) throws -> Bool {
let value = try getValue(forKey: key)
guard case .bool(let bool) = value else {
throw createTypeMismatchError(type: type, forKey: key, value: value)
}
return bool
}
func decode(_ type: String.Type, forKey key: K) throws -> String {
let value = try getValue(forKey: key)
guard case .string(let string) = value else {
throw createTypeMismatchError(type: type, forKey: key, value: value)
}
return string
}
func decode(_: Double.Type, forKey key: K) throws -> Double {
try decodeFloatingPoint(key: key)
}
func decode(_: Float.Type, forKey key: K) throws -> Float {
try decodeFloatingPoint(key: key)
}
func decode(_: Int.Type, forKey key: K) throws -> Int {
try decodeFixedWidthInteger(key: key)
}
func decode(_: Int8.Type, forKey key: K) throws -> Int8 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: Int16.Type, forKey key: K) throws -> Int16 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: Int32.Type, forKey key: K) throws -> Int32 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: Int64.Type, forKey key: K) throws -> Int64 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: UInt.Type, forKey key: K) throws -> UInt {
try decodeFixedWidthInteger(key: key)
}
func decode(_: UInt8.Type, forKey key: K) throws -> UInt8 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: UInt16.Type, forKey key: K) throws -> UInt16 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: UInt32.Type, forKey key: K) throws -> UInt32 {
try decodeFixedWidthInteger(key: key)
}
func decode(_: UInt64.Type, forKey key: K) throws -> UInt64 {
try decodeFixedWidthInteger(key: key)
}
func decode<T>(_: T.Type, forKey key: K) throws -> T where T: Decodable {
let newDecoder = try decoderForKey(key)
return try newDecoder.unwrap(as: T.self)
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws
-> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey
{
try decoderForKey(key).container(keyedBy: type)
}
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
try decoderForKey(key).unkeyedContainer()
}
func superDecoder() throws -> Decoder {
return decoderForKeyNoThrow(_JSONKey.super)
}
func superDecoder(forKey key: K) throws -> Decoder {
return decoderForKeyNoThrow(key)
}
private func decoderForKey<LocalKey: CodingKey>(_ key: LocalKey) throws -> JSONDecoderImpl {
let value = try getValue(forKey: key)
var newPath = self.codingPath
newPath.append(key)
return JSONDecoderImpl(
userInfo: self.impl.userInfo,
from: value,
codingPath: newPath,
options: self.impl.options
)
}
private func decoderForKeyNoThrow<LocalKey: CodingKey>(_ key: LocalKey) -> JSONDecoderImpl {
let value: JSONValue
do {
value = try getValue(forKey: key)
} catch {
// if there no value for this key then return a null value
value = .null
}
var newPath = self.codingPath
newPath.append(key)
return JSONDecoderImpl(
userInfo: self.impl.userInfo,
from: value,
codingPath: newPath,
options: self.impl.options
)
}
@inline(__always) private func getValue<LocalKey: CodingKey>(forKey key: LocalKey) throws -> JSONValue {
guard let value = dictionary[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(
codingPath: self.codingPath,
debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."
))
}
return value
}
@inline(__always) private func createTypeMismatchError(type: Any.Type, forKey key: K, value: JSONValue) -> DecodingError {
let codingPath = self.codingPath + [key]
return DecodingError.typeMismatch(type, .init(
codingPath: codingPath, debugDescription: "Expected to decode \(type) but found \(value.debugDataTypeDescription) instead."
))
}
@inline(__always) private func decodeFixedWidthInteger<T: FixedWidthInteger>(key: Self.Key) throws -> T {
let value = try getValue(forKey: key)
return try self.impl.unwrapFixedWidthInteger(from: value, for: key, as: T.self)
}
@inline(__always) private func decodeFloatingPoint<T: LosslessStringConvertible & BinaryFloatingPoint>(key: K) throws -> T {
let value = try getValue(forKey: key)
return try self.impl.unwrapFloatingPoint(from: value, for: key, as: T.self)
}
}
}
extension JSONDecoderImpl {
struct UnkeyedContainer: UnkeyedDecodingContainer {
let impl: JSONDecoderImpl
let codingPath: [CodingKey]
let array: [JSONValue]
var count: Int? { self.array.count }
var isAtEnd: Bool { self.currentIndex >= (self.count ?? 0) }
var currentIndex = 0
init(impl: JSONDecoderImpl, codingPath: [CodingKey], array: [JSONValue]) {
self.impl = impl
self.codingPath = codingPath
self.array = array
}
mutating func decodeNil() throws -> Bool {
if try self.getNextValue(ofType: Never.self) == .null {
self.currentIndex += 1
return true
}
// The protocol states:
// If the value is not null, does not increment currentIndex.
return false
}
mutating func decode(_ type: Bool.Type) throws -> Bool {
let value = try self.getNextValue(ofType: Bool.self)
guard case .bool(let bool) = value else {
throw impl.createTypeMismatchError(type: type, for: _JSONKey(index: currentIndex), value: value)
}
self.currentIndex += 1
return bool
}
mutating func decode(_ type: String.Type) throws -> String {
let value = try self.getNextValue(ofType: String.self)
guard case .string(let string) = value else {
throw impl.createTypeMismatchError(type: type, for: _JSONKey(index: currentIndex), value: value)
}
self.currentIndex += 1
return string
}
mutating func decode(_: Double.Type) throws -> Double {
try decodeFloatingPoint()
}
mutating func decode(_: Float.Type) throws -> Float {
try decodeFloatingPoint()
}
mutating func decode(_: Int.Type) throws -> Int {
try decodeFixedWidthInteger()
}
mutating func decode(_: Int8.Type) throws -> Int8 {
try decodeFixedWidthInteger()
}
mutating func decode(_: Int16.Type) throws -> Int16 {
try decodeFixedWidthInteger()
}
mutating func decode(_: Int32.Type) throws -> Int32 {
try decodeFixedWidthInteger()
}
mutating func decode(_: Int64.Type) throws -> Int64 {
try decodeFixedWidthInteger()
}
mutating func decode(_: UInt.Type) throws -> UInt {
try decodeFixedWidthInteger()
}
mutating func decode(_: UInt8.Type) throws -> UInt8 {
try decodeFixedWidthInteger()
}
mutating func decode(_: UInt16.Type) throws -> UInt16 {
try decodeFixedWidthInteger()
}
mutating func decode(_: UInt32.Type) throws -> UInt32 {
try decodeFixedWidthInteger()
}
mutating func decode(_: UInt64.Type) throws -> UInt64 {
try decodeFixedWidthInteger()
}
mutating func decode<T>(_: T.Type) throws -> T where T: Decodable {
let newDecoder = try decoderForNextElement(ofType: T.self)
let result = try newDecoder.unwrap(as: T.self)
// Because of the requirement that the index not be incremented unless
// decoding the desired result type succeeds, it can not be a tail call.
// Hopefully the compiler still optimizes well enough that the result
// doesn't get copied around.
self.currentIndex += 1
return result
}
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws
-> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey
{
let decoder = try decoderForNextElement(ofType: KeyedDecodingContainer<NestedKey>.self)
let container = try decoder.container(keyedBy: type)
self.currentIndex += 1
return container
}
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
let decoder = try decoderForNextElement(ofType: UnkeyedDecodingContainer.self)
let container = try decoder.unkeyedContainer()
self.currentIndex += 1
return container
}
mutating func superDecoder() throws -> Decoder {
let decoder = try decoderForNextElement(ofType: Decoder.self)
self.currentIndex += 1
return decoder
}
private mutating func decoderForNextElement<T>(ofType: T.Type) throws -> JSONDecoderImpl {
let value = try self.getNextValue(ofType: T.self)
let newPath = self.codingPath + [_JSONKey(index: self.currentIndex)]
return JSONDecoderImpl(
userInfo: self.impl.userInfo,
from: value,
codingPath: newPath,
options: self.impl.options
)
}
@inline(__always)
private func getNextValue<T>(ofType: T.Type) throws -> JSONValue {
guard !self.isAtEnd else {
var message = "Unkeyed container is at end."
if T.self == UnkeyedContainer.self {
message = "Cannot get nested unkeyed container -- unkeyed container is at end."
}
if T.self == Decoder.self {
message = "Cannot get superDecoder() -- unkeyed container is at end."
}
var path = self.codingPath
path.append(_JSONKey(index: self.currentIndex))
throw DecodingError.valueNotFound(
T.self,
.init(codingPath: path,
debugDescription: message,
underlyingError: nil))
}
return self.array[self.currentIndex]
}
@inline(__always) private mutating func decodeFixedWidthInteger<T: FixedWidthInteger>() throws -> T {