Skip to content
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

Option to change NSNull() return value to a real zero value #77

Closed
dhoerl opened this issue Oct 21, 2014 · 6 comments
Closed

Option to change NSNull() return value to a real zero value #77

dhoerl opened this issue Oct 21, 2014 · 6 comments
Labels

Comments

@dhoerl
Copy link

dhoerl commented Oct 21, 2014

This is a hugely valuable project you created!

One huge issue I had in my last job was JSON nulls being returned by the web service. For instance, if someone had forgotten to set inventory, I'd get a null instead of zero. So I hacked up a category on NSNull so that I didn't have to test every single value before referencing it. The idea was that in most code, a zero would be just as useful as a null.

You can see others agree by the number of upvotes this answer got on SO: http://stackoverflow.com/a/16610117/1633251

What I am requesting is that you support an option that would result in no nulls being returned, with empty dictionaries, empty arrays, zero length strings, etc being returned instead.

If you want, I'll do the code and submit a pull request, but just tell me exactly how you want the option to work (and the actual string name if you care).

@LukeTangPL
Copy link
Member

@dhoerl thank you very much. The NSNull Category which you post on stackoverflow in Objective-C is a masterpiece solution.
There are few way in SwiftyJSON to get NSNull object are var null: NSNull? and var object: AnyObject(better not use), other methods and properties that return the swift type in SwfityJSON are safe and reliable. such as:

let data: NSData = dataFromNetworking()
let json = JSON(data: data)
if let mayBeNull = json.null {
    //It's null
} else {
    //It's not null
}
if let string = json.string {
     //It's String or NSString
} {
     //It's not String or NSString
}

// If json is .String return the object as String, 
// if json is.Number or .Bool convert the object to String
// Otherwise return empty string
let iDontCare = json["iDontCare"].stringValue 

It's seem like NSNull Category.
If your have any idea, please let me know or submit a pull request.
Thanks.

@antoinerabanes
Copy link

Ideally if the value is null, the key should be removed.
in turn when you query the collection for this key it will return nil

@dhoerl
Copy link
Author

dhoerl commented Jan 30, 2015

I completely disagree on deleting the key. A 'null' JSON value is a valid return, and has meaning. I've experience with actual e-commerce JSON replies containing:

inventory : null // means it hasn't been entered yet
inventory: 50 // in stock at that level
inventory: -3 // yeah, got this! - the stupid e-commerce site sold 3 items before it realized it didn't have them in stock, so someone is on the phone now trying to calm a customer down (this really happened and crashed my app - I had coded inventory as a unsigned integer!)

@antoinerabanes
Copy link

Thats interesting insight and a good example of the complexity of integrating an API that returns null/NSNull.
However I would expect an integer back from the API and zero is a valid value to represent the stock when null is not because it is a different type and does not represent a number..

Sent from my iPhone

On 30 Jan 2015, at 17:00, David Hoerl notifications@github.com wrote:

I completely disagree on deleting the key. A 'null' JSON value is a valid return, and has meaning. I've experience with actual e-commerce JSON returns containing such:

inventory : null // means it hasn't been entered yet
inventory: 50 // in stock at that level
inventory: -3 // yeah, got this - the stupid e-commerce site sold 3 items before it realized it didn't have them in stock, so someone is on the phone now trying to calm a client down (yeah, this happened and crashed my app!)


Reply to this email directly or view it on GitHub.

@dhoerl
Copy link
Author

dhoerl commented Jan 30, 2015

All I can say is my comment reflects Apple's current thinking. Swift Optionals either have a value or don't. The value can be 0 or not (if its say an Int). Each of these three states (as stated in my earlier example) means something different. A framework like this one cannot afford (IMHO) to gloss over this and assume all users want "no value" to be mapped into "0" (or similar).

That said, after fussing with nil for weeks, I threw in the towel and created the NSNull category above, which gives you exactly what you want - two cases. Even so, I prefer this framework to do the proper thing, and let the user decide to map 3 states to 2 if desired.

@Nelyus
Copy link

Nelyus commented Feb 4, 2015

@dhoerl You could something like this, without any change to JSON:

if let number = json.number?.integerValue ?? json.null?.integerValue {
  // do stuff
}

It'd be more explicit, which can be good.

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

No branches or pull requests

4 participants