added toJSONArray

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-06-24 09:01:37 -05:00
parent 5ca22837bf
commit 316c0a04ae

View File

@ -34,6 +34,15 @@ extension Encodable {
return jsonAny
}
public func toJSONArray() throws -> [Any] {
let data = try self.encode()
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
guard let jsonArray = json as? [Any] else {
throw JSONError.error(message: "JSON Array not found")
}
return jsonArray
}
public func toJSONString() -> String? {
guard let json = self.toJSON(),
let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted),