Skip to content

Commit

Permalink
Merge pull request #831 from adityap27/refactor
Browse files Browse the repository at this point in the history
Refactor NumberConversionUtil and toString() of CookieList & XML Classes.
  • Loading branch information
stleary authored Dec 3, 2023
2 parents 9299177 + aba82d9 commit d452169
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/json/CookieList.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public static JSONObject toJSONObject(String string) throws JSONException {
* @throws JSONException if a called function fails
*/
public static String toString(JSONObject jo) throws JSONException {
boolean b = false;
boolean isEndOfPair = false;
final StringBuilder sb = new StringBuilder();
// Don't use the new entrySet API to maintain Android support
for (final String key : jo.keySet()) {
final Object value = jo.opt(key);
if (!JSONObject.NULL.equals(value)) {
if (b) {
if (isEndOfPair) {
sb.append(';');
}
sb.append(Cookie.escape(key));
sb.append("=");
sb.append(Cookie.escape(value.toString()));
b = true;
isEndOfPair = true;
}
}
return sb.toString();
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/json/NumberConversionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static Number stringToNumber(final String input) throws NumberFormatException {
val = "-0."+val.substring(2);
}
char initial = val.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-' ) {
if ( isNumericChar(initial) || initial == '-' ) {
// decimal representation
if (isDecimalNotation(val)) {
// Use a BigDecimal all the time so we keep the original
Expand Down Expand Up @@ -53,13 +53,13 @@ static Number stringToNumber(final String input) throws NumberFormatException {
initial = val.charAt(0);
if(initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
if(at1 >= '0' && at1 <= '9') {
if(isNumericChar(at1)) {
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
if(at1 == '0' && isNumericChar(at2)) {
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
}
Expand All @@ -83,6 +83,16 @@ static Number stringToNumber(final String input) throws NumberFormatException {
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}

/**
* Checks if the character is a numeric digit ('0' to '9').
*
* @param c The character to be checked.
* @return true if the character is a numeric digit, false otherwise.
*/
private static boolean isNumericChar(char c) {
return (c <= '9' && c >= '0');
}

/**
* Checks if the value could be considered a number in decimal number system.
* @param value
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/json/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -847,14 +847,14 @@ private static String toString(final Object object, final String tagName, final


string = (object == null) ? "null" : escape(object.toString());

String indentationSuffix = (indentFactor > 0) ? "\n" : "";
if(tagName == null){
return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : "");
return indent(indent) + "\"" + string + "\"" + indentationSuffix;
} else if(string.length() == 0){
return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : "");
return indent(indent) + "<" + tagName + "/>" + indentationSuffix;
} else {
return indent(indent) + "<" + tagName
+ ">" + string + "</" + tagName + ">" + ((indentFactor > 0) ? "\n" : "");
+ ">" + string + "</" + tagName + ">" + indentationSuffix;
}
}

Expand Down

0 comments on commit d452169

Please sign in to comment.