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)) {
|
if (value == null || value.equals(null)) {
|
||||||
writer.write("null");
|
writer.write("null");
|
||||||
} else if (value instanceof JSONString) {
|
} else if (value instanceof JSONString) {
|
||||||
// JSONString must be checked first, so it can overwrite behaviour of other types below
|
// may throw an exception
|
||||||
Object o;
|
processJsonStringToWriteValue(writer, value);
|
||||||
try {
|
|
||||||
o = ((JSONString) value).toJSONString();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException(e);
|
|
||||||
}
|
|
||||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
|
||||||
} else if (value instanceof String) {
|
} else if (value instanceof String) {
|
||||||
// assuming most values are Strings, so testing it early
|
// assuming most values are Strings, so testing it early
|
||||||
quote(value.toString(), writer);
|
quote(value.toString(), writer);
|
||||||
return writer;
|
return writer;
|
||||||
} else if (value instanceof Number) {
|
} else if (value instanceof Number) {
|
||||||
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
// may throw an exception
|
||||||
final String numberAsString = numberToString((Number) value);
|
processNumberToWriteValue(writer, (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);
|
|
||||||
}
|
|
||||||
} else if (value instanceof Boolean) {
|
} else if (value instanceof Boolean) {
|
||||||
writer.write(value.toString());
|
writer.write(value.toString());
|
||||||
} else if (value instanceof Enum<?>) {
|
} else if (value instanceof Enum<?>) {
|
||||||
@ -2970,6 +2957,41 @@ public class JSONObject {
|
|||||||
return writer;
|
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 {
|
static final void indent(Writer writer, int indent) throws IOException {
|
||||||
for (int i = 0; i < indent; i += 1) {
|
for (int i = 0; i < indent; i += 1) {
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
@ -3037,11 +3059,7 @@ public class JSONObject {
|
|||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
writer.write(' ');
|
writer.write(' ');
|
||||||
}
|
}
|
||||||
try {
|
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
|
||||||
writeValue(writer, entry.getValue(), indentFactor, newIndent);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
|
|
||||||
}
|
|
||||||
needsComma = true;
|
needsComma = true;
|
||||||
}
|
}
|
||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user