@@ -109,7 +109,7 @@ private struct JSONMapBuilder {
109
109
110
110
init ( ) {
111
111
mapData = [ ]
112
- mapData. reserveCapacity ( 128 ) // 128 is good enough for most PluginMessage.
112
+ mapData. reserveCapacity ( 128 ) // 128 is good enough for most PluginMessage.
113
113
}
114
114
115
115
@inline ( __always)
@@ -550,7 +550,7 @@ extension JSONMapValue {
550
550
}
551
551
552
552
@inline ( __always)
553
- func asString( ) -> String ? {
553
+ func asString( ) -> String ? {
554
554
if self . is ( . simpleString) {
555
555
return _JSONStringParser. decodeSimpleString ( source: valueBuffer ( ) )
556
556
}
@@ -681,21 +681,41 @@ private struct JSONDecoding {
681
681
// MARK: Pure decoding functions.
682
682
extension JSONDecoding {
683
683
@inline ( __always)
684
- static func _unwrapOrThrow< T> (
685
- _ v: T ? ,
686
- codingPathNode: _CodingPathNode ,
684
+ static func _checkNotNull< T> (
685
+ _ value: JSONMapValue ,
686
+ expectedType: T . Type ,
687
+ for codingPathNode: _CodingPathNode ,
687
688
_ additionalKey: ( some CodingKey ) ?
688
- ) throws -> T {
689
- guard let v = v else {
690
- throw DecodingError . typeMismatch (
691
- T . self ,
692
- . init (
689
+ ) throws {
690
+ guard !value . isNull else {
691
+ throw DecodingError . valueNotFound (
692
+ expectedType ,
693
+ DecodingError . Context (
693
694
codingPath: codingPathNode. path ( byAppending: additionalKey) ,
694
- debugDescription: " type mismatch "
695
+ debugDescription: " Cannot get value of type \( expectedType ) -- found null value instead "
695
696
)
696
697
)
697
698
}
698
- return v
699
+ }
700
+
701
+ @inline ( __always)
702
+ static func _unwrapOrThrow< T> (
703
+ _ result: T ? ,
704
+ decoding value: JSONMapValue ,
705
+ codingPathNode: _CodingPathNode ,
706
+ _ additionalKey: ( some CodingKey ) ?
707
+ ) throws -> T {
708
+ if let result = result {
709
+ return result
710
+ }
711
+ try _checkNotNull ( value, expectedType: T . self, for: codingPathNode, additionalKey)
712
+ throw DecodingError . typeMismatch (
713
+ T . self,
714
+ . init(
715
+ codingPath: codingPathNode. path ( byAppending: additionalKey) ,
716
+ debugDescription: " type mismatch "
717
+ )
718
+ )
699
719
}
700
720
@inline ( __always)
701
721
static func _decode(
@@ -704,7 +724,7 @@ extension JSONDecoding {
704
724
codingPathNode: _CodingPathNode ,
705
725
_ additionalKey: ( some CodingKey ) ?
706
726
) throws -> Bool {
707
- try _unwrapOrThrow ( value. asBool ( ) , codingPathNode: codingPathNode, additionalKey)
727
+ try _unwrapOrThrow ( value. asBool ( ) , decoding : value , codingPathNode: codingPathNode, additionalKey)
708
728
}
709
729
@inline ( __always)
710
730
static func _decode(
@@ -713,7 +733,7 @@ extension JSONDecoding {
713
733
codingPathNode: _CodingPathNode ,
714
734
_ additionalKey: ( some CodingKey ) ?
715
735
) throws -> String {
716
- try _unwrapOrThrow ( value. asString ( ) , codingPathNode: codingPathNode, additionalKey)
736
+ try _unwrapOrThrow ( value. asString ( ) , decoding : value , codingPathNode: codingPathNode, additionalKey)
717
737
}
718
738
@inline ( __always)
719
739
static func _decode< Integer: FixedWidthInteger > (
@@ -722,7 +742,7 @@ extension JSONDecoding {
722
742
codingPathNode: _CodingPathNode ,
723
743
_ additionalKey: ( some CodingKey ) ?
724
744
) throws -> Integer {
725
- try _unwrapOrThrow ( value. asInteger ( type) , codingPathNode: codingPathNode, additionalKey)
745
+ try _unwrapOrThrow ( value. asInteger ( type) , decoding : value , codingPathNode: codingPathNode, additionalKey)
726
746
}
727
747
@inline ( __always)
728
748
static func _decode< Floating: BinaryFloatingPoint > (
@@ -731,7 +751,7 @@ extension JSONDecoding {
731
751
codingPathNode: _CodingPathNode ,
732
752
_ additionalKey: ( some CodingKey ) ?
733
753
) throws -> Floating {
734
- try _unwrapOrThrow ( value. asFloatingPoint ( type) , codingPathNode: codingPathNode, additionalKey)
754
+ try _unwrapOrThrow ( value. asFloatingPoint ( type) , decoding : value , codingPathNode: codingPathNode, additionalKey)
735
755
}
736
756
737
757
static func _decodeGeneric< T: Decodable > (
@@ -740,6 +760,7 @@ extension JSONDecoding {
740
760
codingPathNode: _CodingPathNode ,
741
761
_ additionalKey: ( some CodingKey ) ?
742
762
) throws -> T {
763
+ try _checkNotNull ( value, expectedType: type, for: codingPathNode, additionalKey)
743
764
let decoder = Self ( value: value, codingPathNode: codingPathNode. appending ( additionalKey) )
744
765
return try T . init ( from: decoder)
745
766
}
0 commit comments