-
Notifications
You must be signed in to change notification settings - Fork 23
Closed
Description
The thing you present primarily looks ugly because of the optional chaining at every single step:
if let street = json.person?.address?[0].street.stringValue { }
But it doesn't have to be like that (it is dynamic anyways). I think you could improve that a lot by essentially adding "nil messaging":
if let street = json.person.address[0].street.stringValue { .. }
Instead of returning an optional in every item of the chain, only make the leaf accessors (stringValue, intValue, etc) optional. When a lookup is not successful, return JSON.null (which itself returns JSON.null on all JSON accesses, so that the result propagates).
If you'd like you could also add a JSON.lookupError(parent, key) as a result for missed lookups for debugging purposes (and which also propagates down the call chain, and could even persist the lookup path from the root of the first lookup failure).
Metadata
Metadata
Assignees
Labels
No labels