|
| 1 | +import Foundation |
| 2 | + |
| 3 | +struct AnyCodable { |
| 4 | + let value: Any |
| 5 | + |
| 6 | + init<T: Codable>(_ value: T) { |
| 7 | + self.value = value |
| 8 | + } |
| 9 | + |
| 10 | + init(_ value: Any) { |
| 11 | + self.value = value |
| 12 | + } |
| 13 | + |
| 14 | + func get<T>() -> T? { |
| 15 | + value as? T |
| 16 | + } |
| 17 | + |
| 18 | + func get() -> Any { |
| 19 | + value |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +extension AnyCodable: Codable { |
| 24 | + init(from decoder: Decoder) throws { |
| 25 | + let container = try decoder.singleValueContainer() |
| 26 | + |
| 27 | + if container.decodeNil() { |
| 28 | + self.init(()) |
| 29 | + } else if let bool = try? container.decode(Bool.self) { |
| 30 | + self.init(bool) |
| 31 | + } else if let int = try? container.decode(Int.self) { |
| 32 | + self.init(int) |
| 33 | + } else if let uint = try? container.decode(UInt.self) { |
| 34 | + self.init(uint) |
| 35 | + } else if let double = try? container.decode(Double.self) { |
| 36 | + self.init(double) |
| 37 | + } else if let string = try? container.decode(String.self) { |
| 38 | + self.init(string) |
| 39 | + } else if let array = try? container.decode([AnyCodable].self) { |
| 40 | + self.init(array.map { $0.value }) |
| 41 | + } else if let dictionary = try? container.decode([String: AnyCodable].self) { |
| 42 | + self.init(dictionary.mapValues { $0.value }) |
| 43 | + } else { |
| 44 | + throw DecodingError.dataCorruptedError( |
| 45 | + in: container, |
| 46 | + debugDescription: "Not a known JSON Primitive" |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func encode(to encoder: Encoder) throws { |
| 52 | + var container = encoder.singleValueContainer() |
| 53 | + |
| 54 | + switch self.value { |
| 55 | + case is Void: |
| 56 | + try container.encodeNil() |
| 57 | + case let bool as Bool: |
| 58 | + try container.encode(bool) |
| 59 | + case let int as Int: |
| 60 | + try container.encode(int) |
| 61 | + case let int8 as Int8: |
| 62 | + try container.encode(int8) |
| 63 | + case let int16 as Int16: |
| 64 | + try container.encode(int16) |
| 65 | + case let int32 as Int32: |
| 66 | + try container.encode(int32) |
| 67 | + case let int64 as Int64: |
| 68 | + try container.encode(int64) |
| 69 | + case let uint as UInt: |
| 70 | + try container.encode(uint) |
| 71 | + case let uint8 as UInt8: |
| 72 | + try container.encode(uint8) |
| 73 | + case let uint16 as UInt16: |
| 74 | + try container.encode(uint16) |
| 75 | + case let uint32 as UInt32: |
| 76 | + try container.encode(uint32) |
| 77 | + case let uint64 as UInt64: |
| 78 | + try container.encode(uint64) |
| 79 | + case let float as Float: |
| 80 | + try container.encode(float) |
| 81 | + case let double as Double: |
| 82 | + try container.encode(double) |
| 83 | + case let string as String: |
| 84 | + try container.encode(string) |
| 85 | + case let date as Date: |
| 86 | + try container.encode(date) |
| 87 | + case let url as URL: |
| 88 | + try container.encode(url) |
| 89 | + case let array as [Any]: |
| 90 | + try container.encode(array.map { AnyCodable($0) }) |
| 91 | + case let dictionary as [String: Any]: |
| 92 | + try container.encode(dictionary.mapValues { AnyCodable($0) }) |
| 93 | + default: |
| 94 | + let context = EncodingError.Context( |
| 95 | + codingPath: container.codingPath, |
| 96 | + debugDescription: "Value is not codable" |
| 97 | + ) |
| 98 | + throw EncodingError.invalidValue(self.value, context) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +extension AnyCodable: Equatable { |
| 104 | + static func ==(lhs: AnyCodable, rhs: AnyCodable) -> Bool { |
| 105 | + switch (lhs.value, rhs.value) { |
| 106 | + case is (Void, Void): |
| 107 | + return true |
| 108 | + case let (lhs as Bool, rhs as Bool): |
| 109 | + return lhs == rhs |
| 110 | + case let (lhs as Int, rhs as Int): |
| 111 | + return lhs == rhs |
| 112 | + case let (lhs as Int8, rhs as Int8): |
| 113 | + return lhs == rhs |
| 114 | + case let (lhs as Int16, rhs as Int16): |
| 115 | + return lhs == rhs |
| 116 | + case let (lhs as Int32, rhs as Int32): |
| 117 | + return lhs == rhs |
| 118 | + case let (lhs as Int64, rhs as Int64): |
| 119 | + return lhs == rhs |
| 120 | + case let (lhs as UInt, rhs as UInt): |
| 121 | + return lhs == rhs |
| 122 | + case let (lhs as UInt8, rhs as UInt8): |
| 123 | + return lhs == rhs |
| 124 | + case let (lhs as UInt16, rhs as UInt16): |
| 125 | + return lhs == rhs |
| 126 | + case let (lhs as UInt32, rhs as UInt32): |
| 127 | + return lhs == rhs |
| 128 | + case let (lhs as UInt64, rhs as UInt64): |
| 129 | + return lhs == rhs |
| 130 | + case let (lhs as Float, rhs as Float): |
| 131 | + return lhs == rhs |
| 132 | + case let (lhs as Double, rhs as Double): |
| 133 | + return lhs == rhs |
| 134 | + case let (lhs as String, rhs as String): |
| 135 | + return lhs == rhs |
| 136 | + case (let lhs as [String: AnyCodable], let rhs as [String: AnyCodable]): |
| 137 | + return lhs == rhs |
| 138 | + case (let lhs as [AnyCodable], let rhs as [AnyCodable]): |
| 139 | + return lhs == rhs |
| 140 | + default: |
| 141 | + return false |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +extension AnyCodable: CustomStringConvertible { |
| 147 | + var description: String { |
| 148 | + switch value { |
| 149 | + case is Void: |
| 150 | + return String(describing: nil as Any?) |
| 151 | + case let value as CustomStringConvertible: |
| 152 | + return value.description |
| 153 | + default: |
| 154 | + return String(describing: value) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +extension AnyCodable: CustomDebugStringConvertible { |
| 160 | + var debugDescription: String { |
| 161 | + switch value { |
| 162 | + case let value as CustomDebugStringConvertible: |
| 163 | + return "AnyCodable(\(value.debugDescription))" |
| 164 | + default: |
| 165 | + return "AnyCodable(\(self.description))" |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +extension AnyCodable: ExpressibleByNilLiteral, ExpressibleByBooleanLiteral, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByStringLiteral, ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral { |
| 171 | + |
| 172 | + init(nilLiteral: ()) { |
| 173 | + self.init(nil ?? ()) |
| 174 | + } |
| 175 | + |
| 176 | + init(booleanLiteral value: Bool) { |
| 177 | + self.init(value) |
| 178 | + } |
| 179 | + |
| 180 | + init(integerLiteral value: Int) { |
| 181 | + self.init(value) |
| 182 | + } |
| 183 | + |
| 184 | + init(floatLiteral value: Double) { |
| 185 | + self.init(value) |
| 186 | + } |
| 187 | + |
| 188 | + init(extendedGraphemeClusterLiteral value: String) { |
| 189 | + self.init(value) |
| 190 | + } |
| 191 | + |
| 192 | + init(stringLiteral value: String) { |
| 193 | + self.init(value) |
| 194 | + } |
| 195 | + |
| 196 | + init(arrayLiteral elements: Any...) { |
| 197 | + self.init(elements) |
| 198 | + } |
| 199 | + |
| 200 | + init(dictionaryLiteral elements: (AnyHashable, Any)...) { |
| 201 | + self.init(Dictionary<AnyHashable, Any>(elements, uniquingKeysWith: { (first, _) in first })) |
| 202 | + } |
| 203 | +} |
0 commit comments