I make HTTP requests to a server and I receive JSON documents. I have the following structure to decode the JSON:
struct DocumenJSON: Codable {
let code: Int?
let description: String?
let value: Value?
}
The problem is that making a request "A" I receive an Object value and making the request "B" an Array of Value, so the struct should be the following:
struct DocumenJSONArray: Codable {
let code: Int?
let description: String?
let value: [Value]?
}
How can I implement this in swift 4 without duplicate code?
} catch let jsonErr {
print("Error serializing json:", jsonErr)
do {
document = try JSONDecoder().decode(DocumenJSON.self, from: data)
user = User.init(password: "", email: document?.value?.email ?? "Empty", givenNames: document?.value?.nickname ?? "Empty", familyName: document?.value?.lastname ?? "Empty", phone: document?.value?.nickname ?? "Empty")
} catch let jsonErr2 {
print("Error serializing json2:", jsonErr2)
}
}