I’m coding a REST Web app client and I use JSON which looks like this:
JSON1
{
"device" : "iPhone"
"manufacturer" : "Apple"
"id" : 42
"owner" : "Steve"
}
But the API could give me this kind of JSON also
JSON2
{
"device" : "iPhone"
"manufacturer" : "Apple"
"id" : 42
"latitude" : 3.1415926535
"longitude" : 2.7182818284
}
So now in my app I create a struct who conforms to the Codable protocol
struct MyStruct : Codable {
var name: String
var manufacturer: String
var owner: String?
// I prefer to use a property of type Location rather than 2 variables
var location: Location? {
guard let latitude = latitude, let longitude = longitude else {
return nil
}
return Location(latitude: latitude, longitude: longitude)
}
// Used to conform to the Codable protocol
private var latitude: Double?
private var longitude: Double?
}
struct Location {
var latitude: Double = 0.0
var longitude: Double = 0.0
}
This architecture works but it seems to me not the best one or elegant. Would you know if a better approach exists? Should I use 2 differents json model instead like:
struct MyStruct1 : Codable {
var name: String
var manufacturer: String
var owner: String
}
struct MyStruct2 : Codable {
var name: String
var manufacturer: String
private var latitude: Double
private var longitude: Double
}
I'm new to REST API client and this kind of JSON doesn't seems to use a good architecture.