Skip to content

Commit aff59d0

Browse files
authored
Merge pull request stleary#1011 from marilynel/master
more sonarcube fixes
2 parents b258ea3 + f2af220 commit aff59d0

File tree

2 files changed

+105
-61
lines changed

2 files changed

+105
-61
lines changed

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

Lines changed: 103 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ private static int getAnnotationDepth(final Method m, final Class<? extends Anno
20412041
return 1;
20422042
}
20432043

2044-
// since we've already reached the Object class, return -1;
2044+
// we've already reached the Object class
20452045
Class<?> c = m.getDeclaringClass();
20462046
if (c.getSuperclass() == null) {
20472047
return -1;
@@ -2391,7 +2391,6 @@ public static Writer quote(String string, Writer w) throws IOException {
23912391

23922392
char b;
23932393
char c = 0;
2394-
String hhhh;
23952394
int i;
23962395
int len = string.length();
23972396

@@ -2482,7 +2481,7 @@ public boolean similar(Object other) {
24822481
return false;
24832482
}
24842483
return checkSimilarEntries(other);
2485-
} catch (Throwable exception) {
2484+
} catch (Exception e) {
24862485
return false;
24872486
}
24882487
}
@@ -2499,14 +2498,20 @@ private boolean checkSimilarEntries(Object other) {
24992498
return false;
25002499
}
25012500

2502-
if (!checkThis(valueThis, valueOther)) {
2501+
if (!checkObjectType(valueThis, valueOther)) {
25032502
return false;
25042503
}
25052504
}
25062505
return true;
25072506
}
25082507

2509-
private boolean checkThis(Object valueThis, Object valueOther) {
2508+
/**
2509+
* Convenience function. Compares types of two objects.
2510+
* @param valueThis Object whose type is being checked
2511+
* @param valueOther Reference object
2512+
* @return true if match, else false
2513+
*/
2514+
private boolean checkObjectType(Object valueThis, Object valueOther) {
25102515
if (valueThis instanceof JSONObject) {
25112516
return ((JSONObject)valueThis).similar(valueOther);
25122517
} else if (valueThis instanceof JSONArray) {
@@ -2619,6 +2624,7 @@ public static Object stringToValue(String string) {
26192624
try {
26202625
return stringToNumber(string);
26212626
} catch (Exception ignore) {
2627+
// Do nothing
26222628
}
26232629
}
26242630
return string;
@@ -2639,41 +2645,10 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
26392645
if ((initial >= '0' && initial <= '9') || initial == '-') {
26402646
// decimal representation
26412647
if (isDecimalNotation(val)) {
2642-
// Use a BigDecimal all the time so we keep the original
2643-
// representation. BigDecimal doesn't support -0.0, ensure we
2644-
// keep that by forcing a decimal.
2645-
try {
2646-
BigDecimal bd = new BigDecimal(val);
2647-
if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
2648-
return Double.valueOf(-0.0);
2649-
}
2650-
return bd;
2651-
} catch (NumberFormatException retryAsDouble) {
2652-
// this is to support "Hex Floats" like this: 0x1.0P-1074
2653-
try {
2654-
Double d = Double.valueOf(val);
2655-
if(d.isNaN() || d.isInfinite()) {
2656-
throw new NumberFormatException("val ["+val+"] is not a valid number.");
2657-
}
2658-
return d;
2659-
} catch (NumberFormatException ignore) {
2660-
throw new NumberFormatException("val ["+val+"] is not a valid number.");
2661-
}
2662-
}
2648+
return getNumber(val, initial);
26632649
}
26642650
// block items like 00 01 etc. Java number parsers treat these as Octal.
2665-
if(initial == '0' && val.length() > 1) {
2666-
char at1 = val.charAt(1);
2667-
if(at1 >= '0' && at1 <= '9') {
2668-
throw new NumberFormatException("val ["+val+"] is not a valid number.");
2669-
}
2670-
} else if (initial == '-' && val.length() > 2) {
2671-
char at1 = val.charAt(1);
2672-
char at2 = val.charAt(2);
2673-
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
2674-
throw new NumberFormatException("val ["+val+"] is not a valid number.");
2675-
}
2676-
}
2651+
checkForInvalidNumberFormat(val, initial);
26772652
// integer representation.
26782653
// This will narrow any values to the smallest reasonable Object representation
26792654
// (Integer, Long, or BigInteger)
@@ -2694,6 +2669,57 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
26942669
throw new NumberFormatException("val ["+val+"] is not a valid number.");
26952670
}
26962671

2672+
/**
2673+
* Convenience function. Block items like 00 01 etc. Java number parsers treat these as Octal.
2674+
* @param val value to convert
2675+
* @param initial first char of val
2676+
* @throws exceptions if numbers are formatted incorrectly
2677+
*/
2678+
private static void checkForInvalidNumberFormat(String val, char initial) {
2679+
if(initial == '0' && val.length() > 1) {
2680+
char at1 = val.charAt(1);
2681+
if(at1 >= '0' && at1 <= '9') {
2682+
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
2683+
}
2684+
} else if (initial == '-' && val.length() > 2) {
2685+
char at1 = val.charAt(1);
2686+
char at2 = val.charAt(2);
2687+
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
2688+
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
2689+
}
2690+
}
2691+
}
2692+
2693+
/**
2694+
* Convenience function. Handles val if it is a number
2695+
* @param val value to convert
2696+
* @param initial first char of val
2697+
* @return val as a BigDecimal
2698+
*/
2699+
private static Number getNumber(String val, char initial) {
2700+
// Use a BigDecimal all the time so we keep the original
2701+
// representation. BigDecimal doesn't support -0.0, ensure we
2702+
// keep that by forcing a decimal.
2703+
try {
2704+
BigDecimal bd = new BigDecimal(val);
2705+
if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
2706+
return Double.valueOf(-0.0);
2707+
}
2708+
return bd;
2709+
} catch (NumberFormatException retryAsDouble) {
2710+
// this is to support "Hex Floats" like this: 0x1.0P-1074
2711+
try {
2712+
Double d = Double.valueOf(val);
2713+
if(d.isNaN() || d.isInfinite()) {
2714+
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
2715+
}
2716+
return d;
2717+
} catch (NumberFormatException ignore) {
2718+
throw new NumberFormatException("val ["+ val +"] is not a valid number.");
2719+
}
2720+
}
2721+
}
2722+
26972723
/**
26982724
* Throw an exception if the object is a NaN or infinite number.
26992725
*
@@ -3044,28 +3070,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
30443070
// might throw an exception
30453071
attemptWriteValue(writer, indentFactor, indent, entry, key);
30463072
} else if (length != 0) {
3047-
final int newIndent = indent + indentFactor;
3048-
for (final Entry<String,?> entry : this.entrySet()) {
3049-
if (needsComma) {
3050-
writer.write(',');
3051-
}
3052-
if (indentFactor > 0) {
3053-
writer.write('\n');
3054-
}
3055-
indent(writer, newIndent);
3056-
final String key = entry.getKey();
3057-
writer.write(quote(key));
3058-
writer.write(':');
3059-
if (indentFactor > 0) {
3060-
writer.write(' ');
3061-
}
3062-
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
3063-
needsComma = true;
3064-
}
3065-
if (indentFactor > 0) {
3066-
writer.write('\n');
3067-
}
3068-
indent(writer, indent);
3073+
writeContent(writer, indentFactor, indent, needsComma);
30693074
}
30703075
writer.write('}');
30713076
return writer;
@@ -3074,6 +3079,44 @@ public Writer write(Writer writer, int indentFactor, int indent)
30743079
}
30753080
}
30763081

3082+
/**
3083+
* Convenience function. Writer attempts to write formatted content
3084+
* @param writer
3085+
* Writes the serialized JSON
3086+
* @param indentFactor
3087+
* The number of spaces to add to each level of indentation.
3088+
* @param indent
3089+
* The indentation of the top level.
3090+
* @param needsComma
3091+
* Boolean flag indicating a comma is needed
3092+
* @throws IOException
3093+
* If something goes wrong
3094+
*/
3095+
private void writeContent(Writer writer, int indentFactor, int indent, boolean needsComma) throws IOException {
3096+
final int newIndent = indent + indentFactor;
3097+
for (final Entry<String,?> entry : this.entrySet()) {
3098+
if (needsComma) {
3099+
writer.write(',');
3100+
}
3101+
if (indentFactor > 0) {
3102+
writer.write('\n');
3103+
}
3104+
indent(writer, newIndent);
3105+
final String key = entry.getKey();
3106+
writer.write(quote(key));
3107+
writer.write(':');
3108+
if (indentFactor > 0) {
3109+
writer.write(' ');
3110+
}
3111+
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
3112+
needsComma = true;
3113+
}
3114+
if (indentFactor > 0) {
3115+
writer.write('\n');
3116+
}
3117+
indent(writer, indent);
3118+
}
3119+
30773120
/**
30783121
* Convenience function. Writer attempts to write a value.
30793122
* @param writer

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3895,7 +3895,8 @@ public void issue743SerializationMapWith512Objects() {
38953895
}
38963896

38973897
@Test
3898-
public void issue743SerializationMapWith1000Objects() {
3898+
public void issue743SerializationMapWith500Objects() {
3899+
// TODO: find out why 1000 objects no longer works
38993900
HashMap<String, Object> map = buildNestedMap(500);
39003901
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(500);
39013902
JSONObject object = new JSONObject(map, parserConfiguration);

0 commit comments

Comments
 (0)