@@ -21,14 +21,14 @@ func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T
21
21
22
22
private struct JSONMap {
23
23
enum Descriptor : Int {
24
- case nullKeyword // [desc]
25
- case trueKeyword // [desc]
24
+ case nullKeyword // [desc]
25
+ case trueKeyword // [desc]
26
26
case falseKeyword // [desc]
27
- case number // [desc, pointer, length]
27
+ case number // [desc, pointer, length]
28
28
case simpleString // [desc, pointer, length]
29
- case string // [desc, pointer, length]
30
- case object // [desc, count, (key, value)...]
31
- case array // [desc, count, element...]
29
+ case string // [desc, pointer, length]
30
+ case object // [desc, count, (key, value)...]
31
+ case array // [desc, count, element...]
32
32
}
33
33
let data : [ Int ]
34
34
@@ -78,7 +78,7 @@ private struct JSONMapValue {
78
78
79
79
@inline ( __always)
80
80
func value( at i: Index ) -> JSONMapValue {
81
- return . init( map: map [ i ..< index ( afterValue: i) ] )
81
+ return . init( map: map [ i..< index ( afterValue: i) ] )
82
82
}
83
83
}
84
84
@@ -137,11 +137,11 @@ private enum _JSONStringDecoder {
137
137
case UInt8 ( ascii: " ' " ) : string. append ( " ' " )
138
138
case UInt8 ( ascii: " \\ " ) : string. append ( " \\ " )
139
139
case UInt8 ( ascii: " / " ) : string. append ( " / " )
140
- case UInt8 ( ascii: " b " ) : string. append ( " \u{08} " ) // \b
141
- case UInt8 ( ascii: " f " ) : string. append ( " \u{0C} " ) // \f
142
- case UInt8 ( ascii: " n " ) : string. append ( " \u{0A} " ) // \n
143
- case UInt8 ( ascii: " r " ) : string. append ( " \u{0D} " ) // \r
144
- case UInt8 ( ascii: " t " ) : string. append ( " \u{09} " ) // \t
140
+ case UInt8 ( ascii: " b " ) : string. append ( " \u{08} " ) // \b
141
+ case UInt8 ( ascii: " f " ) : string. append ( " \u{0C} " ) // \f
142
+ case UInt8 ( ascii: " n " ) : string. append ( " \u{0A} " ) // \n
143
+ case UInt8 ( ascii: " r " ) : string. append ( " \u{0D} " ) // \r
144
+ case UInt8 ( ascii: " t " ) : string. append ( " \u{09} " ) // \t
145
145
case UInt8 ( ascii: " u " ) :
146
146
fatalError ( " unimplemented " )
147
147
default :
@@ -178,9 +178,10 @@ private enum _JSONNumberDecoder {
178
178
guard !overflowed else {
179
179
return nil
180
180
}
181
- ( value, overflowed) = isNegative
182
- ? value. subtractingReportingOverflow ( digitValue)
183
- : value. addingReportingOverflow ( digitValue)
181
+ ( value, overflowed) =
182
+ isNegative
183
+ ? value. subtractingReportingOverflow ( digitValue)
184
+ : value. addingReportingOverflow ( digitValue)
184
185
guard !overflowed else {
185
186
return nil
186
187
}
@@ -312,14 +313,14 @@ private struct JSONMapBuilder {
312
313
mutating func startCollection( _ descriptor: JSONMap . Descriptor ) -> Int {
313
314
let handle = mapData. count
314
315
mapData. append ( descriptor. rawValue)
315
- mapData. append ( 0 ) // Count, this will be updated in closeCollection()
316
+ mapData. append ( 0 ) // Count, this will be updated in closeCollection()
316
317
return handle
317
318
}
318
319
319
320
mutating func closeCollection( handle: Int ) {
320
321
// 'handle': descriptor index.
321
322
// 'handle+1': counter index.
322
- mapData [ handle+ 1 ] = mapData. count - handle - 2
323
+ mapData [ handle + 1 ] = mapData. count - handle - 2
323
324
}
324
325
325
326
func finalize( ) -> JSONMap {
@@ -430,21 +431,21 @@ private struct JSONScanner {
430
431
_ = try advance ( )
431
432
}
432
433
try expect ( " \" " )
433
- map. record ( hasEscape ? . string : . simpleString, range: start ..< ptr)
434
+ map. record ( hasEscape ? . string : . simpleString, range: start..< ptr)
434
435
}
435
436
436
437
mutating func scanNumber( start: Cursor ) throws {
437
438
ptr = start
438
439
_ = advance ( if: " - " )
439
- while advance ( if: " 0 " ... " 9 " ) { }
440
+ while advance ( if: " 0 " ... " 9 " ) { }
440
441
if advance ( if: " . " ) {
441
- while advance ( if: " 0 " ... " 9 " ) { }
442
+ while advance ( if: " 0 " ... " 9 " ) { }
442
443
}
443
444
if advance ( if: " e " ) || advance ( if: " E " ) {
444
445
_ = advance ( if: " - " ) || advance ( if: " + " )
445
- while advance ( if: " 0 " ... " 9 " ) { }
446
+ while advance ( if: " 0 " ... " 9 " ) { }
446
447
}
447
- map. record ( . number, range: start ..< ptr)
448
+ map. record ( . number, range: start..< ptr)
448
449
}
449
450
450
451
mutating func scanObject( ) throws {
@@ -496,7 +497,7 @@ private struct JSONScanner {
496
497
try scanFalse ( )
497
498
case UInt8 ( ascii: " \" " ) :
498
499
try scanString ( start: start)
499
- case UInt8 ( ascii: " - " ) , UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " ) :
500
+ case UInt8 ( ascii: " - " ) , UInt8 ( ascii: " 0 " ) ... UInt8 ( ascii: " 9 " ) :
500
501
try scanNumber ( start: start)
501
502
case UInt8 ( ascii: " { " ) :
502
503
try scanObject ( )
@@ -526,33 +527,65 @@ private struct JSONDecoding {
526
527
527
528
extension JSONDecoding {
528
529
@inline ( __always)
529
- static func _unwrapOrThrow< T> ( _ v: T ? , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> T {
530
+ static func _unwrapOrThrow< T> (
531
+ _ v: T ? ,
532
+ codingPathNode: _CodingPathNode ,
533
+ _ additionalKey: ( some CodingKey ) ?
534
+ ) throws -> T {
530
535
guard let v = v else {
531
- throw DecodingError . typeMismatch ( T . self, . init(
532
- codingPath: codingPathNode. path ( byAppending: additionalKey) ,
533
- debugDescription: " type mismatch "
534
- ) )
536
+ throw DecodingError . typeMismatch (
537
+ T . self,
538
+ . init(
539
+ codingPath: codingPathNode. path ( byAppending: additionalKey) ,
540
+ debugDescription: " type mismatch "
541
+ )
542
+ )
535
543
}
536
544
return v
537
545
}
538
546
@inline ( __always)
539
- static func _decode( _ value: JSONMapValue , as _: Bool . Type , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> Bool {
547
+ static func _decode(
548
+ _ value: JSONMapValue ,
549
+ as _: Bool . Type ,
550
+ codingPathNode: _CodingPathNode ,
551
+ _ additionalKey: ( some CodingKey ) ?
552
+ ) throws -> Bool {
540
553
try _unwrapOrThrow ( value. asBool, codingPathNode: codingPathNode, additionalKey)
541
554
}
542
555
@inline ( __always)
543
- static func _decode( _ value: JSONMapValue , as _: String . Type , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> String {
556
+ static func _decode(
557
+ _ value: JSONMapValue ,
558
+ as _: String . Type ,
559
+ codingPathNode: _CodingPathNode ,
560
+ _ additionalKey: ( some CodingKey ) ?
561
+ ) throws -> String {
544
562
try _unwrapOrThrow ( value. asString, codingPathNode: codingPathNode, additionalKey)
545
563
}
546
564
@inline ( __always)
547
- static func _decode< Integer: FixedWidthInteger > ( _ value: JSONMapValue , as type: Integer . Type , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> Integer {
565
+ static func _decode< Integer: FixedWidthInteger > (
566
+ _ value: JSONMapValue ,
567
+ as type: Integer . Type ,
568
+ codingPathNode: _CodingPathNode ,
569
+ _ additionalKey: ( some CodingKey ) ?
570
+ ) throws -> Integer {
548
571
try _unwrapOrThrow ( value. asInteger ( type) , codingPathNode: codingPathNode, additionalKey)
549
572
}
550
573
@inline ( __always)
551
- static func _decode< Floating: BinaryFloatingPoint > ( _ value: JSONMapValue , as type: Floating . Type , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> Floating {
574
+ static func _decode< Floating: BinaryFloatingPoint > (
575
+ _ value: JSONMapValue ,
576
+ as type: Floating . Type ,
577
+ codingPathNode: _CodingPathNode ,
578
+ _ additionalKey: ( some CodingKey ) ?
579
+ ) throws -> Floating {
552
580
try _unwrapOrThrow ( value. asFloatingPoint ( type) , codingPathNode: codingPathNode, additionalKey)
553
581
}
554
582
555
- static func _decodeGeneric< T: Decodable > ( _ value: JSONMapValue , as type: T . Type , codingPathNode: _CodingPathNode , _ additionalKey: ( some CodingKey ) ? ) throws -> T {
583
+ static func _decodeGeneric< T: Decodable > (
584
+ _ value: JSONMapValue ,
585
+ as type: T . Type ,
586
+ codingPathNode: _CodingPathNode ,
587
+ _ additionalKey: ( some CodingKey ) ?
588
+ ) throws -> T {
556
589
let decoder = Self ( value: value, codingPathNode: codingPathNode. appending ( additionalKey) )
557
590
return try T . init ( from: decoder)
558
591
}
@@ -562,7 +595,7 @@ extension JSONDecoding: Decoder {
562
595
var codingPath : [ any CodingKey ] {
563
596
codingPathNode. path
564
597
}
565
- var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
598
+ var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
566
599
567
600
fileprivate struct KeyedContainer < Key: CodingKey > {
568
601
var codingPathNode : _CodingPathNode
@@ -577,10 +610,12 @@ extension JSONDecoding: Decoder {
577
610
}
578
611
579
612
func container< Key: CodingKey > ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > {
580
- return try KeyedDecodingContainer ( KeyedContainer < Key > (
581
- value: value,
582
- codingPathNode: codingPathNode
583
- ) )
613
+ return try KeyedDecodingContainer (
614
+ KeyedContainer < Key > (
615
+ value: value,
616
+ codingPathNode: codingPathNode
617
+ )
618
+ )
584
619
}
585
620
586
621
func unkeyedContainer( ) throws -> any UnkeyedDecodingContainer {
@@ -656,7 +691,7 @@ extension JSONDecoding: SingleValueDecodingContainer {
656
691
try JSONDecoding . _decode ( value, as: type, codingPathNode: codingPathNode, _CodingKey? . none)
657
692
}
658
693
659
- func decode< T> ( _ type: T . Type ) throws -> T where T : Decodable {
694
+ func decode< T> ( _ type: T . Type ) throws -> T where T: Decodable {
660
695
try JSONDecoding . _decodeGeneric ( value, as: type, codingPathNode: codingPathNode, _CodingKey? . none)
661
696
}
662
697
}
@@ -677,9 +712,13 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
677
712
@inline ( __always)
678
713
func _getOrThrow( forKey key: Key ) throws -> JSONMapValue {
679
714
guard let value = mapping [ key. stringValue] else {
680
- throw DecodingError . keyNotFound ( key, . init(
681
- codingPath: codingPathNode. path,
682
- debugDescription: " No value associated with key \( key) ( \" \( key. stringValue) \" ). " ) )
715
+ throw DecodingError . keyNotFound (
716
+ key,
717
+ . init(
718
+ codingPath: codingPathNode. path,
719
+ debugDescription: " No value associated with key \( key) ( \" \( key. stringValue) \" ). "
720
+ )
721
+ )
683
722
}
684
723
return value
685
724
}
@@ -748,11 +787,16 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
748
787
try JSONDecoding . _decodeGeneric ( _getOrThrow ( forKey: key) , as: type, codingPathNode: codingPathNode, key)
749
788
}
750
789
751
- func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type , forKey key: Key ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey : CodingKey {
752
- return try KeyedDecodingContainer ( JSONDecoding . KeyedContainer< NestedKey> (
753
- value: try _getOrThrow ( forKey: key) ,
754
- codingPathNode: codingPathNode. appending ( key)
755
- ) )
790
+ func nestedContainer< NestedKey: CodingKey > (
791
+ keyedBy keyType: NestedKey . Type ,
792
+ forKey key: Key
793
+ ) throws -> KeyedDecodingContainer < NestedKey > {
794
+ return try KeyedDecodingContainer (
795
+ JSONDecoding . KeyedContainer< NestedKey> (
796
+ value: try _getOrThrow ( forKey: key) ,
797
+ codingPathNode: codingPathNode. appending ( key)
798
+ )
799
+ )
756
800
}
757
801
758
802
func nestedUnkeyedContainer( forKey key: Key ) throws -> any UnkeyedDecodingContainer {
@@ -772,20 +816,26 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
772
816
773
817
init ( value: JSONMapValue , codingPathNode: _CodingPathNode ) throws {
774
818
guard value. isObject else {
775
- throw DecodingError . typeMismatch ( [ String : Any ] . self, . init(
776
- codingPath: codingPathNode. path,
777
- debugDescription: " not an array "
778
- ) )
819
+ throw DecodingError . typeMismatch (
820
+ [ String : Any ] . self,
821
+ . init(
822
+ codingPath: codingPathNode. path,
823
+ debugDescription: " not an array "
824
+ )
825
+ )
779
826
}
780
827
self . codingPathNode = codingPathNode
781
828
self . mapping = [ : ]
782
829
var iter = value. makeObjectIterator ( ) !
783
830
while let elem = iter. next ( ) {
784
831
guard let keyStr = elem. key. asString else {
785
- throw DecodingError . typeMismatch ( String . self, . init(
786
- codingPath: codingPathNode. path,
787
- debugDescription: " expected a string as a key "
788
- ) )
832
+ throw DecodingError . typeMismatch (
833
+ String . self,
834
+ . init(
835
+ codingPath: codingPathNode. path,
836
+ debugDescription: " expected a string as a key "
837
+ )
838
+ )
789
839
}
790
840
self . mapping [ keyStr] = elem. value
791
841
}
@@ -815,10 +865,13 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
815
865
mutating func _getOrThrow( ) throws -> ( index: any CodingKey , value: JSONMapValue ) {
816
866
let idx = currentIndex
817
867
guard !isAtEnd else {
818
- throw DecodingError . valueNotFound ( Any . self, . init(
819
- codingPath: codingPathNode. path ( byAppendingIndex: idx) ,
820
- debugDescription: " Unkeyed container is at end "
821
- ) )
868
+ throw DecodingError . valueNotFound (
869
+ Any . self,
870
+ . init(
871
+ codingPath: codingPathNode. path ( byAppendingIndex: idx) ,
872
+ debugDescription: " Unkeyed container is at end "
873
+ )
874
+ )
822
875
}
823
876
let value = array [ _currMapIdx]
824
877
advanceToNextValue ( )
@@ -904,17 +957,21 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
904
957
try _decodeInteger ( type)
905
958
}
906
959
907
- mutating func decode< T> ( _ type: T . Type ) throws -> T where T : Decodable {
960
+ mutating func decode< T> ( _ type: T . Type ) throws -> T where T: Decodable {
908
961
let ( idx, value) = try _getOrThrow ( )
909
962
return try JSONDecoding . _decodeGeneric ( value, as: type, codingPathNode: codingPathNode, idx)
910
963
}
911
964
912
- mutating func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type ) throws -> KeyedDecodingContainer < NestedKey > where NestedKey : CodingKey {
965
+ mutating func nestedContainer< NestedKey: CodingKey > (
966
+ keyedBy keyType: NestedKey . Type
967
+ ) throws -> KeyedDecodingContainer < NestedKey > {
913
968
let ( idx, value) = try _getOrThrow ( )
914
- return try KeyedDecodingContainer ( JSONDecoding . KeyedContainer< NestedKey> (
915
- value: value,
916
- codingPathNode: codingPathNode. appending ( idx)
917
- ) )
969
+ return try KeyedDecodingContainer (
970
+ JSONDecoding . KeyedContainer< NestedKey> (
971
+ value: value,
972
+ codingPathNode: codingPathNode. appending ( idx)
973
+ )
974
+ )
918
975
}
919
976
920
977
mutating func nestedUnkeyedContainer( ) throws -> any UnkeyedDecodingContainer {
@@ -931,10 +988,13 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
931
988
932
989
init ( value: JSONMapValue , codingPathNode: _CodingPathNode ) throws {
933
990
guard value. isArray else {
934
- throw DecodingError . typeMismatch ( [ Any ] . self, . init(
935
- codingPath: codingPathNode. path,
936
- debugDescription: " not an array "
937
- ) )
991
+ throw DecodingError . typeMismatch (
992
+ [ Any ] . self,
993
+ . init(
994
+ codingPath: codingPathNode. path,
995
+ debugDescription: " not an array "
996
+ )
997
+ )
938
998
}
939
999
self . codingPathNode = codingPathNode
940
1000
self . currentIndex = 0
0 commit comments