New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to check for null
in JSON with is NSNull
.
#8
Conversation
@kreeger in the event that a key is not found, public var isNull: Bool {
return (object == nil) || object is NSNull
} |
Well, the way I'm using this, I need to check that the key is there, and also if its value is if json["feature"].isNull {
// Nil out the feature object if this document was not assigned one.
feature = nil
} else {
feature = SomeObject(fromJSON: json["feature"])
} With your change, this would force me to check if the object from the JSON is non- if !json["feature"].isNil && json["feature"].isNull {
// Nil out the feature object if this document was not assigned one.
feature = nil
} else {
feature = SomeObject(fromJSON: json["feature"])
} And that feels odd to me. |
In the case where |
@jlandon That's not the case; But in this case, I do care if the key for But if the |
Hmm, that seems like a pretty unique situation. Since ModelRocket doesn't natively support Realm, I'd say that it makes more sense for this to be added via an extension to your project. To me, Here's what I propose: Then you could simply add an extension to your project like this: extension JSON {
var validKeyNullValue: Bool {
return !isNil && isNull
}
} Does that seem reasonable? |
@jlandon I still think in my mind that |
Per out-of-band discussion, I'm going to amend this PR so that we expose two computed boolean properties: |
Tell you what — let me close this PR and open up a new one with cleaner history (#9). |
The inability for us to check if a key exists, but is
null
, in the JSON was kind of a bummer, so I wrote this up.Note that this is different for checking if a key doesn't exist, which is where you'd use
json["not_a_key"].isNil
. Instead, if you have an object in the JSON as such…Then you check if that object is null using the change in this PR by asking
This PR comes complete with unit test addition!