-
Notifications
You must be signed in to change notification settings - Fork 63
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
Added option to encode null values #26
base: master
Are you sure you want to change the base?
Conversation
Thanks for the PR! It looks like you thought through this. I'm just concerned this would be make it ambiguous which variant of
I reckon we can best solve this problem is by eventually supporting a partially specialised |
I should say the redundant method was because the compiler couldn't find the generic version when we use the default implementation that generates This might be fixed the latest version of Swift but I've yet to test it. |
…nd JSONDictionary to be consistent with the visibility of these types This also allows for clients (that include the code not the framework) to use the JSONArray and JSONDictionary helper extensions
This should probably be the default...
You're right about having two toJSON() methods. I don't like it either, the only reason I maintained the initial one was for backward compatibility. Your proposal works as well, and is easy enough to implement. My main issues with it is that it spreads a "setting" throughout the code. For cases where the developer always wants to encode nulls, having to pass that extra parameter in every single encode call will seem like an unnecessary burden. For cases where he actually wants to choose wether to encode nulls or not depending on the context, he must add logic to his model to change that setting in all encode calls. I also noticed that empty collections are not serialized, which should also be an option. So here's what I'm thinking... I'll make toJSON() receive an OptionSet with both settings (encode nulls, and encode empty collection) and make it the single method defined in JSONEncodable. This has the advantage of easily allowing future options (say pretty printing for example) and avoids any ambiguity, but would be a backward incompatible change. |
Actually, the more I think about it, the more I feel like not encoding empty collections is actually a bug, and it should not even be an option. It's like not encoding an empty string. |
… received a set of options for encoding - JSONEncoder.create method also has a mandatory options parameter - Empty collections are now always encoded
Thanks for thinking about this more. I see your point. My final suggestion is to leave the
Please also squash your commits so it's its easier to follow the changes. Thanks! |
Conflicts: JSONCodableTests/RegularTests.swift JSONCodableTests/User.swift
There are some situations where not encoding a property, or encoding it with a null value will result in different behaviours.
So I've added the ability to do this while trying to keep backward compatibility.