From 05a549c633ada07e71b73bc33f240680f425f4b3 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 24 Jun 2022 09:01:37 -0500 Subject: [PATCH] added toJSONArray Signed-off-by: Matt Bruce --- MVMCore/MVMCore/Models/Extensions/Encoder.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MVMCore/MVMCore/Models/Extensions/Encoder.swift b/MVMCore/MVMCore/Models/Extensions/Encoder.swift index 9f50bc3..832059b 100644 --- a/MVMCore/MVMCore/Models/Extensions/Encoder.swift +++ b/MVMCore/MVMCore/Models/Extensions/Encoder.swift @@ -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),