Skip to content

Commit 4e0f62b

Browse files
committed
more sonarcube optimization in jsonobject.java
1 parent 9b8eefc commit 4e0f62b

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
13901390
if (!numberIsFinite((Number)val)) {
13911391
return defaultValue;
13921392
}
1393-
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
1393+
return BigDecimal.valueOf(((Number) val).doubleValue()).toBigInteger();
13941394
}
13951395
if (val instanceof Long || val instanceof Integer
13961396
|| val instanceof Short || val instanceof Byte){
@@ -2041,7 +2041,7 @@ private static int getAnnotationDepth(final Method m, final Class<? extends Anno
20412041
return 1;
20422042
}
20432043

2044-
// if we've already reached the Object class, return -1;
2044+
// since we've already reached the Object class, return -1;
20452045
Class<?> c = m.getDeclaringClass();
20462046
if (c.getSuperclass() == null) {
20472047
return -1;
@@ -2057,9 +2057,9 @@ private static int getAnnotationDepth(final Method m, final Class<? extends Anno
20572057
return d + 1;
20582058
}
20592059
} catch (final SecurityException ex) {
2060-
continue;
2060+
// Nothing to do here
20612061
} catch (final NoSuchMethodException ex) {
2062-
continue;
2062+
// Nothing to do here
20632063
}
20642064
}
20652065

@@ -2427,21 +2427,32 @@ public static Writer quote(String string, Writer w) throws IOException {
24272427
w.write("\\r");
24282428
break;
24292429
default:
2430-
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
2431-
|| (c >= '\u2000' && c < '\u2100')) {
2432-
w.write("\\u");
2433-
hhhh = Integer.toHexString(c);
2434-
w.write("0000", 0, 4 - hhhh.length());
2435-
w.write(hhhh);
2436-
} else {
2437-
w.write(c);
2438-
}
2430+
writeAsHex(w, c);
24392431
}
24402432
}
24412433
w.write('"');
24422434
return w;
24432435
}
24442436

2437+
/**
2438+
* Convenience method to reduce cognitive complexity of quote()
2439+
* @param w The Writer to which the quoted string will be appended.
2440+
* @param c Character to write
2441+
* @throws IOException
2442+
*/
2443+
private static void writeAsHex(Writer w, char c) throws IOException {
2444+
String hhhh;
2445+
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
2446+
|| (c >= '\u2000' && c < '\u2100')) {
2447+
w.write("\\u");
2448+
hhhh = Integer.toHexString(c);
2449+
w.write("0000", 0, 4 - hhhh.length());
2450+
w.write(hhhh);
2451+
} else {
2452+
w.write(c);
2453+
}
2454+
}
2455+
24452456
/**
24462457
* Remove a name and its value, if present.
24472458
*
@@ -2470,42 +2481,46 @@ public boolean similar(Object other) {
24702481
if (!this.keySet().equals(((JSONObject)other).keySet())) {
24712482
return false;
24722483
}
2473-
for (final Entry<String,?> entry : this.entrySet()) {
2474-
String name = entry.getKey();
2475-
Object valueThis = entry.getValue();
2476-
Object valueOther = ((JSONObject)other).get(name);
2477-
if(valueThis == valueOther) {
2478-
continue;
2479-
}
2480-
if(valueThis == null) {
2481-
return false;
2482-
}
2483-
if (valueThis instanceof JSONObject) {
2484-
if (!((JSONObject)valueThis).similar(valueOther)) {
2485-
return false;
2486-
}
2487-
} else if (valueThis instanceof JSONArray) {
2488-
if (!((JSONArray)valueThis).similar(valueOther)) {
2489-
return false;
2490-
}
2491-
} else if (valueThis instanceof Number && valueOther instanceof Number) {
2492-
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
2493-
return false;
2494-
}
2495-
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
2496-
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
2497-
return false;
2498-
}
2499-
} else if (!valueThis.equals(valueOther)) {
2500-
return false;
2501-
}
2502-
}
2503-
return true;
2484+
return checkSimilarEntries(other);
25042485
} catch (Throwable exception) {
25052486
return false;
25062487
}
25072488
}
25082489

2490+
private boolean checkSimilarEntries(Object other) {
2491+
for (final Entry<String,?> entry : this.entrySet()) {
2492+
String name = entry.getKey();
2493+
Object valueThis = entry.getValue();
2494+
Object valueOther = ((JSONObject)other).get(name);
2495+
if(valueThis == valueOther) {
2496+
continue;
2497+
}
2498+
if(valueThis == null) {
2499+
return false;
2500+
}
2501+
2502+
if (!checkThis(valueThis, valueOther)) {
2503+
return false;
2504+
}
2505+
}
2506+
return true;
2507+
}
2508+
2509+
private boolean checkThis(Object valueThis, Object valueOther) {
2510+
if (valueThis instanceof JSONObject) {
2511+
return ((JSONObject)valueThis).similar(valueOther);
2512+
} else if (valueThis instanceof JSONArray) {
2513+
return ((JSONArray)valueThis).similar(valueOther);
2514+
} else if (valueThis instanceof Number && valueOther instanceof Number) {
2515+
return isNumberSimilar((Number)valueThis, (Number)valueOther);
2516+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
2517+
return ((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString());
2518+
} else if (!valueThis.equals(valueOther)) {
2519+
return false;
2520+
}
2521+
return true;
2522+
}
2523+
25092524
/**
25102525
* Compares two numbers to see if they are similar.
25112526
*

0 commit comments

Comments
 (0)