|
| 1 | +// |
| 2 | +// DecodingContainer+AnyCollection.swift |
| 3 | +// AnyDecodable |
| 4 | +// |
| 5 | +// Created by levantAJ on 1/18/19. |
| 6 | +// Copyright © 2019 levantAJ. All rights reserved. |
| 7 | +// |
| 8 | +import Foundation |
| 9 | + |
| 10 | +struct AnyCodingKey: CodingKey { |
| 11 | + var stringValue: String |
| 12 | + var intValue: Int? |
| 13 | + |
| 14 | + init?(stringValue: String) { |
| 15 | + self.stringValue = stringValue |
| 16 | + } |
| 17 | + |
| 18 | + init?(intValue: Int) { |
| 19 | + self.intValue = intValue |
| 20 | + self.stringValue = String(intValue) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +extension KeyedDecodingContainer { |
| 25 | + /// Decodes a value of the given type for the given key. |
| 26 | + /// |
| 27 | + /// - parameter type: The type of value to decode. |
| 28 | + /// - parameter key: The key that the decoded value is associated with. |
| 29 | + /// - returns: A value of the requested type, if present for the given key |
| 30 | + /// and convertible to the requested type. |
| 31 | + /// - throws: `DecodingError.typeMismatch` if the encountered encoded value |
| 32 | + /// is not convertible to the requested type. |
| 33 | + /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry |
| 34 | + /// for the given key. |
| 35 | + /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for |
| 36 | + /// the given key. |
| 37 | + public func decode(_ type: [Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [Any] { |
| 38 | + var values = try nestedUnkeyedContainer(forKey: key) |
| 39 | + return try values.decode(type) |
| 40 | + } |
| 41 | + |
| 42 | + /// Decodes a value of the given type for the given key. |
| 43 | + /// |
| 44 | + /// - parameter type: The type of value to decode. |
| 45 | + /// - parameter key: The key that the decoded value is associated with. |
| 46 | + /// - returns: A value of the requested type, if present for the given key |
| 47 | + /// and convertible to the requested type. |
| 48 | + /// - throws: `DecodingError.typeMismatch` if the encountered encoded value |
| 49 | + /// is not convertible to the requested type. |
| 50 | + /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry |
| 51 | + /// for the given key. |
| 52 | + /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for |
| 53 | + /// the given key. |
| 54 | + public func decode(_ type: [String: Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [String: Any] { |
| 55 | + let values = try nestedContainer(keyedBy: AnyCodingKey.self, forKey: key) |
| 56 | + return try values.decode(type) |
| 57 | + } |
| 58 | + |
| 59 | + /// Decodes a value of the given type for the given key, if present. |
| 60 | + /// |
| 61 | + /// This method returns `nil` if the container does not have a value |
| 62 | + /// associated with `key`, or if the value is null. The difference between |
| 63 | + /// these states can be distinguished with a `contains(_:)` call. |
| 64 | + /// |
| 65 | + /// - parameter type: The type of value to decode. |
| 66 | + /// - parameter key: The key that the decoded value is associated with. |
| 67 | + /// - returns: A decoded value of the requested type, or `nil` if the |
| 68 | + /// `Decoder` does not have an entry associated with the given key, or if |
| 69 | + /// the value is a null value. |
| 70 | + /// - throws: `DecodingError.typeMismatch` if the encountered encoded value |
| 71 | + /// is not convertible to the requested type. |
| 72 | + public func decodeIfPresent(_ type: [Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [Any]? { |
| 73 | + guard contains(key), |
| 74 | + try decodeNil(forKey: key) == false else { return nil } |
| 75 | + return try decode(type, forKey: key) |
| 76 | + } |
| 77 | + |
| 78 | + /// Decodes a value of the given type for the given key, if present. |
| 79 | + /// |
| 80 | + /// This method returns `nil` if the container does not have a value |
| 81 | + /// associated with `key`, or if the value is null. The difference between |
| 82 | + /// these states can be distinguished with a `contains(_:)` call. |
| 83 | + /// |
| 84 | + /// - parameter type: The type of value to decode. |
| 85 | + /// - parameter key: The key that the decoded value is associated with. |
| 86 | + /// - returns: A decoded value of the requested type, or `nil` if the |
| 87 | + /// `Decoder` does not have an entry associated with the given key, or if |
| 88 | + /// the value is a null value. |
| 89 | + /// - throws: `DecodingError.typeMismatch` if the encountered encoded value |
| 90 | + /// is not convertible to the requested type. |
| 91 | + public func decodeIfPresent(_ type: [String: Any].Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> [String: Any]? { |
| 92 | + guard contains(key), |
| 93 | + try decodeNil(forKey: key) == false else { return nil } |
| 94 | + return try decode(type, forKey: key) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +private extension KeyedDecodingContainer { |
| 99 | + func decode(_ type: [String: Any].Type) throws -> [String: Any] { |
| 100 | + var dictionary: [String: Any] = [:] |
| 101 | + for key in allKeys { |
| 102 | + if try decodeNil(forKey: key) { |
| 103 | + dictionary[key.stringValue] = NSNull() |
| 104 | + } else if let bool = try? decode(Bool.self, forKey: key) { |
| 105 | + dictionary[key.stringValue] = bool |
| 106 | + } else if let string = try? decode(String.self, forKey: key) { |
| 107 | + dictionary[key.stringValue] = string |
| 108 | + } else if let int = try? decode(Int.self, forKey: key) { |
| 109 | + dictionary[key.stringValue] = int |
| 110 | + } else if let double = try? decode(Double.self, forKey: key) { |
| 111 | + dictionary[key.stringValue] = double |
| 112 | + } else if let dict = try? decode([String: Any].self, forKey: key) { |
| 113 | + dictionary[key.stringValue] = dict |
| 114 | + } else if let array = try? decode([Any].self, forKey: key) { |
| 115 | + dictionary[key.stringValue] = array |
| 116 | + } |
| 117 | + } |
| 118 | + return dictionary |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +private extension UnkeyedDecodingContainer { |
| 123 | + mutating func decode(_ type: [Any].Type) throws -> [Any] { |
| 124 | + var elements: [Any] = [] |
| 125 | + while !isAtEnd { |
| 126 | + if try decodeNil() { |
| 127 | + elements.append(NSNull()) |
| 128 | + } else if let int = try? decode(Int.self) { |
| 129 | + elements.append(int) |
| 130 | + } else if let bool = try? decode(Bool.self) { |
| 131 | + elements.append(bool) |
| 132 | + } else if let double = try? decode(Double.self) { |
| 133 | + elements.append(double) |
| 134 | + } else if let string = try? decode(String.self) { |
| 135 | + elements.append(string) |
| 136 | + } else if let values = try? nestedContainer(keyedBy: AnyCodingKey.self), |
| 137 | + let element = try? values.decode([String: Any].self) { |
| 138 | + elements.append(element) |
| 139 | + } else if var values = try? nestedUnkeyedContainer(), |
| 140 | + let element = try? values.decode([Any].self) { |
| 141 | + elements.append(element) |
| 142 | + } |
| 143 | + } |
| 144 | + return elements |
| 145 | + } |
| 146 | +} |
0 commit comments