mirror of
https://github.com/stleary/JSON-java.git
synced 2025-10-01 14:25:55 -04:00
more sonarcube optimization in jsonobject.java
This commit is contained in:
parent
9b8eefc2de
commit
4e0f62b1a6
@ -1390,7 +1390,7 @@ public class JSONObject {
|
|||||||
if (!numberIsFinite((Number)val)) {
|
if (!numberIsFinite((Number)val)) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
|
return BigDecimal.valueOf(((Number) val).doubleValue()).toBigInteger();
|
||||||
}
|
}
|
||||||
if (val instanceof Long || val instanceof Integer
|
if (val instanceof Long || val instanceof Integer
|
||||||
|| val instanceof Short || val instanceof Byte){
|
|| val instanceof Short || val instanceof Byte){
|
||||||
@ -2041,7 +2041,7 @@ public class JSONObject {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we've already reached the Object class, return -1;
|
// since we've already reached the Object class, return -1;
|
||||||
Class<?> c = m.getDeclaringClass();
|
Class<?> c = m.getDeclaringClass();
|
||||||
if (c.getSuperclass() == null) {
|
if (c.getSuperclass() == null) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -2057,9 +2057,9 @@ public class JSONObject {
|
|||||||
return d + 1;
|
return d + 1;
|
||||||
}
|
}
|
||||||
} catch (final SecurityException ex) {
|
} catch (final SecurityException ex) {
|
||||||
continue;
|
// Nothing to do here
|
||||||
} catch (final NoSuchMethodException ex) {
|
} catch (final NoSuchMethodException ex) {
|
||||||
continue;
|
// Nothing to do here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2427,6 +2427,21 @@ public class JSONObject {
|
|||||||
w.write("\\r");
|
w.write("\\r");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
writeAsHex(w, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.write('"');
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience method to reduce cognitive complexity of quote()
|
||||||
|
* @param w The Writer to which the quoted string will be appended.
|
||||||
|
* @param c Character to write
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private static void writeAsHex(Writer w, char c) throws IOException {
|
||||||
|
String hhhh;
|
||||||
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|
if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
|
||||||
|| (c >= '\u2000' && c < '\u2100')) {
|
|| (c >= '\u2000' && c < '\u2100')) {
|
||||||
w.write("\\u");
|
w.write("\\u");
|
||||||
@ -2437,10 +2452,6 @@ public class JSONObject {
|
|||||||
w.write(c);
|
w.write(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
w.write('"');
|
|
||||||
return w;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a name and its value, if present.
|
* Remove a name and its value, if present.
|
||||||
@ -2470,6 +2481,13 @@ public class JSONObject {
|
|||||||
if (!this.keySet().equals(((JSONObject)other).keySet())) {
|
if (!this.keySet().equals(((JSONObject)other).keySet())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return checkSimilarEntries(other);
|
||||||
|
} catch (Throwable exception) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkSimilarEntries(Object other) {
|
||||||
for (final Entry<String,?> entry : this.entrySet()) {
|
for (final Entry<String,?> entry : this.entrySet()) {
|
||||||
String name = entry.getKey();
|
String name = entry.getKey();
|
||||||
Object valueThis = entry.getValue();
|
Object valueThis = entry.getValue();
|
||||||
@ -2480,30 +2498,27 @@ public class JSONObject {
|
|||||||
if(valueThis == null) {
|
if(valueThis == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (valueThis instanceof JSONObject) {
|
|
||||||
if (!((JSONObject)valueThis).similar(valueOther)) {
|
if (!checkThis(valueThis, valueOther)) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (valueThis instanceof JSONArray) {
|
|
||||||
if (!((JSONArray)valueThis).similar(valueOther)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (valueThis instanceof Number && valueOther instanceof Number) {
|
|
||||||
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
|
|
||||||
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (!valueThis.equals(valueOther)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (Throwable exception) {
|
}
|
||||||
|
|
||||||
|
private boolean checkThis(Object valueThis, Object valueOther) {
|
||||||
|
if (valueThis instanceof JSONObject) {
|
||||||
|
return ((JSONObject)valueThis).similar(valueOther);
|
||||||
|
} else if (valueThis instanceof JSONArray) {
|
||||||
|
return ((JSONArray)valueThis).similar(valueOther);
|
||||||
|
} else if (valueThis instanceof Number && valueOther instanceof Number) {
|
||||||
|
return isNumberSimilar((Number)valueThis, (Number)valueOther);
|
||||||
|
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
|
||||||
|
return ((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString());
|
||||||
|
} else if (!valueThis.equals(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user