|
1 | | -// The Swift Programming Language |
2 | | -// https://docs.swift.org/swift-book |
| 1 | +// |
| 2 | +// DynamicJSON.swift |
| 3 | +// DynamicFeatureFlag |
| 4 | +// |
| 5 | +// Created by Hamad Ali on 11/05/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Combine |
| 10 | + |
| 11 | +@dynamicMemberLookup |
| 12 | +public enum DynamicJSON: Decodable { |
| 13 | + case dictionary([String: DynamicJSON]) |
| 14 | + case array([DynamicJSON]) |
| 15 | + case string(String) |
| 16 | + case number(Double) |
| 17 | + case bool(Bool) |
| 18 | + case null |
| 19 | + |
| 20 | + public init(from decoder: Decoder) throws { |
| 21 | + if let container = try? decoder.container(keyedBy: DynamicCodingKeys.self) { |
| 22 | + var dict: [String: DynamicJSON] = [:] |
| 23 | + for key in container.allKeys { |
| 24 | + let normalized = key.stringValue.normalizedKey() |
| 25 | + dict[normalized] = try container.decode(DynamicJSON.self, forKey: key) |
| 26 | + } |
| 27 | + self = .dictionary(dict) |
| 28 | + } else if var arrayContainer = try? decoder.unkeyedContainer() { |
| 29 | + var arr: [DynamicJSON] = [] |
| 30 | + while !arrayContainer.isAtEnd { |
| 31 | + arr.append(try arrayContainer.decode(DynamicJSON.self)) |
| 32 | + } |
| 33 | + self = .array(arr) |
| 34 | + } else if let val = try? decoder.singleValueContainer().decode(Bool.self) { |
| 35 | + self = .bool(val) |
| 36 | + } else if let val = try? decoder.singleValueContainer().decode(Double.self) { |
| 37 | + self = .number(val) |
| 38 | + } else if let val = try? decoder.singleValueContainer().decode(String.self) { |
| 39 | + self = .string(val) |
| 40 | + } else { |
| 41 | + self = .null |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private struct DynamicCodingKeys: CodingKey { |
| 46 | + var stringValue: String |
| 47 | + init?(stringValue: String) { self.stringValue = stringValue } |
| 48 | + var intValue: Int? = nil |
| 49 | + init?(intValue: Int) { nil } |
| 50 | + } |
| 51 | + |
| 52 | + public subscript(dynamicMember key: String) -> DynamicJSON { |
| 53 | + self[key] |
| 54 | + } |
| 55 | + |
| 56 | + public subscript(_ key: String) -> DynamicJSON { |
| 57 | + let parts = key.split(separator: ".").map { String($0).normalizedKey() } |
| 58 | + return parts.reduce(self) { current, part in |
| 59 | + guard case .dictionary(let dict) = current else { return .null } |
| 60 | + if let exact = dict[part] { |
| 61 | + return exact |
| 62 | + } |
| 63 | + return dict.fuzzyMatch(for: part) { original, matched in |
| 64 | + print("⚠️ [DynamicJSON] '\(original)' matched with '\(matched)' via fuzzy/partial logic.") |
| 65 | + } ?? .null |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public var bool: Bool? { |
| 70 | + switch self { |
| 71 | + case .bool(let val): return val |
| 72 | + case .number(let num): return num != 0 |
| 73 | + case .string(let str): return ["1", "true", "yes", "on"].contains(str.lowercased()) |
| 74 | + default: return nil |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public var string: String? { |
| 79 | + switch self { |
| 80 | + case .string(let str): return str |
| 81 | + case .number(let num): |
| 82 | + if num.truncatingRemainder(dividingBy: 1) == 0 { |
| 83 | + return String(Int(num)) |
| 84 | + } else { |
| 85 | + return String(num) |
| 86 | + } |
| 87 | + case .bool(let b): return String(b) |
| 88 | + default: return nil |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public var date: Date? { |
| 93 | + switch self { |
| 94 | + case .string(let str): |
| 95 | + |
| 96 | + // Try ISO8601 first |
| 97 | + let isoFormatter = ISO8601DateFormatter() |
| 98 | + if let date = isoFormatter.date(from: str) { |
| 99 | + return date |
| 100 | + } |
| 101 | + |
| 102 | + let formatter = DateFormatter() |
| 103 | + formatter.locale = Locale(identifier: "en_US_POSIX") |
| 104 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) |
| 105 | + |
| 106 | + // Try fallback formats |
| 107 | + let fallbackFormats = [ |
| 108 | + "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX", |
| 109 | + "yyyy-MM-dd'T'HH:mm:ssXXXXX", |
| 110 | + "yyyy-MM-dd HH:mm:ss", |
| 111 | + "yyyy-MM-dd", |
| 112 | + "MM/dd/yyyy", |
| 113 | + "dd-MM-yyyy" |
| 114 | + ] |
| 115 | + |
| 116 | + for format in fallbackFormats { |
| 117 | + formatter.dateFormat = format |
| 118 | + if let date = formatter.date(from: str) { |
| 119 | + return date |
| 120 | + } |
| 121 | + } |
| 122 | + return nil |
| 123 | + case .number(let num): |
| 124 | + // If it's a large number, treat as milliseconds |
| 125 | + if num > 1_000_000_000_000 { |
| 126 | + return Date(timeIntervalSince1970: num / 1000) |
| 127 | + } else { |
| 128 | + return Date(timeIntervalSince1970: num) |
| 129 | + } |
| 130 | + default: return nil |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public var int: Int? { |
| 135 | + switch self { |
| 136 | + case .number(let num): |
| 137 | + return Int(num) |
| 138 | + case .string(let str): |
| 139 | + if let intVal = Int(str) { |
| 140 | + return intVal |
| 141 | + } else if let doubleVal = Double(str) { |
| 142 | + return Int(doubleVal) |
| 143 | + } else if ["true", "yes", "on"].contains(str.lowercased()) { |
| 144 | + return 1 |
| 145 | + } else if ["false", "no", "off"].contains(str.lowercased()) { |
| 146 | + return 0 |
| 147 | + } |
| 148 | + return nil |
| 149 | + case .bool(let b): |
| 150 | + return b ? 1 : 0 |
| 151 | + default: |
| 152 | + return nil |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public var double: Double? { |
| 157 | + switch self { |
| 158 | + case .number(let num): return num |
| 159 | + case .string(let str): |
| 160 | + if let doubleVal = Double(str) { |
| 161 | + return doubleVal |
| 162 | + } else if let intVal = Int(str) { |
| 163 | + return Double(intVal) |
| 164 | + } else if ["true", "yes", "on"].contains(str.lowercased()) { |
| 165 | + return 1.0 |
| 166 | + } else if ["false", "no", "off"].contains(str.lowercased()) { |
| 167 | + return 0.0 |
| 168 | + } |
| 169 | + return nil |
| 170 | + case .bool(let b): return b ? 1.0 : 0.0 |
| 171 | + default: |
| 172 | + return nil |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public var isNull: Bool { |
| 177 | + if case .null = self { return true } |
| 178 | + return false |
| 179 | + } |
| 180 | + |
| 181 | + public var dictionary: [String: DynamicJSON]? { |
| 182 | + if case .dictionary(let dict) = self { return dict } |
| 183 | + return nil |
| 184 | + } |
| 185 | + |
| 186 | + public var array: [DynamicJSON]? { |
| 187 | + if case .array(let arr) = self { return arr } |
| 188 | + return nil |
| 189 | + } |
| 190 | + |
| 191 | + public func `as`<T>(_ type: T.Type) -> T? { |
| 192 | + switch type { |
| 193 | + case is Bool.Type: |
| 194 | + return bool as? T |
| 195 | + case is Int.Type: |
| 196 | + return int as? T |
| 197 | + case is Double.Type: |
| 198 | + return double as? T |
| 199 | + case is String.Type: |
| 200 | + return string as? T |
| 201 | + case is Date.Type: |
| 202 | + return date as? T |
| 203 | + default: |
| 204 | + return nil |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments