|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/** |
| 4 | + A type-erased `Decodable` value. |
| 5 | + |
| 6 | + The `AnyDecodable` type forwards decoding responsibilities |
| 7 | + to an underlying value, hiding its specific underlying type. |
| 8 | + |
| 9 | + You can decode mixed-type values in dictionaries |
| 10 | + and other collections that require `Decodable` conformance |
| 11 | + by declaring their contained type to be `AnyDecodable`: |
| 12 | + |
| 13 | + let json = """ |
| 14 | + { |
| 15 | + "boolean": true, |
| 16 | + "integer": 42, |
| 17 | + "double": 3.141592653589793, |
| 18 | + "string": "string", |
| 19 | + "array": [1, 2, 3], |
| 20 | + "nested": { |
| 21 | + "a": "alpha", |
| 22 | + "b": "bravo", |
| 23 | + "c": "charlie" |
| 24 | + }, |
| 25 | + "null": null |
| 26 | + } |
| 27 | + """.data(using: .utf8)! |
| 28 | + |
| 29 | + let decoder = JSONDecoder() |
| 30 | + let dictionary = try! decoder.decode([String: AnyDecodable].self, from: json) |
| 31 | + */ |
| 32 | +@frozen public struct AnyDecodable: Decodable { |
| 33 | + public let value: Any |
| 34 | + |
| 35 | + public init(_ value: (some Any)?) { |
| 36 | + self.value = value ?? () |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +@usableFromInline |
| 41 | +protocol _AnyDecodable { |
| 42 | + var value: Any { get } |
| 43 | + init<T>(_ value: T?) |
| 44 | +} |
| 45 | + |
| 46 | +extension AnyDecodable: _AnyDecodable {} |
| 47 | + |
| 48 | +extension _AnyDecodable { |
| 49 | + public init(from decoder: Decoder) throws { |
| 50 | + let container = try decoder.singleValueContainer() |
| 51 | + |
| 52 | + if container.decodeNil() { |
| 53 | +#if canImport(Foundation) |
| 54 | + self.init(NSNull()) |
| 55 | +#else |
| 56 | + self.init(Self?.none) |
| 57 | +#endif |
| 58 | + } else if let bool = try? container.decode(Bool.self) { |
| 59 | + self.init(bool) |
| 60 | + } else if let int = try? container.decode(Int.self) { |
| 61 | + self.init(int) |
| 62 | + } else if let uint = try? container.decode(UInt.self) { |
| 63 | + self.init(uint) |
| 64 | + } else if let double = try? container.decode(Double.self) { |
| 65 | + self.init(double) |
| 66 | + } else if let string = try? container.decode(String.self) { |
| 67 | + self.init(string) |
| 68 | + } else if let data = try? container.decode(Data.self) { |
| 69 | + self.init(data) |
| 70 | + } else if let date = try? container.decode(Date.self) { |
| 71 | + self.init(date) |
| 72 | + } else if let array = try? container.decode([AnyDecodable].self) { |
| 73 | + self.init(array.map(\.value)) |
| 74 | + } else if let dictionary = try? container.decode([String: AnyDecodable].self) { |
| 75 | + self.init(dictionary.mapValues { $0.value }) |
| 76 | + } else { |
| 77 | + throw DecodingError.dataCorruptedError(in: container, debugDescription: "AnyDecodable value cannot be decoded") |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +extension AnyDecodable: Equatable { |
| 83 | + public static func == (lhs: AnyDecodable, rhs: AnyDecodable) -> Bool { |
| 84 | + switch (lhs.value, rhs.value) { |
| 85 | +#if canImport(Foundation) |
| 86 | + case is (NSNull, NSNull), is (Void, Void): |
| 87 | + return true |
| 88 | +#endif |
| 89 | + case let (lhs as Bool, rhs as Bool): |
| 90 | + return lhs == rhs |
| 91 | + case let (lhs as Int, rhs as Int): |
| 92 | + return lhs == rhs |
| 93 | + case let (lhs as Int8, rhs as Int8): |
| 94 | + return lhs == rhs |
| 95 | + case let (lhs as Int16, rhs as Int16): |
| 96 | + return lhs == rhs |
| 97 | + case let (lhs as Int32, rhs as Int32): |
| 98 | + return lhs == rhs |
| 99 | + case let (lhs as Int64, rhs as Int64): |
| 100 | + return lhs == rhs |
| 101 | + case let (lhs as UInt, rhs as UInt): |
| 102 | + return lhs == rhs |
| 103 | + case let (lhs as UInt8, rhs as UInt8): |
| 104 | + return lhs == rhs |
| 105 | + case let (lhs as UInt16, rhs as UInt16): |
| 106 | + return lhs == rhs |
| 107 | + case let (lhs as UInt32, rhs as UInt32): |
| 108 | + return lhs == rhs |
| 109 | + case let (lhs as UInt64, rhs as UInt64): |
| 110 | + return lhs == rhs |
| 111 | + case let (lhs as Float, rhs as Float): |
| 112 | + return lhs == rhs |
| 113 | + case let (lhs as Double, rhs as Double): |
| 114 | + return lhs == rhs |
| 115 | + case let (lhs as String, rhs as String): |
| 116 | + return lhs == rhs |
| 117 | + case let (lhs as Data, rhs as Data): |
| 118 | + return lhs == rhs |
| 119 | + case let (lhs as Date, rhs as Date): |
| 120 | + return lhs == rhs |
| 121 | + case let (lhs as [String: AnyDecodable], rhs as [String: AnyDecodable]): |
| 122 | + return lhs == rhs |
| 123 | + case let (lhs as [AnyDecodable], rhs as [AnyDecodable]): |
| 124 | + return lhs == rhs |
| 125 | + default: |
| 126 | + return false |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +extension AnyDecodable: CustomStringConvertible { |
| 132 | + public var description: String { |
| 133 | + switch value { |
| 134 | + case is Void: |
| 135 | + String(describing: nil as Any?) |
| 136 | + case let value as CustomStringConvertible: |
| 137 | + value.description |
| 138 | + default: |
| 139 | + String(describing: value) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +extension AnyDecodable: CustomDebugStringConvertible { |
| 145 | + public var debugDescription: String { |
| 146 | + switch value { |
| 147 | + case let value as CustomDebugStringConvertible: |
| 148 | + "AnyDecodable(\(value.debugDescription))" |
| 149 | + default: |
| 150 | + "AnyDecodable(\(description))" |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +extension AnyDecodable: Hashable { |
| 156 | + public func hash(into hasher: inout Hasher) { |
| 157 | + switch value { |
| 158 | + case let value as Bool: |
| 159 | + hasher.combine(value) |
| 160 | + case let value as Int: |
| 161 | + hasher.combine(value) |
| 162 | + case let value as Int8: |
| 163 | + hasher.combine(value) |
| 164 | + case let value as Int16: |
| 165 | + hasher.combine(value) |
| 166 | + case let value as Int32: |
| 167 | + hasher.combine(value) |
| 168 | + case let value as Int64: |
| 169 | + hasher.combine(value) |
| 170 | + case let value as UInt: |
| 171 | + hasher.combine(value) |
| 172 | + case let value as UInt8: |
| 173 | + hasher.combine(value) |
| 174 | + case let value as UInt16: |
| 175 | + hasher.combine(value) |
| 176 | + case let value as UInt32: |
| 177 | + hasher.combine(value) |
| 178 | + case let value as UInt64: |
| 179 | + hasher.combine(value) |
| 180 | + case let value as Float: |
| 181 | + hasher.combine(value) |
| 182 | + case let value as Double: |
| 183 | + hasher.combine(value) |
| 184 | + case let value as String: |
| 185 | + hasher.combine(value) |
| 186 | + case let value as Data: |
| 187 | + hasher.combine(value) |
| 188 | + case let value as Date: |
| 189 | + hasher.combine(value) |
| 190 | + case let value as [String: AnyDecodable]: |
| 191 | + hasher.combine(value) |
| 192 | + case let value as [AnyDecodable]: |
| 193 | + hasher.combine(value) |
| 194 | + default: |
| 195 | + break |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments