diff --git a/Sources/XXClient/Models/NotificationReport.swift b/Sources/XXClient/Models/NotificationReport.swift index 06eb1d89c273fcfec99e8d4615936a1d9b8ff237..3a0192cca17d9e0392302800c298f591389a02dc 100644 --- a/Sources/XXClient/Models/NotificationReport.swift +++ b/Sources/XXClient/Models/NotificationReport.swift @@ -45,3 +45,13 @@ extension NotificationReport: Codable { try JSONEncoder().encode(self) } } + +extension Array where Element == NotificationReport { + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/XXClientTests/Models/NotificationReportTests.swift b/Tests/XXClientTests/Models/NotificationReportTests.swift index 9e99d82544e5631c26142d4a43b5a64ba96342bc..4b8372fc8669d0a660325412dcbf72b5ad5a7877 100644 --- a/Tests/XXClientTests/Models/NotificationReportTests.swift +++ b/Tests/XXClientTests/Models/NotificationReportTests.swift @@ -28,4 +28,43 @@ final class NotificationReportTests: XCTestCase { XCTAssertNoDifference(decodedModel, model) } + + func testCodingArray() throws { + let source1B64 = "dGVzdGVyMTIzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + let source2B64 = "ciI1cpyUUY/UPaVeMy1zBFWbZRgiZSXhY+cVoM+fCxwD" + let jsonString = """ + [ + { + "ForMe": true, + "Type": "\(NotificationReport.ReportType.default.rawValue)", + "Source": "\(source1B64)" + }, + { + "ForMe": false, + "Type": "\(NotificationReport.ReportType.request.rawValue)", + "Source": "\(source2B64)" + }, + ] + """ + let jsonData = jsonString.data(using: .utf8)! + let models = try [NotificationReport].decode(jsonData) + + XCTAssertNoDifference(models, [ + NotificationReport( + forMe: true, + type: .default, + source: Data(base64Encoded: source1B64)! + ), + NotificationReport( + forMe: false, + type: .request, + source: Data(base64Encoded: source2B64)! + ) + ]) + + let encodedModels = try models.encode() + let decodedModels = try [NotificationReport].decode(encodedModels) + + XCTAssertNoDifference(decodedModels, models) + } }