Skip to content
Snippets Groups Projects
Commit ceb3fc62 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Merge branch 'fix/empty-facts-list-coding' into 'development'

Encode/decode empty facts array to/from "null"

See merge request elixxir/elixxir-dapps-sdk-swift!69
parents 790000c1 6c31fc64
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!69Encode/decode empty facts array to/from "null"
......@@ -30,10 +30,16 @@ extension Fact: Codable {
extension Array where Element == Fact {
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
if let string = String(data: data, encoding: .utf8), string == "null" {
return []
}
return try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
if isEmpty {
return "null".data(using: .utf8)!
}
return try JSONEncoder().encode(self)
}
}
......@@ -38,4 +38,17 @@ final class FactTests: XCTestCase {
XCTAssertNoDifference(models, decodedModels)
}
func testCodingEmptyArray() throws {
let jsonString = "null"
let jsonData = jsonString.data(using: .utf8)!
let decodedModels = try [Fact].decode(jsonData)
XCTAssertNoDifference(decodedModels, [])
let encodedModels = try decodedModels.encode()
XCTAssertNoDifference(encodedModels, jsonData)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment