Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip the value of UnkeyedDecodingContainer while decoding?

Tags:

swift

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?

like image 368
Aleksey Timoshchenko Avatar asked Nov 16 '25 19:11

Aleksey Timoshchenko


2 Answers

This question was already discussed here: Swift JSONDecode decoding arrays fails if single element decoding fails

You can catch an error and decode a "DummyCodable" like that

catch let error {
    _ = try? itemContainer.decode(DummyCodable.self)
}

DummyCodable looks like:

public struct DummyCodable: Codable {}
like image 65
Patrick_K Avatar answered Nov 19 '25 10:11

Patrick_K


Eventually, I went with this solution - https://forums.swift.org/t/pitch-unkeyeddecodingcontainer-movenext-to-skip-items-in-deserialization/22151/17 (thanks to @Larme)

struct Empty: Decodable { }

extension UnkeyedDecodingContainer {
  public mutating func skip() throws {
    _ = try decode(Empty.self)
  }
}
like image 41
Aleksey Timoshchenko Avatar answered Nov 19 '25 08:11

Aleksey Timoshchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!