@@ -9,6 +9,82 @@ import BigInt
9
9
import Foundation
10
10
11
11
extension KeyedDecodingContainer {
12
+ /// Decodes a value of the given type for the given key.
13
+ ///
14
+ /// - parameter type: The type of value to decode.
15
+ /// - parameter key: The key that the decoded value is associated with.
16
+ /// - returns: A value of the requested type, if present for the given key
17
+ /// and convertible to the requested type.
18
+ /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
19
+ /// is not convertible to the requested type.
20
+ /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry
21
+ /// for the given key.
22
+ /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
23
+ /// the given key.
24
+ @available ( * , deprecated, message: " Use decodeHex insetad " )
25
+ public func decode( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] {
26
+ var values = try nestedUnkeyedContainer ( forKey: key)
27
+ return try values. decode ( type)
28
+ }
29
+
30
+ /// Decodes a value of the given type for the given key.
31
+ ///
32
+ /// - parameter type: The type of value to decode.
33
+ /// - parameter key: The key that the decoded value is associated with.
34
+ /// - returns: A value of the requested type, if present for the given key
35
+ /// and convertible to the requested type.
36
+ /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
37
+ /// is not convertible to the requested type.
38
+ /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry
39
+ /// for the given key.
40
+ /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for
41
+ /// the given key.
42
+ @available ( * , deprecated, message: " Use decodeHex() insetad " )
43
+ public func decode( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] {
44
+ let values = try nestedContainer ( keyedBy: AnyCodingKey . self, forKey: key)
45
+ return try values. decode ( type)
46
+ }
47
+
48
+ /// Decodes a value of the given type for the given key, if present.
49
+ ///
50
+ /// This method returns `nil` if the container does not have a value
51
+ /// associated with `key`, or if the value is null. The difference between
52
+ /// these states can be distinguished with a `contains(_:)` call.
53
+ ///
54
+ /// - parameter type: The type of value to decode.
55
+ /// - parameter key: The key that the decoded value is associated with.
56
+ /// - returns: A decoded value of the requested type, or `nil` if the
57
+ /// `Decoder` does not have an entry associated with the given key, or if
58
+ /// the value is a null value.
59
+ /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
60
+ /// is not convertible to the requested type.
61
+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
62
+ public func decodeIfPresent( _ type: [ Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ Any ] ? {
63
+ guard contains ( key) ,
64
+ try decodeNil ( forKey: key) == false else { return nil }
65
+ return try decode ( type, forKey: key)
66
+ }
67
+
68
+ /// Decodes a value of the given type for the given key, if present.
69
+ ///
70
+ /// This method returns `nil` if the container does not have a value
71
+ /// associated with `key`, or if the value is null. The difference between
72
+ /// these states can be distinguished with a `contains(_:)` call.
73
+ ///
74
+ /// - parameter type: The type of value to decode.
75
+ /// - parameter key: The key that the decoded value is associated with.
76
+ /// - returns: A decoded value of the requested type, or `nil` if the
77
+ /// `Decoder` does not have an entry associated with the given key, or if
78
+ /// the value is a null value.
79
+ /// - throws: `DecodingError.typeMismatch` if the encountered encoded value
80
+ /// is not convertible to the requested type.
81
+ @available ( * , deprecated, message: " In next version Will be replaced by decodeHexIfPresent() insetad " )
82
+ public func decodeIfPresent( _ type: [ String : Any ] . Type, forKey key: KeyedDecodingContainer < K > . Key ) throws -> [ String : Any ] ? {
83
+ guard contains ( key) ,
84
+ try decodeNil ( forKey: key) == false else { return nil }
85
+ return try decode ( type, forKey: key)
86
+ }
87
+
12
88
/// Decodes a value of the given key from Hex to `DecodableFromHex`
13
89
///
14
90
/// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`
@@ -123,3 +199,55 @@ extension EthereumAddress: DecodableFromHex {
123
199
self . init ( hexString, ignoreChecksum: true )
124
200
}
125
201
}
202
+
203
+ // deprecated, should be removed in 3.0.0
204
+ private extension KeyedDecodingContainer {
205
+ func decode( _ type: [ String : Any ] . Type) throws -> [ String : Any ] {
206
+ var dictionary : [ String : Any ] = [ : ]
207
+ for key in allKeys {
208
+ if try decodeNil ( forKey: key) {
209
+ dictionary [ key. stringValue] = NSNull ( )
210
+ } else if let bool = try ? decode ( Bool . self, forKey: key) {
211
+ dictionary [ key. stringValue] = bool
212
+ } else if let string = try ? decode ( String . self, forKey: key) {
213
+ dictionary [ key. stringValue] = string
214
+ } else if let int = try ? decode ( Int . self, forKey: key) {
215
+ dictionary [ key. stringValue] = int
216
+ } else if let double = try ? decode ( Double . self, forKey: key) {
217
+ dictionary [ key. stringValue] = double
218
+ } else if let dict = try ? decode ( [ String : Any ] . self, forKey: key) {
219
+ dictionary [ key. stringValue] = dict
220
+ } else if let array = try ? decode ( [ Any ] . self, forKey: key) {
221
+ dictionary [ key. stringValue] = array
222
+ }
223
+ }
224
+ return dictionary
225
+ }
226
+ }
227
+
228
+ // deprecated, should be removed in 3.0.0
229
+ private extension UnkeyedDecodingContainer {
230
+ mutating func decode( _ type: [ Any ] . Type) throws -> [ Any ] {
231
+ var elements : [ Any ] = [ ]
232
+ while !isAtEnd {
233
+ if try decodeNil ( ) {
234
+ elements. append ( NSNull ( ) )
235
+ } else if let int = try ? decode ( Int . self) {
236
+ elements. append ( int)
237
+ } else if let bool = try ? decode ( Bool . self) {
238
+ elements. append ( bool)
239
+ } else if let double = try ? decode ( Double . self) {
240
+ elements. append ( double)
241
+ } else if let string = try ? decode ( String . self) {
242
+ elements. append ( string)
243
+ } else if let values = try ? nestedContainer ( keyedBy: AnyCodingKey . self) ,
244
+ let element = try ? values. decode ( [ String : Any ] . self) {
245
+ elements. append ( element)
246
+ } else if var values = try ? nestedUnkeyedContainer ( ) ,
247
+ let element = try ? values. decode ( [ Any ] . self) {
248
+ elements. append ( element)
249
+ }
250
+ }
251
+ return elements
252
+ }
253
+ }
0 commit comments