Merge branch 'feature/JSONValue-Update' into 'develop'

added more extension methods

See merge request BPHV_MIPS/mvm_core!217
This commit is contained in:
Pfeil, Scott Robert 2022-07-22 13:16:18 +00:00
commit 064fd5d621

View File

@ -145,17 +145,55 @@ extension JSONValue: ExpressibleByDictionaryLiteral {
}
}
extension Dictionary where Key == String, Value == Any {
public func toJSONValue() throws -> [String: JSONValue] {
extension Array where Element == Any {
public func toJSONValue() throws -> JSONValue {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode([String:JSONValue].self, from: data)
return try JSONDecoder().decode(JSONValue.self, from: data)
}
}
extension Array where Element == AnyHashable {
public func toJSONValue() throws -> JSONValue {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode(JSONValue.self, from: data)
}
}
extension Array where Element == JSONValue {
public func toJSONArray() throws -> [Any] {
let encoded = try JSONEncoder().encode(self)
guard let json = try JSONSerialization.jsonObject(with: encoded, options: .mutableContainers) as? [Any] else {
throw JSONValueError.encode
}
return json
}
}
extension Dictionary where Key == String, Value == Any {
public func toJSONValue() throws -> JSONValue {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode(JSONValue.self, from: data)
}
}
extension Dictionary where Key == String, Value == AnyHashable {
public func toJSONValue() throws -> [String: JSONValue] {
public func toJSONValue() throws -> JSONValue {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode([String:JSONValue].self, from: data)
return try JSONDecoder().decode(JSONValue.self, from: data)
}
}
extension Dictionary where Key == String, Value == Any {
public func toJSONValueDictionary() throws -> JSONValueDictionary {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode(JSONValueDictionary.self, from: data)
}
}
extension Dictionary where Key == String, Value == AnyHashable {
public func toJSONValueDictionary() throws -> JSONValueDictionary {
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode(JSONValueDictionary.self, from: data)
}
}