Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Fixing missingKeyBecomesNil being recursive #256

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

nerdsupremacist
Copy link

@nerdsupremacist nerdsupremacist commented Jun 29, 2017

Here's a fix for the .missingKeyBecomesNil Option catching errors from decode calls below.

For instance:

     struct A: JSONDecodable {
            let b: B?
            
            init(json: JSON) throws {
                b = try json.decode(at: "b", alongPath: [.missingKeyBecomesNil])
            }
            
        }
        
        struct B: JSONDecodable {
            let string: String
            
            init(json: JSON) throws {
                string = try json.decode(at: "string")
            }
        }

If I were to call decode with the following invalid json:

{
    "b": {
    
    }
}

The previous implementation wouldn't throw an error but return A(b: nil)

If you still want the previous functionality you can use the parameter applyRecursively.

 b = try json.decode(at: "b", alongPath: [.missingKeyBecomesNil]) // Will throw an error
 b = try json.decode(at: "b", alongPath: [.missingKeyBecomesNil], applyRecursively: true) // nil

@nerdsupremacist
Copy link
Author

@zwaldowski I hope you like this! ;)

@nerdsupremacist
Copy link
Author

nerdsupremacist commented Aug 18, 2017

@mdmathias thoughts? I could also make the applyRecursively flag a case in JSON.SubscriptingOptions if you prefer. Although it could lead to some confusion

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant