-
Notifications
You must be signed in to change notification settings - Fork 29
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
Initialisation using nil
or NSNull
#22
Comments
Makes sense! Short test case: XCTAssertEqual(try JSON(Optional<Int>.none as Any), .null)
XCTAssertEqual(try JSON(NSNull()), .null) The proposed |
This works for me: --- a/GenericJSON/Initialization.swift
+++ b/GenericJSON/Initialization.swift
@@ -9,6 +9,10 @@ extension JSON {
/// of those types.
public init(_ value: Any) throws {
switch value {
+ case _ as NSNull:
+ self = .null
+ case let opt as Optional<Any> where opt == nil:
+ self = .null
case let num as Float:
self = .number(num)
case let num as Int: I have to run now, will write some tests to check for corner cases later, the relationship between |
I’ve pushed my working version, can you please try that? |
I have added a further comment to the commit regarding the unit tests, but it is more a question of interest. I would consider this issue resolved. 😄 |
🎉 |
Currently, initialisation from an
Any
type does not allownil
orNSNull
values (as far as I can tell). Would it make sense to support this?There are two use cases I have run across:
nil
:NSNull
. For example deserialising a json objectJSONSerialization.jsonObject()
.Potential solution (wip)
For
NSNull
initialisation, the following could work:The text was updated successfully, but these errors were encountered: