|
| 1 | +// |
| 2 | +// SPCCPAArbitraryJson.swift |
| 3 | +// CCPAConsentViewController |
| 4 | +// |
| 5 | +// Created by Vilas on 13/12/20. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public enum SPCCPAArbitraryJson: Codable, CustomStringConvertible, Equatable { |
| 11 | + public var description: String { |
| 12 | + switch self { |
| 13 | + case .string(let string): return "\"\(string)\"" |
| 14 | + case .number(let double): |
| 15 | + if let int = Int(exactly: double) { |
| 16 | + return "\(int)" |
| 17 | + } else { |
| 18 | + return "\(double)" |
| 19 | + } |
| 20 | + case .object(let object): |
| 21 | + let keyValues = object |
| 22 | + .map { (key, value) in "\"\(key)\": \(value)" } |
| 23 | + .joined(separator: ",") |
| 24 | + return "{\(keyValues)}" |
| 25 | + case .array(let array): |
| 26 | + return "\(array)" |
| 27 | + case .bool(let bool): |
| 28 | + return "\(bool)" |
| 29 | + case .null: |
| 30 | + return "null" |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public struct Key: CodingKey, Hashable, CustomStringConvertible { |
| 35 | + public var description: String { |
| 36 | + return stringValue |
| 37 | + } |
| 38 | + public let stringValue: String |
| 39 | + public init(_ string: String) { self.stringValue = string } |
| 40 | + public init?(stringValue: String) { self.init(stringValue) } |
| 41 | + public var intValue: Int? { return nil } |
| 42 | + public init?(intValue: Int) { return nil } |
| 43 | + } |
| 44 | + |
| 45 | + case string(String) |
| 46 | + case number(Double) |
| 47 | + case object([Key: SPCCPAArbitraryJson]) |
| 48 | + case array([SPCCPAArbitraryJson]) |
| 49 | + case bool(Bool) |
| 50 | + case null |
| 51 | + |
| 52 | + /// Creates the equivalent of an empty object JSON |
| 53 | + public init() { |
| 54 | + self = .object([:]) |
| 55 | + } |
| 56 | + |
| 57 | + public init(from decoder: Decoder) throws { |
| 58 | + if let string = try? decoder.singleValueContainer().decode(String.self) { |
| 59 | + self = .string(string) |
| 60 | + } else if let number = try? decoder.singleValueContainer().decode(Double.self) { self = .number(number) } else if let object = try? decoder.container(keyedBy: Key.self) { |
| 61 | + var result: [Key: SPCCPAArbitraryJson] = [:] |
| 62 | + for key in object.allKeys { |
| 63 | + result[key] = (try? object.decode(SPCCPAArbitraryJson.self, forKey: key)) ?? .null |
| 64 | + } |
| 65 | + self = .object(result) |
| 66 | + } else if var array = try? decoder.unkeyedContainer() { |
| 67 | + var result: [SPCCPAArbitraryJson] = [] |
| 68 | + for _ in 0..<(array.count ?? 0) { |
| 69 | + result.append(try array.decode(SPCCPAArbitraryJson.self)) |
| 70 | + } |
| 71 | + self = .array(result) |
| 72 | + } else if let bool = try? decoder.singleValueContainer().decode(Bool.self) { |
| 73 | + self = .bool(bool) |
| 74 | + } else if let isNull = try? decoder.singleValueContainer().decodeNil(), isNull { self = .null |
| 75 | + } else { throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], |
| 76 | + debugDescription: "Unknown SPGDPRArbitraryJson type")) } |
| 77 | + } |
| 78 | + |
| 79 | + public func encode(to encoder: Encoder) throws { |
| 80 | + switch self { |
| 81 | + case .string(let string): |
| 82 | + var container = encoder.singleValueContainer() |
| 83 | + try container.encode(string) |
| 84 | + case .number(let number): |
| 85 | + var container = encoder.singleValueContainer() |
| 86 | + try container.encode(number) |
| 87 | + case .bool(let bool): |
| 88 | + var container = encoder.singleValueContainer() |
| 89 | + try container.encode(bool) |
| 90 | + case .object(let object): |
| 91 | + var container = encoder.container(keyedBy: Key.self) |
| 92 | + for (key, value) in object { |
| 93 | + try container.encode(value, forKey: key) |
| 94 | + } |
| 95 | + case .array(let array): |
| 96 | + var container = encoder.unkeyedContainer() |
| 97 | + for value in array { |
| 98 | + try container.encode(value) |
| 99 | + } |
| 100 | + case .null: |
| 101 | + var container = encoder.singleValueContainer() |
| 102 | + try container.encodeNil() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public var objectValue: [String: SPCCPAArbitraryJson]? { |
| 107 | + switch self { |
| 108 | + case .object(let object): |
| 109 | + let mapped: [String: SPCCPAArbitraryJson] = Dictionary(uniqueKeysWithValues: |
| 110 | + object.map { (key, value) in (key.stringValue, value) }) |
| 111 | + return mapped |
| 112 | + default: return nil |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public var arrayValue: [SPCCPAArbitraryJson]? { |
| 117 | + switch self { |
| 118 | + case .array(let array): return array |
| 119 | + default: return nil |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public subscript(key: String) -> SPCCPAArbitraryJson? { |
| 124 | + guard let jsonKey = Key(stringValue: key), |
| 125 | + case .object(let object) = self, |
| 126 | + let value = object[jsonKey] |
| 127 | + else { return nil } |
| 128 | + return value |
| 129 | + } |
| 130 | + |
| 131 | + public var stringValue: String? { |
| 132 | + switch self { |
| 133 | + case .string(let string): return string |
| 134 | + default: return nil |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public var nullValue: Any? { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + public var doubleValue: Double? { |
| 143 | + switch self { |
| 144 | + case .number(let number): return number |
| 145 | + default: return nil |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public var intValue: Int? { |
| 150 | + switch self { |
| 151 | + case .number(let number): return Int(number) |
| 152 | + default: return nil |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public subscript(index: Int) -> SPCCPAArbitraryJson? { |
| 157 | + switch self { |
| 158 | + case .array(let array): return array[index] |
| 159 | + default: return nil |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public var boolValue: Bool? { |
| 164 | + switch self { |
| 165 | + case .bool(let bool): return bool |
| 166 | + default: return nil |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public var anyValue: Any? { |
| 171 | + switch self { |
| 172 | + case .string(let string): return string |
| 173 | + case .number(let number): |
| 174 | + if let int = Int(exactly: number) { return int } else { return number } |
| 175 | + case .bool(let bool): return bool |
| 176 | + case .object(let object): |
| 177 | + return Dictionary(uniqueKeysWithValues: |
| 178 | + object.compactMap { (key, value) -> (String, Any)? in |
| 179 | + if let nonNilValue = value.anyValue { |
| 180 | + return (key.stringValue, nonNilValue) |
| 181 | + } else { return nil } |
| 182 | + }) |
| 183 | + case .array(let array): |
| 184 | + return array.compactMap { $0.anyValue } |
| 185 | + case .null: |
| 186 | + return nil |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public var dictionaryValue: [String: Any]? { |
| 191 | + return anyValue as? [String: Any] |
| 192 | + } |
| 193 | + |
| 194 | + public subscript(dynamicMember member: String) -> SPCCPAArbitraryJson { |
| 195 | + return self[member] ?? .null |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +public extension SPCCPAArbitraryJson { |
| 200 | + init(_ value: Any) throws { |
| 201 | + if let string = value as? String { self = .string(string) } else if let number = value as? NSNumber { |
| 202 | + switch CFGetTypeID(number as CFTypeRef) { |
| 203 | + case CFBooleanGetTypeID(): |
| 204 | + self = .bool(number.boolValue) |
| 205 | + case CFNumberGetTypeID(): |
| 206 | + self = .number(number.doubleValue) |
| 207 | + default: |
| 208 | + throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Cannot encode value")) |
| 209 | + } |
| 210 | + } else if let object = value as? [String: Any] { |
| 211 | + var result: [Key: SPCCPAArbitraryJson] = [:] |
| 212 | + for (key, subvalue) in object { |
| 213 | + result[Key(key)] = try SPCCPAArbitraryJson(subvalue) |
| 214 | + } |
| 215 | + self = .object(result) |
| 216 | + } else if let array = value as? [Any] { |
| 217 | + self = .array(try array.map(SPCCPAArbitraryJson.init)) |
| 218 | + } else if case Optional<Any>.none = value { |
| 219 | + self = .null |
| 220 | + } else if let obj = value as? NSObject, obj.isEqual(NSNull()) { |
| 221 | + self = .null |
| 222 | + } else { |
| 223 | + throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Cannot encode value")) |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments