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

Optional setter #475

Open
dral3x opened this issue Mar 15, 2016 · 6 comments
Open

Optional setter #475

dral3x opened this issue Mar 15, 2016 · 6 comments

Comments

@dral3x
Copy link

dral3x commented Mar 15, 2016

Hi,

In our code we will generate a json representation of some entities. We would like to have setters that handle optional to NOT output "null" values but simply ignore the key-value all together.

For example, by doing

var json = JSON([String: JSON]())
json["test"] = nil

the result should be {} and not {"test":null}

Is there something already built-in for this?

@ldiqual
Copy link
Contributor

ldiqual commented Nov 17, 2016

There hasn't been much activity on this issue for a while so I'm gonna go ahead and consider it fixed. Please let us know it you'd like to reopen it.

@ldiqual ldiqual closed this as completed Nov 17, 2016
@dral3x
Copy link
Author

dral3x commented Nov 17, 2016

@ldiqual As far as I know, it's not fixed. I did an extension to work around this limitation

extension JSON {

    // MARK: Setter with optional
    public mutating func optionalSetter(key: String, value: String?) {

        guard let value = value else {
            return
        }

        self[key].string = value
    }
...
}

Of course I did a function of each data type supported.

I can make a PR if you want to embrace this. Just let me know!

@hernangonzalez
Copy link

I'd like to see optional setters support as well. @dral3x could you send a PR to try and get this going?

@dral3x
Copy link
Author

dral3x commented Jan 27, 2017

Here it is the PR #784

@dral3x
Copy link
Author

dral3x commented Mar 28, 2018

More than 1 year has passed and this issue is still open.
Is there any problem in integrating the PR I made?

Thanks!

@hernangonzalez
Copy link

I guess we have all moved to Swift4 support by now :/

@mistralaix mistralaix mentioned this issue Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants