-
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
optLong vs getLong inconsitencies #653
Comments
Thanks, @ejoncas-rivalbet for bringing up this issue, investigating. |
@ejoncas-rivalbet , just to be sure I understand you correctly, you believe that To be honest, I'm a little surprised that your first test passes. I would have expected the parser to process that as octal, which would be |
hmm, I'm reading the javadocs on the parsers, and I'm not seeing a reference to the "leading 0 means octal" as noted in our comments. From what I can tell, all the string -> number parsers seem to process as decimal unless using the overload with the radix. @stleary , if we don't have it already, we should include tests which process various number formats (hex, scientific/exponent, floating point hex, octal, standard decimal) and make sure we have coverage. |
The JSON spec does not allow leading zeros on a number: so I'm going with |
@johnjaylward But since it does it, I believe getLong does it well where as The JSON spect does not allow leading zeros on numbers but given this is a string everything is possible. Fixing optLong to behave the same way as getLong is a small change that will simply fix anyone running into this issue. If you change getLong to return On the project where I run onto this issue. I simply came back to use |
I think there are reasonable arguments for both sides. I don't think either JSON spec covers accessor functions, so this implementation is free to choose how to support get and opt functions. |
This issue is available for someone to work on. Whatever solution is selected, |
@stleary I would like to provide the fix. Was there any reason to change from calling |
Rereading the discussion here, I'm leaning towards modifying the It also looks like Possible values to use for new test cases:
|
also to note, for android compatibility, the code changes for "stringToNumber" will need to be copied to the |
@ejoncas-rivalbet Is this still a problem area for you? If not, I think it might be difficult to find a fix that doesn't break existing applications (see #661, which was eventually closed). |
I have identified the cause and fix for this. Can this issue be marked as Hacktoberfest please. |
Done. |
For exponential decimal conversion, number is not touched. Leading zeros removed from numeric number strings before converting to number.
PR 783 raised. Please review. |
@johnjaylward is this comment still relevant and should it be included in the solution? |
Fixed with #783 |
Yes, it should be copied over |
Hi there,
We found a problem today where we stopped using
getLong(String)
and started usingoptLong(String)
as it doesn't throw exception.The problem is that there are inconsistencies between the two methods in regards on how they treat "numbers as strings". I know this is not the intended case but our mobile-app was sending strings when we expected numbers and everything was working well with
getLong
and suddenly some strange scenarios broke.Here's is a sample scenario:
As you can see optLong and getLong almost behave the same for numbers as strings but the problem arises when the number starts with
0
.The root of this problem is on how getLong parse numbers:
https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/JSONObject.java#L807
(it uses instanceof and
Number#longValue()
) where asoptLong
ends up calling this functionJSON-java/src/main/java/org/json/JSONObject.java
Line 2218 in f1b0210
The text was updated successfully, but these errors were encountered: