diff --git a/MVMCore/MVMCore/Models/Extensions/Encoder.swift b/MVMCore/MVMCore/Models/Extensions/Encoder.swift index db9c762..9f50bc3 100644 --- a/MVMCore/MVMCore/Models/Extensions/Encoder.swift +++ b/MVMCore/MVMCore/Models/Extensions/Encoder.swift @@ -25,6 +25,15 @@ extension Encodable { return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? JSONDictionary } } + public func toJSONAny() throws -> [String: Any] { + let data = try self.encode() + let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) + guard let jsonAny = json as? [String: Any] else { + throw JSONError.error(message: "JSON Dictionary not found") + } + return jsonAny + } + public func toJSONString() -> String? { guard let json = self.toJSON(), let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted), diff --git a/MVMCore/MVMCore/Models/JSON/JSONValue.swift b/MVMCore/MVMCore/Models/JSON/JSONValue.swift index 43796cb..333fc91 100644 --- a/MVMCore/MVMCore/Models/JSON/JSONValue.swift +++ b/MVMCore/MVMCore/Models/JSON/JSONValue.swift @@ -119,6 +119,13 @@ extension Dictionary where Key == String, Value == Any { } } +extension Dictionary where Key == String, Value == AnyHashable { + public func toJSONValue() throws -> [String: JSONValue] { + let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) + return try JSONDecoder().decode([String:JSONValue].self, from: data) + } +} + extension Dictionary where Key == String, Value == JSONValue { public func toJSONObject() throws -> JSONDictionary { let encoded = try JSONEncoder().encode(self)