-
Notifications
You must be signed in to change notification settings - Fork 43
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
Serialisation to JSON #51
Comments
Adding a way to convert the object to a dictionary representation is certainly valuable. We've discussed adding this at Pinterest for a while but haven't had a strong reason to do so just yet. We could certainly add it and I'd be happy to review a What do you think should happen if we have 'nil' properties? Store NSNull as values? What if a property hasn't been set yet? |
yes, I started to work on my own patch already:) I am currently facing this problem with nil values. However I am facing another issue, like when I receive object from REST which contains number, Plank's generated class store it to NSInteger. But NSInteger cannot be nil (if parameter was not received), so it is set to 0. But how can I check if this value is really 0, or it was not received at all. In some cases it makes difference, for example some update service. |
@matelo - Sounds great. We have a struct internally ("dirtyProperties") on each model that you can utilize for detecting if a field has been set. Below is an example of how we've utilized it. From
Couple things I'd like to see from the implementation:
Sound good? Looking forward to your patch and will help get this in. |
It would be nice to have a test that for all example schemas and for a bunch of arbitrary dictionaries per schema:
@rahul-malik we could do this nicely with SwiftCheck https://github.com/typelift/SwiftCheck |
@rahul-malik I totally agree with all the points, just have a question regarding the point
Do you have some hint how can I check if the value was/wasn't set, when its type is NSInteger? Because AFAIK, unset value of NSInteger is 0 - which also can be desired (set) value. |
@matelo - When you're checking if a property is set, don't use it's value but rather the This snippet below loops through all properties on an object and generates an You'll need to replace the "ADD STUFF TO DICTIONARY" portion with specific ways to convert each schema type into something that can be inserted into a dictionary.
You should be able to reference other files to get a sense of how this is accomplished but let me know if this is confusing. Thanks! |
can I create branch (or can you create branch) for this enhancement? I already have done almost all the types representation to NSDictionary. Thanks |
I would advise creating a fork of the repository and applying your changes on a branch there. Then you'll be able to create a pull request I can look at and provide feedback while you iterate. |
Is there possibility to serialise generated object to JSON? We can do initWithDictionary, but what about to have something like
dictionary
method, which will return dictionary to be send as JSON via REST?The text was updated successfully, but these errors were encountered: