-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Update enum support to be more fully featured. #271
Conversation
…e Enums. The new way enums are handled is to always place the actual enum in the JSONObject/JSONArray. When writing, we always write the actual "name" of the enum, so even with a toString override on the enum class, the value remains consistant and compatible with the optEnum/getEnum methods. The constructor JSONObject(Object) functions the same way as before when passing an enum and is consistent with other "value" types. For example, when creating a JSONObject with Long, Boolean, BigDecimal as the constructor parameter, the value will be treated as a "bean".
done. |
Is there a reasonable way to implement this such that existing users are unaffected, but new users can choose the new behavior? |
I don't think so since it was inconsistent. Sometimes values would be On Aug 12, 2016 01:49, "Sean Leary" [email protected] wrote:
|
Enum support is connected in an odd way to BigNumber: both are Java features that did not exist when the lib was first implemented. At the times we added support for both, Douglas gave us pretty clear guidance on what we should and should not do, when implementing support for new features. That is, it needs to be exactly correct, and it can't break existing code. Of course, there will be a little give and take between those two constraints. I am still trying to get a sense of whether this enhancement is within the gray area ("Some rules can be bent; some can be broken" - Morpheus), or if we are straying too far into the realm of breaking existing code. I am going to be working all weekend - chores and job. Anything you can do to nail down precisely what is going to change, plus any discussion you want to throw in, would be humbly appreciated. |
I'd say that since the current implementation is inconsistent that this is On Aug 13, 2016 02:45, "Sean Leary" [email protected] wrote:
|
Also to note, this library did have support for enums a while ago, but On Aug 13, 2016 09:09, "John Aylward" [email protected] wrote:
|
Good information, thanks. Would that be the change and revert to JSONObject.wrap() in 2012? Or was there more? |
Yes, just those 2 commits as far as I know. One in July adding support, On Aug 13, 2016 12:35, "Sean Leary" [email protected] wrote:
|
Taking small steps...
Let me know what you think. |
That sounds good. The changes to valueToString are indeed needed to prevent For the number changes, I'll revert those here and place them in a separate On Aug 15, 2016 02:01, "Sean Leary" [email protected] wrote:
|
ok, cleanup up the commit as well as merging in the latest changes you made for the comments. Also updated my notes in comment#1 |
Thanks, starting a 3 day comment window. |
Revert Enum changes introduced in upstream pull request stleary#271 and revert the Enum unit tests to how they were around PR stleary#127 on 2015-07-08. This reintroduces the original JSON-java Enum handling that did not have explicit handling for Enums and handled them inconsistently between JSONObject and JSONArray, and also relied on Enum.toString() when wrapping them instead of storing them as value and serializing them to their Enum.name() value.
Adds support to JSONObject wrap and write methods to explicitly handle Enums.
What problem does this code solve?
Enums were handled inconsistently as noted in the original improvement (#140 and #127) and brought up again in #145 and #255
The new way enums are handled is to always place the actual enum in the JSONObject/JSONArray (not wrapped, similar to other value types). When writing, we always write the actual "name" of the enum, so even with a toString override on the enum class, the value remains consistent and compatible with the optEnum/getEnum methods.
The constructor JSONObject(Object) functions the same way as before when passing an enum and is consistent with other "value" types. For example, when creating a JSONObject with Long, Boolean, BigDecimal as the constructor parameter, the value will be treated as a "bean".
Risks
Low risk. Currently inserting enums into JSONObject and JSONArray are not consistent. Anyone depending on the existing handling of enums probably does so with if blocks to ensure they have the correct data. Additionally, some cases of inserting an enum would produce results that could not be read back using get|optEnum methods.
Given that the data is not always reversible, it's unlikely that people are depending on this behaviour.
Changes to the API?
No, but there are functional changes
Will this require a new release?
Can be rolled into the next release, pending comments.
Should the documentation be updated?
Not required.
Does it break the unit tests?
Yes, new unit tests have been added.
Review status
ACCEPTED. Starting 3-day comment window 15 Aug 2016