Skip to content

Commit d452169

Browse files
authored
Merge pull request stleary#831 from adityap27/refactor
Refactor NumberConversionUtil and toString() of CookieList & XML Classes.
2 parents 9299177 + aba82d9 commit d452169

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/main/java/org/json/CookieList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ public static JSONObject toJSONObject(String string) throws JSONException {
4646
* @throws JSONException if a called function fails
4747
*/
4848
public static String toString(JSONObject jo) throws JSONException {
49-
boolean b = false;
49+
boolean isEndOfPair = false;
5050
final StringBuilder sb = new StringBuilder();
5151
// Don't use the new entrySet API to maintain Android support
5252
for (final String key : jo.keySet()) {
5353
final Object value = jo.opt(key);
5454
if (!JSONObject.NULL.equals(value)) {
55-
if (b) {
55+
if (isEndOfPair) {
5656
sb.append(';');
5757
}
5858
sb.append(Cookie.escape(key));
5959
sb.append("=");
6060
sb.append(Cookie.escape(value.toString()));
61-
b = true;
61+
isEndOfPair = true;
6262
}
6363
}
6464
return sb.toString();

src/main/java/org/json/NumberConversionUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static Number stringToNumber(final String input) throws NumberFormatException {
2424
val = "-0."+val.substring(2);
2525
}
2626
char initial = val.charAt(0);
27-
if ((initial >= '0' && initial <= '9') || initial == '-' ) {
27+
if ( isNumericChar(initial) || initial == '-' ) {
2828
// decimal representation
2929
if (isDecimalNotation(val)) {
3030
// Use a BigDecimal all the time so we keep the original
@@ -53,13 +53,13 @@ static Number stringToNumber(final String input) throws NumberFormatException {
5353
initial = val.charAt(0);
5454
if(initial == '0' && val.length() > 1) {
5555
char at1 = val.charAt(1);
56-
if(at1 >= '0' && at1 <= '9') {
56+
if(isNumericChar(at1)) {
5757
throw new NumberFormatException("val ["+input+"] is not a valid number.");
5858
}
5959
} else if (initial == '-' && val.length() > 2) {
6060
char at1 = val.charAt(1);
6161
char at2 = val.charAt(2);
62-
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
62+
if(at1 == '0' && isNumericChar(at2)) {
6363
throw new NumberFormatException("val ["+input+"] is not a valid number.");
6464
}
6565
}
@@ -83,6 +83,16 @@ static Number stringToNumber(final String input) throws NumberFormatException {
8383
throw new NumberFormatException("val ["+input+"] is not a valid number.");
8484
}
8585

86+
/**
87+
* Checks if the character is a numeric digit ('0' to '9').
88+
*
89+
* @param c The character to be checked.
90+
* @return true if the character is a numeric digit, false otherwise.
91+
*/
92+
private static boolean isNumericChar(char c) {
93+
return (c <= '9' && c >= '0');
94+
}
95+
8696
/**
8797
* Checks if the value could be considered a number in decimal number system.
8898
* @param value

src/main/java/org/json/XML.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,14 @@ private static String toString(final Object object, final String tagName, final
847847

848848

849849
string = (object == null) ? "null" : escape(object.toString());
850-
850+
String indentationSuffix = (indentFactor > 0) ? "\n" : "";
851851
if(tagName == null){
852-
return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : "");
852+
return indent(indent) + "\"" + string + "\"" + indentationSuffix;
853853
} else if(string.length() == 0){
854-
return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : "");
854+
return indent(indent) + "<" + tagName + "/>" + indentationSuffix;
855855
} else {
856856
return indent(indent) + "<" + tagName
857-
+ ">" + string + "</" + tagName + ">" + ((indentFactor > 0) ? "\n" : "");
857+
+ ">" + string + "</" + tagName + ">" + indentationSuffix;
858858
}
859859
}
860860

0 commit comments

Comments
 (0)