-
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
changes number parsing to use BigDecimal as the backing type #453
Conversation
0cf737b
to
3244f30
Compare
Breaks a lot of unit tests. Aren't users likely to see the same breakages? |
Yes, I'm not sure how impactful that would be though. The APIs that are affected are the Object get/opt and not the typed ones (i.e. getString and getDouble still work the same way). The only reason the raw ones are affected is because the type changed, not the value. A programmer calling the Object get/opt methods would hopefully not be depending on a certain type like we do in the test cases. |
Is there any way to restrict this so that just values that are added as BigDecimal get the new behavior? |
Values added as big decimal are already handled as big decimal. This is strictly to change how the parsing Of JSON text works, not how |
3244f30
to
429a2ef
Compare
The behavior for this conversion has changed from 20180813 to 20190722.
Output for 20180813: Output for 20190722: Instead of chaging this behavior by default, would it be possible to choose a method for parsing that is suitable to the application? |
@PeerHeijnen, the main issue here is that you do the following steps:
The conversion from This PR is meant to fix the round tripping that causes the issue you are seeing. Currently in Another option would be to add another getter with a BigDecimal bd = JSONObject.getBigDecimal("test", 4);
System.out.println(bd); // would print 348.8000 abbreviated to 348.8 Using today's API, this would look like: BigDecimal bd = JSONObject.getBigDecimal("test").setScale(4, RoundingMode.HALF_EVEN);
System.out.println(bd); // would print 348.8000 abbreviated to 348.8 As a side note, it's usually recommended to set a scale on all your BigDecimal uses to prevent issues like this popping up. |
Sure, it was not ment as criticism. I just noted the change in behavior, be it intentional or not. The code I shared was not actual production code, just a short version of how the conversion happens as you explained. I was just wondering whether people may experience side effect from the PR. For me, it solves my problems I have now perfectly, so thanks for the effort! :) |
429a2ef
to
287e40b
Compare
rebased to support the new project structure |
287e40b
to
e46ddd0
Compare
* updated tests to support BigDecimal as the backing type for numbers * updated some test resource handling to java7 try-with-resources format * cleaned up some other minor compiler warnings
e46ddd0
to
56d33b8
Compare
Starting 3 day comment window |
@TheMatthew Do you still have the benchmark tests you were running for the other improvements we made? I was wondering if you could spin them up for this change... I should have probably thought of doing that in the last 1.5 years this has been open, but it just occurred to me now |
What problem does this code solve?
See issue #441 (use BigDecimal for underlying storage of decimal number types)
Risks
Medium
Changes to the API?
No, but it changes how certain API calls work for decimal/floating-point types work.
for example a JSON document list this:
would parse like this:
In general, the raw "get/opt" methods with no type information (like getDouble, getInt, optDouble, etc) are not guaranteed to be useful as the backing types were never guaranteed. However, people may be using them without the knowledge that the backing types are semi-dynamic (i.e. number types have always been backed differently if an
Integer
was parsed vs afloat
). So even a "standard" document like this:Would have a
Double
stored for the firstfloatValue
and anInteger
for the secondfloatValue
before the change. After the change they would be stored asBigDecimal
andInteger
respectively.Will this require a new release?
Yes.
Should the documentation be updated?
Yes. we should probably mention that the backing type has changed and how that may affect existing code.
Does it break the unit tests?
Yes, many of the tests depend on the data type stored for numbers. The tests have been updated to reflect the new backing type.
Was any code refactored in this commit?
Yes, changes were made to the number parsing methods for XML and JSONObject (they need to be kept in sync for Android support)
Review status
APPROVED