There seems to be a method .null which returns NSNull to represent a null JSON object, or nil to represent some other JSON. This is quite confusing. Would it not be better to have a method isNull to check if the JSON represents a null? Which of the following is more intuitive?
if myJSON.null != nil
{
// Object represents a null
}
if myJSON.isNull
{
// Object represents a null
}
Also how should isEmpty behave if self.type is .Null? I would think it should return true but currently it returns false.
I've only been learning swift for one day so please just explain if my thinking is wrong.
JSON 's null is a value not Swift's nil.
isEmpty: If type is .Array or .Dictionary, return array.empty or dictonary.empty otherwise return false.
Ok I understand the null value.
Why is isEmpty defined to return false for null values?
For everything except .Array and .Dictionary:
count() returns zerogenerate() returns an empty generatorbut
isEmpty() returns falseThat seems a bit strange to me.
This seems also strange for me.
Currently I have to do this for checking nil values in a json dictionary:
func testNilValue() {
let person = JSON(["name" : "John Wick"])
let personName = person["some_typo_with_name"]
// XCTAssertNil(personName) // obviously compiler error
XCTAssertEqual(personName, "John Wick") // obviously fails
XCTAssert(personName.isEmpty) // test fails
XCTAssertNotNil(personName.null) // works, but not really intuitive
XCTAssert(personName.isNull) // would be way better
}
Proposal:
extension JSON {
public var isNull: Bool {
get {
return self.type == .Null;
}
}
}
and/or extent isEmpty to also return true for .Null
@tangplin wouldn't it be better if isEmpty() defaulted to true? Imho it's very confusing when it returns false for .Null or .Unknown json types.
I had a similar question. Now it looks like the isEmpty code does return true, but the comments still say it returns false.
This issue has been inactive for a while so I'm gonna go ahead and consider it fixed. Please let us know if you want to reopen it.
Did this ever get fixed? Null checking should be trivial, not completely confusing. I still don't know how the framework doesn't have a simple "isNull"...???
Totally agree. As always in code, simplicity and syntax are important
@ldiqual Lois can we re-open this? Still can't figure out how to (reliably) know how to check for null values in SwiftyJson! This is crazy. Let's add that 'isNull' already and be done with it!
@ldiqual we found a bug why this wasn't reliable and driving us crazy. If you store a JSON result in a variable the null value gets lost on it! This is quite critical and unexpected.
Example:
let json:JSON = .... /*from somewhere*/
// this works
print( json["Data"].null == NSNull() ) // prints true
// this doesn't!
let dataJSON:JSON = json["Data"]
print( dataJSON.null == NSNull() ) // prints false
So, what is the proper way to check for absent value now?
Did you guys manage a proper consensus?
Just done my migration and this keeps popping warnings everywhere
+1
+1, I prefer isEmpty to check JSON object. Does it support now ?
Same question here, any solutions or should we create a PR?
Most helpful comment
Did this ever get fixed? Null checking should be trivial, not completely confusing. I still don't know how the framework doesn't have a simple "isNull"...???