I'm struggeling to decode some Json data in swift.
My back-end api will return me either an array of X or an object with (at least) one property named "items" of type array of X.
I've searched but found no solutions. Do you have any ?
struct A: Decodable {
var items: [X]
// some other optional properties
public init(from decoder: Decoder) throws {
// Sometimes I receive the correct A object
// Sometimes I only receive the array of X without the surrounding object of type A.
}
}
And to make things worst, I'm bound to decode like I was always receiving an object of typa A... :
myObjectOfTypeA = try decoder.decode(A.self, from: data)
Most of the time, I'll receive a proper A object like this :
{
"items":
[
{
"id": 7,
"startsOn": "2021-03-01",
"endsOn": "2021-12-31"
},
{
"id": 6,
"startsOn": "2021-04-19",
"endsOn": "2022-04-04"
}
],
"next": null,
"prev": null,
"count": 2
}
But sometimes, I'll receive only the items array like this :
[
{
"id": 7,
"startsOn": "2021-03-01",
"endsOn": "2021-12-31"
},
{
"id": 6,
"startsOn": "2021-04-19",
"endsOn": "2022-04-04"
}
]
Any ideas would be greatly appreciated because I'm clearly out of ideas myself...