Merge branch 'feature/TrialFraud-DEVAO-1533' into 'develop'

TrialFraud extension additions

See merge request BPHV_MIPS/mvm_core!194
This commit is contained in:
Pfeil, Scott Robert 2022-02-15 16:11:37 +00:00
commit 7f57d493f6
2 changed files with 16 additions and 0 deletions

View File

@ -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),

View File

@ -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)