Skip to content

Conversation

@nayak-yash
Copy link

What does this PR do ?

This PR fixes the optLong() - getLong() and optDouble() and getDouble() inconsistencies
Fixes #653

Type of change

Previously stringToNumber() checked for strings starting with either digit or - . Now it only checks for whether the string is a decimal number or integer.

Passed all test cases

Other information

JSONObject object1 = new JSONObject("{\"number\":\"-.1234\"}");
JSONObject object2 = new JSONObject("{\"number\":\"-0000000.32879\"}");
JSONObject object3 = new JSONObject("{\"number\":\"-07.89\"}");
JSONObject object4 = new JSONObject("{\"number\":\"-0.00000000\"}");
System.out.println("1. getDouble() = " + object1.getDouble("number"));
System.out.println("1. optDouble() = " + object1.optDouble("number"));
System.out.println("2. getDouble() = " + object2.getDouble("number"));
System.out.println("2. optDouble() = " + object2.optDouble("number"));
System.out.println("3. getDouble() = " + object3.getDouble("number"));
System.out.println("3. optDouble() = " + object3.optDouble("number"));
System.out.println("4. getDouble() = " + object4.getDouble("number"));
System.out.println("4. optDouble() = " + object4.optDouble("number"));

Output :

1. getDouble() = -0.1234
1. optDouble() = -0.1234
2. getDouble() = -0.32879
2. optDouble() = -0.32879
3. getDouble() = -7.89
3. optDouble() = -7.89
4. getDouble() = -0.0
4. optDouble() = -0.0

@stleary
Copy link
Owner

stleary commented Oct 7, 2023

@nayak-yash Thanks for submitting this PR, but #783 has already been accepted to fix this bug. Your fix also successfully addressed the bug, however, the other solution was preferred because the code changes were directly related to the bug fix, and numerous unit tests were added. Please feel free to pick another issue and work on that.

@stleary stleary closed this Oct 7, 2023
@nayak-yash nayak-yash deleted the fix-opt-get-inconsistency branch October 9, 2023 11:49
@nayak-yash nayak-yash restored the fix-opt-get-inconsistency branch October 9, 2023 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optLong vs getLong inconsitencies

2 participants