Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public JSONArray() {
* @param x
* A JSONTokener
* @throws JSONException
* If there is a syntax error.
* If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
this(x, x.getJsonParserConfiguration());
Expand All @@ -94,9 +94,21 @@ public JSONArray(JSONTokener x) throws JSONException {
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/
public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(x, jsonParserConfiguration, x.isAtStart());
}

/**
* Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration, for internal use. <br>
* Never call this instead of using withStrictMode(boolean).
*
* @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @param isInitial A boolean that determines whether this array is the root.
* @throws JSONException If a syntax error occurs during the construction of the JSONArray.
*/
JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();

boolean isInitial = x.getPrevious() == 0;
if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public JSONObject(JSONObject jo, String ... names) {
* @param x
* A JSONTokener object containing the source string.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x) throws JSONException {
this(x, x.getJsonParserConfiguration());
Expand All @@ -210,12 +210,29 @@ public JSONObject(JSONTokener x) throws JSONException {
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException {
this(x, jsonParserConfiguration, x.isAtStart());
}

/**
* Construct a JSONObject from a JSONTokener with custom json parse configurations, for internal use. <br>
* Never call this instead of using withStrictMode(boolean).
*
* @param x
* A JSONTokener object containing the source string.
* @param jsonParserConfiguration
* Variable to pass parser custom configuration for json parsing.
* @param isInitial
* A boolean that determines whether this object is the root.
* @throws JSONException
* If there is a syntax error in the source string or a
* duplicated key.
*/
JSONObject(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) throws JSONException {
this();
boolean isInitial = x.getPrevious() == 0;

if (x.nextClean() != '{') {
throw x.syntaxError("A JSONObject text must begin with '{'");
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class JSONTokener {
private boolean usePrevious;
/** the number of characters read in the previous line. */
private long characterPreviousLine;
/** number of non-whitespace characters read from the source. */
private long contentCharCount;

// access to this object is required for strict mode checking
private JSONParserConfiguration jsonParserConfiguration;
Expand Down Expand Up @@ -60,6 +62,7 @@ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.contentCharCount = 0;
this.character = 1;
this.characterPreviousLine = 0;
this.line = 1;
Expand Down Expand Up @@ -120,6 +123,17 @@ public void setJsonParserConfiguration(JSONParserConfiguration jsonParserConfigu
this.jsonParserConfiguration = jsonParserConfiguration;
}

/**
* Returns whether the tokener is positioned at the beginning,
* i.e. only whitespace characters (or no characters at all) have been read so far.
* Consuming and backing up over the first characters does not change the result.
*
* @return true if no non-whitespace character has been read
*/
protected boolean isAtStart() {
return this.contentCharCount == 0;
}

/**
* Back up one character. This provides a sort of lookahead capability,
* so that you can test for a digit or letter before attempting to parse
Expand All @@ -131,6 +145,9 @@ public void back() throws JSONException {
if (this.usePrevious || this.index <= 0) {
throw new JSONException("Stepping back two steps is not supported");
}
if (this.previous > ' ') {
this.contentCharCount--;
}
this.decrementIndexes();
this.usePrevious = true;
this.eof = false;
Expand Down Expand Up @@ -231,6 +248,9 @@ public char next() throws JSONException {
return 0;
}
this.incrementIndexes(c);
if (c > ' ') {
this.contentCharCount++;
}
this.previous = (char) c;
return this.previous;
}
Expand Down Expand Up @@ -458,14 +478,14 @@ public Object nextValue() throws JSONException {
case '{':
this.back();
try {
return new JSONObject(this, jsonParserConfiguration);
return new JSONObject(this, jsonParserConfiguration, false);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
case '[':
this.back();
try {
return new JSONArray(this, jsonParserConfiguration);
return new JSONArray(this, jsonParserConfiguration, false);
} catch (StackOverflowError e) {
throw new JSONException("JSON Array or Object depth too large to process.", e);
}
Expand Down Expand Up @@ -549,6 +569,7 @@ public char skipTo(char to) throws JSONException {
long startIndex = this.index;
long startCharacter = this.character;
long startLine = this.line;
long startContentCharCount = this.contentCharCount;
this.reader.mark(1000000);
do {
c = this.next();
Expand All @@ -560,6 +581,7 @@ public char skipTo(char to) throws JSONException {
this.index = startIndex;
this.character = startCharacter;
this.line = startLine;
this.contentCharCount = startContentCharCount;
return 0;
}
} while (c != to);
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,12 @@
assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8")));
assertTrue("expected 1 item in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 1);
assertTrue("expected world", "world".equals(jsonArray.query("/9/0")));
assertTrue("expected 4 items in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);

Check warning on line 536 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcosp&open=AaBiIZIVi8ZKxY3Jcosp&pullRequest=1074
assertTrue("expected value1", "value1".equals(jsonArray.query("/10/key1")));

Check warning on line 537 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcosq&open=AaBiIZIVi8ZKxY3Jcosq&pullRequest=1074
assertTrue("expected value2", "value2".equals(jsonArray.query("/10/key2")));

Check warning on line 538 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcosr&open=AaBiIZIVi8ZKxY3Jcosr&pullRequest=1074
assertTrue("expected value3", "value3".equals(jsonArray.query("/10/key3")));
assertTrue("expected value4", "value4".equals(jsonArray.query("/10/key4")));
assertTrue("expected 0", Integer.valueOf(0).equals(jsonArray.query("/11")));

Check warning on line 541 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcosu&open=AaBiIZIVi8ZKxY3Jcosu&pullRequest=1074
assertTrue("expected \"-1\"", "-1".equals(jsonArray.query("/12")));
Util.checkJSONArrayMaps(jsonArray);
}
Expand Down Expand Up @@ -590,7 +590,7 @@
assertTrue("Array opt boolean object implicit default",
Boolean.FALSE.equals(jsonArray.optBooleanObject(-1)));

assertTrue("Array opt double",

Check warning on line 593 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcos9&open=AaBiIZIVi8ZKxY3Jcos9&pullRequest=1074
Double.valueOf(23.45e-4).equals(jsonArray.optDouble(5)));
assertTrue("Array opt double default",
Double.valueOf(1).equals(jsonArray.optDouble(0, 1)));
Expand Down Expand Up @@ -647,7 +647,7 @@
"value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0)));

JSONObject nestedJsonObject = jsonArray.optJSONObject(10);
assertTrue("Array opt JSONObject", nestedJsonObject != null);

Check warning on line 650 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertNotNull instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3JcotQ&open=AaBiIZIVi8ZKxY3JcotQ&pullRequest=1074
assertTrue("Array opt JSONObject null",
null == jsonArray.optJSONObject(99));
assertTrue("Array opt JSONObject default",
Expand All @@ -669,7 +669,7 @@

assertTrue("Array opt string",
"hello".equals(jsonArray.optString(4)));
assertTrue("Array opt string default implicit",

Check warning on line 672 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcota&open=AaBiIZIVi8ZKxY3Jcota&pullRequest=1074
"".equals(jsonArray.optString(-1)));
Util.checkJSONArraysMaps(new ArrayList<JSONArray>(Arrays.asList(
jsonArray, nestedJsonArray
Expand Down Expand Up @@ -756,7 +756,7 @@
assertTrue("expected 10 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 10);
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);

Check warning on line 759 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcotq&open=AaBiIZIVi8ZKxY3Jcotq&pullRequest=1074
assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0")));
assertTrue("expected world", "world".equals(jsonArray.query("/2/1")));
assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3")));
Expand Down Expand Up @@ -847,9 +847,9 @@
assertTrue("expected 3 items in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 3);
assertTrue("expected val10", "val10".equals(jsonArray.query("/8/key10")));
assertTrue("expected val20", "val20".equals(jsonArray.query("/8/key20")));
assertTrue("expected val30", "val30".equals(jsonArray.query("/8/key30")));

Check warning on line 850 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3JcouI&open=AaBiIZIVi8ZKxY3JcouI&pullRequest=1074
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));

Check warning on line 852 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3JcouK&open=AaBiIZIVi8ZKxY3JcouK&pullRequest=1074
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
assertTrue("expected 1 item in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
assertTrue("expected v1", "v1".equals(jsonArray.query("/10/k1")));
Expand Down Expand Up @@ -1074,7 +1074,7 @@
JSONObject nestedJsonObject = (JSONObject)it.next();
assertTrue("Array value JSONObject", nestedJsonObject != null);

assertTrue("Array value long",

Check warning on line 1077 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcouj&open=AaBiIZIVi8ZKxY3Jcouj&pullRequest=1074
Long.valueOf(0).equals(((Number) it.next()).longValue()));
assertTrue("Array value string long",
Long.valueOf(-1).equals(Long.parseLong((String) it.next())));
Expand Down Expand Up @@ -1266,7 +1266,7 @@
Map<?,?> val2Map = (Map<?,?>) list.get(1);
assertTrue("val2 should not be null", val2Map != null);
assertTrue("val2 should have 4 elements", val2Map.size() == 4);
assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));

Check warning on line 1269 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcoux&open=AaBiIZIVi8ZKxY3Jcoux&pullRequest=1074
assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42)));

Map<?,?> val2Key2Map = (Map<?,?>)val2Map.get("key2");
Expand All @@ -1275,7 +1275,7 @@
assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null);

List<?> val2Key4List = (List<?>)val2Map.get("key4");
assertTrue("val2 map key 4 should not be null", val2Key4List != null);

Check warning on line 1278 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertNotNull instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcou1&open=AaBiIZIVi8ZKxY3Jcou1&pullRequest=1074
assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty());

List<?> val3List = (List<?>) list.get(2);
Expand All @@ -1285,7 +1285,7 @@
List<?> val3Val1List = (List<?>)val3List.get(0);
assertTrue("val3 list val 1 should not be null", val3Val1List != null);
assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));

Check warning on line 1288 in src/test/java/org/json/junit/JSONArrayTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIZIVi8ZKxY3Jcou6&open=AaBiIZIVi8ZKxY3Jcou6&pullRequest=1074
assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(new BigDecimal("2.1")));

List<?> val3Val2List = (List<?>)val3List.get(1);
Expand Down Expand Up @@ -1566,4 +1566,56 @@
"[1,null,3]", jsonArray.toString());
}
}

@Test
Comment thread
XIAYM-gh marked this conversation as resolved.
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener("[]xxx");
tok.next();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}

@Test
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener(" []xxx");
tok.next();
tok.next();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}

@Test
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener(" []xxx");
tok.nextClean();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONArray(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
}
51 changes: 51 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 6 top level items", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 6);
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));

Check warning on line 319 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcomg&open=AaBiIYzwi8ZKxY3Jcomg&pullRequest=1074
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"",
"h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
Expand Down Expand Up @@ -780,7 +780,7 @@
// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 1 top level item", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 1);
assertTrue("expected 6 myArray items", ((List<?>) (JsonPath.read(doc, "$.myArray"))).size() == 6);

Check warning on line 783 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JconD&open=AaBiIYzwi8ZKxY3JconD&pullRequest=1074
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0")));
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1")));
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
Expand Down Expand Up @@ -877,7 +877,7 @@
assertTrue("optNumber negZeroKey should return Double", jsonObject.optNumber("negZeroKey") instanceof Double);
assertTrue("optNumber negZeroStrKey should return Double",
jsonObject.optNumber("negZeroStrKey") instanceof Double);
assertTrue("opt negZeroKey should be double", Double.compare(jsonObject.optDouble("negZeroKey"), -0.0d) == 0);

Check warning on line 880 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcond&open=AaBiIYzwi8ZKxY3Jcond&pullRequest=1074
assertTrue("opt negZeroStrKey with Default should be double",
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
assertTrue("opt negZeroKey should be Double",
Expand All @@ -891,7 +891,7 @@
assertTrue("optFloat doubleKey should be float", jsonObject.optFloat("doubleKey") == -23.45e7f);
assertTrue("optFloat doubleKey with Default should be float",
jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
assertTrue("optFloat doubleKey should be Float",

Check warning on line 894 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jconl&open=AaBiIYzwi8ZKxY3Jconl&pullRequest=1074
Float.valueOf(-23.45e7f).equals(jsonObject.optFloatObject("doubleKey")));
assertTrue("optFloat doubleKey with Default should be Float",
Float.valueOf(1f).equals(jsonObject.optFloatObject("doubleStrKey", Float.NaN)));
Expand All @@ -903,7 +903,7 @@
assertTrue("opt intKey with default should be int", jsonObject.getInt("intKey") == 42);
assertTrue("intStrKey should be int", jsonObject.getInt("intStrKey") == 43);
assertTrue("longKey should be long", jsonObject.getLong("longKey") == 1234567890123456789L);
assertTrue("opt longKey should be long", jsonObject.optLong("longKey") == 1234567890123456789L);

Check warning on line 906 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jconu&open=AaBiIYzwi8ZKxY3Jconu&pullRequest=1074
assertTrue("opt longKey with default should be long", jsonObject.optLong("longKey", 0) == 1234567890123456789L);
assertTrue("opt longKey should be Long",
Long.valueOf(1234567890123456789L).equals(jsonObject.optLongObject("longKey")));
Expand All @@ -922,7 +922,7 @@
jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
assertTrue("xKey should not exist", jsonObject.isNull("xKey"));
assertTrue("stringKey should exist", jsonObject.has("stringKey"));
assertTrue("opt stringKey should string", jsonObject.optString("stringKey").equals("hello world!"));

Check warning on line 925 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jconz&open=AaBiIYzwi8ZKxY3Jconz&pullRequest=1074
assertTrue("opt stringKey with default should string",
jsonObject.optString("stringKey", "not found").equals("hello world!"));
JSONArray jsonArray = jsonObject.getJSONArray("arrayKey");
Expand Down Expand Up @@ -1014,19 +1014,19 @@
Object obj;
obj = jsonObject.get("hexNumber");
assertFalse("hexNumber must not be a number (should throw exception!?)", obj instanceof Number);
assertTrue("hexNumber currently evaluates to string", obj.equals("-0x123"));

Check warning on line 1017 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcon8&open=AaBiIYzwi8ZKxY3Jcon8&pullRequest=1074
assertTrue("tooManyZeros currently evaluates to string", jsonObject.get("tooManyZeros").equals("00"));
obj = jsonObject.get("negativeInfinite");
assertTrue("negativeInfinite currently evaluates to string", obj.equals("-Infinity"));
obj = jsonObject.get("negativeNaN");
assertTrue("negativeNaN currently evaluates to string", obj.equals("-NaN"));

Check warning on line 1022 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcon_&open=AaBiIYzwi8ZKxY3Jcon_&pullRequest=1074
assertTrue("negativeFraction currently evaluates to double -0.01",
jsonObject.get("negativeFraction").equals(BigDecimal.valueOf(-0.01)));
assertTrue("tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.optLong("tooManyZerosFraction") == 0);
assertTrue("negativeHexFloat currently evaluates to double -3.99951171875",
jsonObject.get("negativeHexFloat").equals(Double.valueOf(-3.99951171875)));
assertTrue("hexFloat currently evaluates to double 4.9E-324",

Check warning on line 1029 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcooD&open=AaBiIYzwi8ZKxY3JcooD&pullRequest=1074
jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324)));
assertTrue("floatIdentifier currently evaluates to double 0.1",
jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
Expand Down Expand Up @@ -1219,7 +1219,7 @@
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("bigInt", bigInteger);
assertTrue("jsonObject.put() handles bigInt correctly", jsonObject2.get("bigInt").equals(bigInteger));
assertTrue("jsonObject.getBigInteger() handles bigInt correctly",

Check warning on line 1222 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcooP&open=AaBiIYzwi8ZKxY3JcooP&pullRequest=1074
jsonObject2.getBigInteger("bigInt").equals(bigInteger));
assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
jsonObject2.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
Expand Down Expand Up @@ -1329,7 +1329,7 @@
fail("should not be able to get big dec");
} catch (Exception ignored) {
}
assertTrue("optBigInt is default", jsonArray0.optBigInteger(2, BigInteger.ONE).equals(BigInteger.ONE));

Check warning on line 1332 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcook&open=AaBiIYzwi8ZKxY3Jcook&pullRequest=1074
assertTrue("optBigDec is default", jsonArray0.optBigDecimal(2, BigDecimal.ONE).equals(BigDecimal.ONE));

// bigInt,bigDec list ctor
Expand Down Expand Up @@ -1467,7 +1467,7 @@
public void jsonObjectNames() {

// getNames() from null JSONObject
assertTrue("null names from null Object", null == JSONObject.getNames((Object) null));

Check warning on line 1470 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertNull instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcooq&open=AaBiIYzwi8ZKxY3Jcooq&pullRequest=1074

// getNames() from object with no fields
assertTrue("null names from Object with no fields", null == JSONObject.getNames(new MyJsonString()));
Expand Down Expand Up @@ -1509,7 +1509,7 @@
docList = JsonPath.read(doc, "$");
assertTrue("expected 3 items", docList.size() == 3);
assertTrue("expected to find VAL1", ((List<?>) JsonPath.read(doc, "$[?(@=='VAL1')]")).size() == 1);
assertTrue("expected to find VAL2", ((List<?>) JsonPath.read(doc, "$[?(@=='VAL2')]")).size() == 1);

Check warning on line 1512 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoo0&open=AaBiIYzwi8ZKxY3Jcoo0&pullRequest=1074
assertTrue("expected to find VAL3", ((List<?>) JsonPath.read(doc, "$[?(@=='VAL3')]")).size() == 1);

/**
Expand Down Expand Up @@ -1562,7 +1562,7 @@
assertTrue("expected 3 top level items", ((List<?>) (JsonPath.read(doc, "$"))).size() == 3);
assertTrue("expected to find trueKey", ((List<?>) JsonPath.read(doc, "$[?(@=='trueKey')]")).size() == 1);
assertTrue("expected to find falseKey", ((List<?>) JsonPath.read(doc, "$[?(@=='falseKey')]")).size() == 1);
assertTrue("expected to find stringKey", ((List<?>) JsonPath.read(doc, "$[?(@=='stringKey')]")).size() == 1);

Check warning on line 1565 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoo9&open=AaBiIYzwi8ZKxY3Jcoo9&pullRequest=1074
Util.checkJSONObjectMaps(jsonObject);
Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
}
Expand Down Expand Up @@ -1596,7 +1596,7 @@

// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 6 top level items", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 6);

Check warning on line 1599 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoo-&open=AaBiIYzwi8ZKxY3Jcoo-&pullRequest=1074
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt")));
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong")));
assertTrue("expected 3.1", BigDecimal.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
Expand Down Expand Up @@ -1740,9 +1740,9 @@
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
assertTrue("expected 3 arrayKey items", ((List<?>) (JsonPath.read(doc, "$.arrayKey"))).size() == 3);
assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));

Check warning on line 1743 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcopR&open=AaBiIYzwi8ZKxY3JcopR&pullRequest=1074
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));

Check warning on line 1745 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcopT&open=AaBiIYzwi8ZKxY3JcopT&pullRequest=1074
assertTrue("expected 4 objectKey items", ((Map<?, ?>) (JsonPath.read(doc, "$.objectKey"))).size() == 4);
assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
Expand Down Expand Up @@ -1793,13 +1793,13 @@
assertTrue("expected 4 top level items", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 4);
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
assertTrue("expected 3 arrayKey items", ((List<?>) (JsonPath.read(doc, "$.arrayKey"))).size() == 3);

Check warning on line 1796 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcopc&open=AaBiIYzwi8ZKxY3Jcopc&pullRequest=1074
assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));

Check warning on line 1798 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcope&open=AaBiIYzwi8ZKxY3Jcope&pullRequest=1074
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));
assertTrue("expected 4 objectKey items", ((Map<?, ?>) (JsonPath.read(doc, "$.objectKey"))).size() == 4);
assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));

Check warning on line 1802 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcopi&open=AaBiIYzwi8ZKxY3Jcopi&pullRequest=1074
assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));
Util.checkJSONObjectMaps(jsonObject);
Expand Down Expand Up @@ -1853,7 +1853,7 @@
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
assertTrue("expected 1 top level item", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 1);
assertTrue("expected 1 key item", ((Map<?, ?>) (JsonPath.read(doc, "$.key"))).size() == 1);
assertTrue("expected def", "def".equals(jsonObject.query("/key/abc")));

Check warning on line 1856 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcopn&open=AaBiIYzwi8ZKxY3Jcopn&pullRequest=1074
Util.checkJSONObjectMaps(jsonObject);
}

Expand Down Expand Up @@ -1889,7 +1889,7 @@
assertTrue("null valueToString() incorrect", "null".equals(JSONObject.valueToString(null)));
MyJsonString jsonString = new MyJsonString();
assertTrue("jsonstring valueToString() incorrect", "my string".equals(JSONObject.valueToString(jsonString)));
assertTrue("boolean valueToString() incorrect", "true".equals(JSONObject.valueToString(Boolean.TRUE)));

Check warning on line 1892 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcopt&open=AaBiIYzwi8ZKxY3Jcopt&pullRequest=1074
assertTrue("non-numeric double", "null".equals(JSONObject.doubleToString(Double.POSITIVE_INFINITY)));
String jsonObjectStr = "{" + "\"key1\":\"val1\"," + "\"key2\":\"val2\"," + "\"key3\":\"val3\"" + "}";
JSONObject jsonObject = new JSONObject(jsonObjectStr);
Expand Down Expand Up @@ -1989,7 +1989,7 @@
assertTrue("expected 3 top level items", ((List<?>) (JsonPath.read(doc, "$"))).size() == 3);
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));

Check warning on line 1992 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcop_&open=AaBiIYzwi8ZKxY3Jcop_&pullRequest=1074

// validate JSON
doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString());
Expand Down Expand Up @@ -2228,7 +2228,7 @@

@Test
public void parsingErrorInvalidKey() {
try {

Check warning on line 2231 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the body of this try/catch to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcoqP&open=AaBiIYzwi8ZKxY3JcoqP&pullRequest=1074
// invalid key
String str = "{\"myKey\":true, \"myOtherKey\":false}";
JSONObject jsonObject = new JSONObject(str);
Expand Down Expand Up @@ -2500,7 +2500,7 @@
assertTrue("optInt() should return default int", 42 == jsonObject.optInt("myKey", 42));
assertTrue("optIntegerObject() should return default Integer",
Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42)));
assertTrue("optEnum() should return default Enum",

Check warning on line 2503 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoql&open=AaBiIYzwi8ZKxY3Jcoql&pullRequest=1074
MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
assertTrue("optJSONArray() should return default JSONArray",
"value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0)));
Expand All @@ -2509,7 +2509,7 @@
jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey")
.equals("testValue"));
assertTrue("optLong() should return default long", 42l == jsonObject.optLong("myKey", 42l));
assertTrue("optLongObject() should return default Long",

Check warning on line 2512 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoqq&open=AaBiIYzwi8ZKxY3Jcoqq&pullRequest=1074
Long.valueOf(42l).equals(jsonObject.optLongObject("myKey", 42l)));
assertTrue("optDouble() should return default double", 42.3d == jsonObject.optDouble("myKey", 42.3d));
assertTrue("optDoubleObject() should return default Double",
Expand All @@ -2535,13 +2535,13 @@
assertTrue("unexpected optBoolean value", jo.optBoolean("false", true) == false);
assertTrue("unexpected optBooleanObject value",
Boolean.valueOf(false).equals(jo.optBooleanObject("false", true)));
assertTrue("unexpected optInt value", jo.optInt("int", 0) == 123);

Check warning on line 2538 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoq1&open=AaBiIYzwi8ZKxY3Jcoq1&pullRequest=1074
assertTrue("unexpected optIntegerObject value", Integer.valueOf(123).equals(jo.optIntegerObject("int", 0)));

Check warning on line 2539 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoq2&open=AaBiIYzwi8ZKxY3Jcoq2&pullRequest=1074
assertTrue("unexpected optLong value", jo.optLong("int", 0) == 123l);
assertTrue("unexpected optLongObject value", Long.valueOf(123l).equals(jo.optLongObject("int", 0L)));
assertTrue("unexpected optDouble value", jo.optDouble("int", 0.0d) == 123.0d);
assertTrue("unexpected optDoubleObject value", Double.valueOf(123.0d).equals(jo.optDoubleObject("int", 0.0d)));
assertTrue("unexpected optFloat value", jo.optFloat("int", 0.0f) == 123.0f);

Check warning on line 2544 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcoq7&open=AaBiIYzwi8ZKxY3Jcoq7&pullRequest=1074
assertTrue("unexpected optFloatObject value", Float.valueOf(123.0f).equals(jo.optFloatObject("int", 0.0f)));
assertTrue("unexpected optBigInteger value",
jo.optBigInteger("int", BigInteger.ZERO).compareTo(new BigInteger("123")) == 0);
Expand Down Expand Up @@ -2763,7 +2763,7 @@

// test single element JSONObject
StringWriter writer = new StringWriter();
try {

Check warning on line 2766 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the body of this try/catch to have only one invocation possibly throwing a runtime exception.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcorM&open=AaBiIYzwi8ZKxY3JcorM&pullRequest=1074
jsonObject.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
Expand Down Expand Up @@ -3019,7 +3019,7 @@
* Exercise JSONObject toMap() method.
*/
@Test
public void toMap() {

Check warning on line 3022 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce the number of assertions from 29 to less than 25.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcorX&open=AaBiIYzwi8ZKxY3JcorX&pullRequest=1074
String jsonObjectStr = "{" + "\"key1\":" + "[1,2," + "{\"key3\":true}" + "]," + "\"key2\":"
+ "{\"key1\":\"val1\",\"key2\":" + "{\"key2\":null}," + "\"key3\":42" + "}," + "\"key3\":" + "["
+ "[\"value1\",2.1]" + "," + "[null]" + "]" + "}";
Expand All @@ -3031,10 +3031,10 @@
assertTrue("Map should have 3 elements", map.size() == 3);

List<?> key1List = (List<?>) map.get("key1");
assertTrue("key1 should not be null", key1List != null);

Check warning on line 3034 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertNotNull instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcora&open=AaBiIYzwi8ZKxY3Jcora&pullRequest=1074
assertTrue("key1 list should have 3 elements", key1List.size() == 3);
assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1)));
assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2)));

Check warning on line 3037 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcord&open=AaBiIYzwi8ZKxY3Jcord&pullRequest=1074

Map<?, ?> key1Value3Map = (Map<?, ?>) key1List.get(2);
assertTrue("Map should not be null", key1Value3Map != null);
Expand Down Expand Up @@ -3072,8 +3072,8 @@
assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));

// assert that the new map is mutable
assertTrue("Removing a key should succeed", map.remove("key3") != null);

Check warning on line 3075 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertNotNull instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcorx&open=AaBiIYzwi8ZKxY3Jcorx&pullRequest=1074
assertTrue("Map should have 2 elements", map.size() == 2);

Check warning on line 3076 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertEquals instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3Jcory&open=AaBiIYzwi8ZKxY3Jcory&pullRequest=1074
Util.checkJSONObjectMaps(jsonObject);
}

Expand Down Expand Up @@ -3625,7 +3625,7 @@
JSONObject j3 = new JSONObject("{ " + "\"hex1\": \"010e4\", \"hex2\": \"00f0\", \"hex3\": \"0011\", "
+ "\"hex4\": 00e0, \"hex5\": \"00f0\", \"hex6\": \"0011\" }");
assertEquals(j3.getString("hex1"), "010e4");
assertEquals(j3.getString("hex2"), "00f0");

Check warning on line 3628 in src/test/java/org/json/junit/JSONObjectTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Swap these 2 arguments so they are in the correct order: expected value, actual value.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBiIYzwi8ZKxY3JcosA&open=AaBiIYzwi8ZKxY3JcosA&pullRequest=1074
assertEquals(j3.getString("hex3"), "0011");
assertEquals(j3.getLong("hex4"), 0, .1);
assertEquals(j3.getString("hex5"), "00f0");
Expand Down Expand Up @@ -4337,4 +4337,55 @@
}
}

@Test
Comment thread
XIAYM-gh marked this conversation as resolved.
public void strictModeShouldCheckTrailingCharactersAfterNextAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener("{}xxx");
tok.next();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}

@Test
public void strictModeShouldCheckTrailingCharactersAfterConsumedWhitespace() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener(" {}xxx");
tok.next();
tok.next();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}

@Test
public void strictModeShouldCheckTrailingCharactersAfterNextCleanAndBack() {
JSONParserConfiguration strict =
new JSONParserConfiguration().withStrictMode();

JSONTokener tok = new JSONTokener(" {}xxx");
tok.nextClean();
tok.back();

JSONException exception = assertThrows(
JSONException.class,
() -> new JSONObject(tok, strict));

assertTrue(exception.getMessage().contains(
"Unparsed characters found at end of input text"));
}
}
Loading