Skip to content
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

Refactor duplicate code for stringToNumber() in JSONObject, JSONArray, and XML #814

Merged
merged 4 commits into from Oct 31, 2023

Conversation

rudrajyotib
Copy link
Contributor

@rudrajyotib rudrajyotib commented Oct 23, 2023

Both XML and JSON parser do convert string to number. Similar code has been duplicated for both parsers, which has been moved to a single utility method.

Fixes #813

import java.math.BigDecimal;
import java.math.BigInteger;

public class NumberConversionUtil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not make this public. We may wish to refactor this class in the future. For now make it private or default (package) visible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static methods meant to be used by functional classes being marked private do not add up. But yes, @stleary - unnecessary making these classes open for used by clients might not be a great idea, there are plenty of scope of improvements in there. So marked them default visibility.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. I simply meant that the class itself should be default or private, not the static methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I now recall that private outer classes is not a thing in Java. I was thinking inner class scopes. Default visibility is correct here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stleary @johnjaylward - can this PR be accepted please?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rudrajyotib Will get to it as soon as I can after the work of #811 is completed.

@rudrajyotib
Copy link
Contributor Author

For issue #813

@rudrajyotib
Copy link
Contributor Author

@stleary - can you accept this PR please?

@stleary
Copy link
Owner

stleary commented Oct 27, 2023

@rudrajyotib Please update the PR:

From https://github.com/rudrajyotib/JSON-java

  • branch issue813 -> FETCH_HEAD
    hint: You have divergent branches and need to specify how to reconcile them.
    hint: You can do so by running one of the following commands sometime before
    hint: your next pull:
    hint:
    hint: git config pull.rebase false # merge
    hint: git config pull.rebase true # rebase
    hint: git config pull.ff only # fast-forward only
    hint:
    hint: You can replace "git config" with "git config --global" to set a default
    hint: preference for all repositories. You can also pass --rebase, --no-rebase,
    hint: or --ff-only on the command line to override the configured default per
    hint: invocation.
    fatal: Need to specify how to reconcile divergent branches.

@rudrajyotib
Copy link
Contributor Author

@stleary - PR is now aligned with HEAD.

@rudrajyotib
Copy link
Contributor Author

@stleary - can you please see if this can be accepted, or is it still showing some discrepancy across git branches.

@stleary
Copy link
Owner

stleary commented Oct 29, 2023

What problem does this code solve?
Refactoring duplicate code

Does the code still compile with Java6?
Yes

Risks
Low

Changes to the API?
No

Will this require a new release?
No

Should the documentation be updated?
No

Does it break the unit tests?
No, new unit tests were added

Was any code refactored in this commit?
Yes, this was a refactoring ticket

Review status
APPROVED - Pending completion of code review

Start 3-day comment window

@@ -28,6 +28,9 @@
import java.util.Set;
import java.util.regex.Pattern;

import static org.json.NumberConversionUtil.potentialNumber;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just import NumberConversionUtil, not the methods. Someone glancing at the code might not realize it is a static method in another class.

|| val.indexOf('E') > -1 || "-0".equals(val);
}

private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a duplicate of this method in JSONObject. It should be removed, and the calling code updated to use the NumberConversionUtil method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the duplicate method will be removed in the next PR.

@@ -10,6 +10,9 @@
import java.math.BigInteger;
import java.util.Iterator;

import static org.json.NumberConversionUtil.potentialNumber;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just import NumberConversionUtil, not the methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next PR it would be changed.

@stleary
Copy link
Owner

stleary commented Oct 30, 2023

@rudrajyotib Looks good to me. I left some comments in the code. They are all trivial issues that can wait until this commit is merged. Please do not make any more changes to this PR; if you do, the accept status will be taken down and it won't get committed before the end of Hacktoberfest. I will open an issue to address the notes after Nov 1, but they are low priority.

@stleary stleary changed the title 813 - de-duplication of string to number logic. Refactor duplicate code for stringToNumber() in JSONObject, JSONArray, and XML. Oct 30, 2023
@stleary stleary changed the title Refactor duplicate code for stringToNumber() in JSONObject, JSONArray, and XML. Refactor duplicate code for stringToNumber() in JSONObject, JSONArray, and XML Oct 30, 2023
@rudrajyotib
Copy link
Contributor Author

@stleary sure thing. I will update them in another PR.

@stleary stleary merged commit 783d298 into stleary:master Oct 31, 2023
7 checks passed
@rudrajyotib rudrajyotib deleted the issue813 branch November 1, 2023 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Number parsing - de-duplication of logic
4 participants