Is it possible to return the successfully decoded objects of an array if one of the objects throws a DecodingError? I'm communicating with quite a ropey API which intermittently sends me some malformed JSON which causes a DecodingError to be thrown and the entire array of objects (including valid entries) is not parsed.
I don't know if there's some way to do it manually, but given an array of JSON with some duff data in it, I'd like to somehow still get at the objects which can be successfully decoded.
EDIT: As requested, code sample:
let json = """
[
{"prop1": 1, "prop2": 2, "prop3": 3},
{"prop1": 1, "prop2": 2, "prop3": 3},
{"prop1": 1, "prop2": 2}
]
""".data(using: .utf8, allowLossyConversion: false)!
struct MyStruct: Codable {
let prop1: Int
let prop2: Int
let prop3: Int
}
// Throws keyNotFound (uncaught obviously) and returns nil because prop3 in 3rd object is missing
let decoded = try? JSONDecoder().decode(MyStruct.self, from: json)
Obviously a contrived example, but hopefully illustrates my point. If a mandatory property is missing from an object in the response array then I'd like that object to be removed from the list of decoded objects rather than bombing out and returning nothing.
This also applies to all the other DecodingErrors (dataCorrupted, typeMismatch, valueNotFound).