-
Notifications
You must be signed in to change notification settings - Fork 1.2k
NSJSONSerialization isValidJSONObject Implementation #99
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
Conversation
Foundation/NSJSONSerialization.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would would probably be better implemented using guard
to guarantee the early exit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, scratch that, I would just write inside the outer if let:
let invalid = number.doubleValue.isInfinite || number.doubleValue.isNaN || ...
return !invalid
Addressed review comments. Quick question about
|
code like this: let arr = NSArray(array: [1])
arr is [AnyObject] // true are implicit type conversions from the objc bridging code in the swift standard library and the compiler which do not exist on all platforms: see https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Issues.md |
Gotcha, thanks for the clarification. |
This means that I can't use the filter function as well, correct? I'll update that. |
you can if you use |
Makes more sense to avoid bridging if possible then. Updated to account for this. |
This one is tricky, but I think we need to change this API to take |
I'm a bit confused on what you mean. If we pass in an Are you saying that the function signature should take in an |
The assumption we've decided to make here is that, in the absence of implicit bridging, we have to assume that clients are using the Swift collection and String types instead of the Foundation ones. Hopefully, once we get our bridging story fully figured out here, we can accept either String or NSString. |
Updated to check for Changed function signature of Uncommented and reformatted tests. Added Let me know if this is in line with what you were thinking. As a side note, what do you think about also accepting |
func foo(t: AnyObject) { }
foo(nil) on darwin results in So I would be inclined to claim that NSNull is more appropriate to represent nil |
Right, an optional type would probably have to be used, but since only the internal recursive method would be checking for What I mean:
|
The current NSJSONSerialization only uses NSNull to represent nil. I don't think that we bridge a nil optional to NSNull in Objective-C mode, so if we accept an optional type here then I suspect we lose our cross-platform compatibility... I think it makes total sense to use optionals for this (something we obviously could not do in Objective-C). But that would be an API change, so we'd have to discuss it separately. |
That makes sense - it's definitely something that I think is worth looking into in the long run. Is there anything else that's blocking a merge for this? |
Not really, just need to finish running tests and verify a good state before merging anything else in this morning (last night there were some test breaks that got merged in that I am still trying to account for) |
Sounds good. As a quick related note, currently on master, in |
NSJSONSerialization isValidJSONObject Implementation
The spec for the function says that any |
The more tests we have the better even if they are slightly redundant; if anything it ensures consistency with little cost since these tests are rather short in execution time. |
Add Windows Support to SKSupport
Added implementation of
isValidJSONObject
.Testing is currently difficult since
NSArray
,NSString,
andNSDictionary
are not yet fully implemented, so I tested this by writing tests using the existing Foundation classes in another project. I added these tests here and commented them out since they cannot yet be used and added aTODO:
to enable the tests once ready.Let me know what you think!