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

Merge branch 'development' into dev/update-bindings

parents ac6bf60b ceb3fc62
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!67Update Bindings
...@@ -30,10 +30,16 @@ extension Fact: Codable { ...@@ -30,10 +30,16 @@ extension Fact: Codable {
extension Array where Element == Fact { extension Array where Element == Fact {
public static func decode(_ data: Data) throws -> Self { 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 { 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 { ...@@ -38,4 +38,17 @@ final class FactTests: XCTestCase {
XCTAssertNoDifference(models, decodedModels) 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