Let's assume we have a JSON structure as following:
{
"July": [
{
...
"startDate": "July 10",
"endDate": "July 11",
...
},
{
...
},
{
...
},
{
...
}
]
}
I am trying to parse this API with the following struct, using only native swift.
struct Listing: Codable {
let months: [Month]
enum CodingKeys: String, CodingKey {
case months = "June" //here we need all months for the whole year.
}
}
struct Month: Codable {
...
let startDate: String
let endDate: String
...
enum CodingKeys: String, CodingKey {
...
}
}
The problem is that the API will return per request every time a new JSON response with a new month thus I need a couple of "CodingKeys" cases: "July", "August" etc, in the same time the Month struct is reusable. There was an idea to solve the issue mapping the entity, though I guess there can be a more elegant solution. Please let me know if you have any ideas how to simplify the solution.
Monthor wouldListing.monthsbe a dictionary mapping month to Month? Do you actually care about the word "July" since you have the startDate/endDate? Or would you rather throw away the "July" level?Codable. Do you really need to encode them? You have many more options if you make this onlyDecodable.