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
53cfa742a7
commit
69c87dc4db
@ -2926,28 +2926,15 @@ public class JSONObject {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if (value instanceof JSONString) {
|
||||
// JSONString must be checked first, so it can overwrite behaviour of other types below
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
// may throw an exception
|
||||
processJsonStringToWriteValue(writer, value);
|
||||
} else if (value instanceof String) {
|
||||
// assuming most values are Strings, so testing it early
|
||||
quote(value.toString(), writer);
|
||||
return writer;
|
||||
} else if (value instanceof Number) {
|
||||
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
||||
final String numberAsString = numberToString((Number) value);
|
||||
if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
|
||||
writer.write(numberAsString);
|
||||
} else {
|
||||
// The Number value is not a valid JSON number.
|
||||
// Instead we will quote it as a string
|
||||
quote(numberAsString, writer);
|
||||
}
|
||||
// may throw an exception
|
||||
processNumberToWriteValue(writer, (Number) value);
|
||||
} else if (value instanceof Boolean) {
|
||||
writer.write(value.toString());
|
||||
} else if (value instanceof Enum<?>) {
|
||||
@ -2970,6 +2957,41 @@ public class JSONObject {
|
||||
return writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to reduce cog complexity of calling method; writes value if string is valid
|
||||
* @param writer Object doing the writing
|
||||
* @param value Value to be written
|
||||
* @throws IOException if something goes wrong
|
||||
*/
|
||||
private static void processJsonStringToWriteValue(Writer writer, Object value) throws IOException {
|
||||
// JSONString must be checked first, so it can overwrite behaviour of other types below
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to reduce cog complexity of calling method; writes value if number is valid
|
||||
* @param writer Object doing the writing
|
||||
* @param value Value to be written
|
||||
* @throws IOException if something goes wrong
|
||||
*/
|
||||
private static void processNumberToWriteValue(Writer writer, Number value) throws IOException {
|
||||
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
||||
final String numberAsString = numberToString(value);
|
||||
if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
|
||||
writer.write(numberAsString);
|
||||
} else {
|
||||
// The Number value is not a valid JSON number.
|
||||
// Instead we will quote it as a string
|
||||
quote(numberAsString, writer);
|
||||
}
|
||||
}
|
||||
|
||||
static final void indent(Writer writer, int indent) throws IOException {
|
||||
for (int i = 0; i < indent; i += 1) {
|
||||
writer.write(' ');
|
||||
@ -3037,11 +3059,7 @@ public class JSONObject {
|
||||
if (indentFactor > 0) {
|
||||
writer.write(' ');
|
||||
}
|
||||
try {
|
||||
writeValue(writer, entry.getValue(), indentFactor, newIndent);
|
||||
} catch (Exception e) {
|
||||
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
|
||||
}
|
||||
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
|
||||
needsComma = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user