I need to decode a JSON with my custom implementation
internal init(from decoder: Decoder) throws {
var container: UnkeyedDecodingContainer = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let obj = try? container.decode(Node.self) {
parserableArr.append(obj)
}
else if let obj = try? container.decode(NodeGPU.self) {
parserableArr.append(obj)
}
}
}
So, the flow is - I get container
and try to decode it with each type one by one. The problem is that if I didn't find the needed type I was stuck in an infinity loop, because the while !container.isAtEnd
is never stopped.
I am looking for a method like container.skipValue()
, so I can use it like this
...
else {
container.skipValue()
}
...
But there is no such method.
P.S. Of course I can use a kind of workaround and provide kind of the dummy implementation in the last else
state which won't throw, but I am wondering if there is a way to make it work without such a workaround?