Open nerdsupremacist opened 7 years ago
Here's a fix for the .missingKeyBecomesNil Option catching errors from decode calls below.
.missingKeyBecomesNil
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)
A(b: nil)
If you still want the previous functionality you can use the parameter applyRecursively.
applyRecursively
b = try json.decode(at: "b", alongPath: [.missingKeyBecomesNil]) // Will throw an error b = try json.decode(at: "b", alongPath: [.missingKeyBecomesNil], applyRecursively: true) // nil
@zwaldowski I hope you like this! ;)
@mdmathias thoughts? I could also make the applyRecursively flag a case in JSON.SubscriptingOptions if you prefer. Although it could lead to some confusion
JSON.SubscriptingOptions
Here's a fix for the
.missingKeyBecomesNil
Option catching errors from decode calls below.For instance:
If I were to call decode with the following invalid json:
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
.