Skip to content

Commit 69c87dc

Browse files
committed
more sonarcube optimization in jsonobject.java
1 parent 53cfa74 commit 69c87dc

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,28 +2926,15 @@ static final Writer writeValue(Writer writer, Object value,
29262926
if (value == null || value.equals(null)) {
29272927
writer.write("null");
29282928
} else if (value instanceof JSONString) {
2929-
// JSONString must be checked first, so it can overwrite behaviour of other types below
2930-
Object o;
2931-
try {
2932-
o = ((JSONString) value).toJSONString();
2933-
} catch (Exception e) {
2934-
throw new JSONException(e);
2935-
}
2936-
writer.write(o != null ? o.toString() : quote(value.toString()));
2929+
// may throw an exception
2930+
processJsonStringToWriteValue(writer, value);
29372931
} else if (value instanceof String) {
29382932
// assuming most values are Strings, so testing it early
29392933
quote(value.toString(), writer);
29402934
return writer;
29412935
} else if (value instanceof Number) {
2942-
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
2943-
final String numberAsString = numberToString((Number) value);
2944-
if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
2945-
writer.write(numberAsString);
2946-
} else {
2947-
// The Number value is not a valid JSON number.
2948-
// Instead we will quote it as a string
2949-
quote(numberAsString, writer);
2950-
}
2936+
// may throw an exception
2937+
processNumberToWriteValue(writer, (Number) value);
29512938
} else if (value instanceof Boolean) {
29522939
writer.write(value.toString());
29532940
} else if (value instanceof Enum<?>) {
@@ -2970,6 +2957,41 @@ static final Writer writeValue(Writer writer, Object value,
29702957
return writer;
29712958
}
29722959

2960+
/**
2961+
* Convenience function to reduce cog complexity of calling method; writes value if string is valid
2962+
* @param writer Object doing the writing
2963+
* @param value Value to be written
2964+
* @throws IOException if something goes wrong
2965+
*/
2966+
private static void processJsonStringToWriteValue(Writer writer, Object value) throws IOException {
2967+
// JSONString must be checked first, so it can overwrite behaviour of other types below
2968+
Object o;
2969+
try {
2970+
o = ((JSONString) value).toJSONString();
2971+
} catch (Exception e) {
2972+
throw new JSONException(e);
2973+
}
2974+
writer.write(o != null ? o.toString() : quote(value.toString()));
2975+
}
2976+
2977+
/**
2978+
* Convenience function to reduce cog complexity of calling method; writes value if number is valid
2979+
* @param writer Object doing the writing
2980+
* @param value Value to be written
2981+
* @throws IOException if something goes wrong
2982+
*/
2983+
private static void processNumberToWriteValue(Writer writer, Number value) throws IOException {
2984+
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
2985+
final String numberAsString = numberToString(value);
2986+
if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
2987+
writer.write(numberAsString);
2988+
} else {
2989+
// The Number value is not a valid JSON number.
2990+
// Instead we will quote it as a string
2991+
quote(numberAsString, writer);
2992+
}
2993+
}
2994+
29732995
static final void indent(Writer writer, int indent) throws IOException {
29742996
for (int i = 0; i < indent; i += 1) {
29752997
writer.write(' ');
@@ -3037,11 +3059,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
30373059
if (indentFactor > 0) {
30383060
writer.write(' ');
30393061
}
3040-
try {
3041-
writeValue(writer, entry.getValue(), indentFactor, newIndent);
3042-
} catch (Exception e) {
3043-
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
3044-
}
3062+
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
30453063
needsComma = true;
30463064
}
30473065
if (indentFactor > 0) {

0 commit comments

Comments
 (0)