Swift Decodable Optional Key
Asked Answered
N

3

28

(This is a follow-up from this question: Using Decodable protocol with multiples keys.)

I have the following Swift code:

let additionalInfo = try values.nestedContainer(keyedBy: UserInfoKeys.self, forKey: .age)
age = try additionalInfo.decodeIfPresent(Int.self, forKey: .realage)

I know that if I use decodeIfPresent and don't have the property it will still handle it correctly if it's an optional variable.

For example the following JSON works to parse it using the code above.

{
    "firstname": "Test",
    "lastname": "User",
    "age": {"realage": 29}
}

And the following JSON works as well.

{
    "firstname": "Test",
    "lastname": "User",
    "age": {"notrealage": 30}
}

But the following doesn't work.

{
    "firstname": "Test",
    "lastname": "User"
}

How can I make all 3 examples work? Is there something similar to decodeIfPresent for nestedContainer?

Neural answered 14/10, 2017 at 2:9 Comment(1)
for every one trying to achieve these kind of requirements, You can use this pod github.com/muhammadali2012/ModelCoffelt
D
74

You can use the following KeyedDecodingContainer function:

func contains(_ key: KeyedDecodingContainer.Key) -> Bool

Returns a Bool value indicating whether the decoder contains a value associated with the given key. The value associated with the given key may be a null value as appropriate for the data format.

For instance, to check if the "age" key exists before requesting the corresponding nested container:

struct Person: Decodable {
    let firstName, lastName: String
    let age: Int?

    enum CodingKeys: String, CodingKey {
        case firstName = "firstname"
        case lastName = "lastname"
        case age
    }

    enum AgeKeys: String, CodingKey {
        case realAge = "realage"
        case fakeAge = "fakeage"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        self.firstName = try values.decode(String.self, forKey: .firstName)
        self.lastName = try values.decode(String.self, forKey: .lastName)

        if values.contains(.age) {
            let age = try values.nestedContainer(keyedBy: AgeKeys.self, forKey: .age)
            self.age = try age.decodeIfPresent(Int.self, forKey: .realAge)
        } else {
            self.age = nil
        }
    }
}
Distemper answered 14/10, 2017 at 13:23 Comment(2)
Is there any solution without the intiailizers?Hula
@ShreeshaKedlaya The only ways without init(from:) is to either use nested structs, like in other answers, or use a library such as dgrzeszczak/KeyedCodable.Gurias
I
17

I had this issue and I found this solution, just in case is helpful to somebody else:

let ageContainer = try? values.nestedContainer(keyedBy: AgeKeys.self, forKey: .age)
self.age = try ageContainer?.decodeIfPresent(Int.self, forKey: .realAge)

If you have an optional container, using try? values.nestedContainer(keyedBy:forKey) you don't need to check if the container exist using contains(.

Isaacs answered 31/10, 2018 at 10:22 Comment(1)
needed this, thanks!Misunderstood
D
3

Can you try pasting your sample JSON into quicktype to see what types it infers? Based on your question, I pasted your samples and got:

struct UserInfo: Codable {
    let firstname: String
    let age: Age?
    let lastname: String
}

struct Age: Codable {
    let realage: Int?
}

Making UserInfo.age and Age.realage optionals works, if that's what you're trying to accomplish.

Disquieting answered 14/10, 2017 at 5:15 Comment(3)
I'm trying to achieve this with 1 class/struct. I have specific reasons for designing it with 1 class/struct. See my question and answer here for more info.Neural
Ah, I see – I have to agree with the answer there that suggests a 'raw' model corresponding to the JSON structure, which you could map to your higher-level model with the idiosyncratic behavior you desire! I think this could be much easier to maintain and understand than implementing custom decoders, but good luck either way!Disquieting
Just the way my data is structured in my server files and such makes it a lot easier to use custom decoders. And just from my understanding and structuring it's a lot easier to do it this way. Maybe someone will be able to give an answer that fits what I want. Thanks for your help tho.Neural

© 2022 - 2025 — McMap. All rights reserved.