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
34 changes: 29 additions & 5 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
Expand Down Expand Up @@ -1750,18 +1751,25 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
* Make a JSON text of this JSONArray. For compactness, no unnecessary
* whitespace is added. If it is not possible to produce a syntactically
* correct JSON text then null will be returned instead. This could occur if
* the array contains an invalid number.
* the array contains an invalid number. Cyclic references throw
* {@link JSONException}; other serialization failures still return null.
* <p><b>
* Warning: This method assumes that the data structure is acyclical.
* </b>
*
* @return a printable, displayable, transmittable representation of the
* array.
* @throws JSONException if a cyclic reference is detected during serialization
*/
@Override
public String toString() {
try {
return this.toString(0);
} catch (JSONException e) {
if (JSONObject.isCyclicReferenceException(e)) {
throw e;
}
return null;
} catch (Exception e) {
return null;
}
Expand Down Expand Up @@ -1848,13 +1856,21 @@ public Writer write(Writer writer) throws JSONException {
@SuppressWarnings("resource")
public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
return write(writer, indentFactor, indent, JSONObject.newSerializationStack());
}

Writer write(Writer writer, int indentFactor, int indent, Set<Object> serializationStack)
throws JSONException {
if (!serializationStack.add(this)) {
throw JSONObject.cyclicReferenceDuringSerializationException();
}
try {
boolean needsComma = false;
int length = this.length();
writer.write('[');

if (length == 1) {
writeArrayAttempt(writer, indentFactor, indent, 0);
writeArrayAttempt(writer, indentFactor, indent, 0, serializationStack);
} else if (length != 0) {
final int newIndent = indent + indentFactor;

Expand All @@ -1866,7 +1882,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
writer.write('\n');
}
JSONObject.indent(writer, newIndent);
writeArrayAttempt(writer, indentFactor, newIndent, i);
writeArrayAttempt(writer, indentFactor, newIndent, i, serializationStack);
needsComma = true;
}
if (indentFactor > 0) {
Expand All @@ -1878,6 +1894,8 @@ public Writer write(Writer writer, int indentFactor, int indent)
return writer;
} catch (IOException e) {
throw new JSONException(e);
} finally {
serializationStack.remove(this);
}
}

Expand All @@ -1892,10 +1910,16 @@ public Writer write(Writer writer, int indentFactor, int indent)
* @param i
* Index in array to be added
*/
private void writeArrayAttempt(Writer writer, int indentFactor, int indent, int i) {
private void writeArrayAttempt(Writer writer, int indentFactor, int indent, int i,
Set<Object> serializationStack) {
try {
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, indent);
indentFactor, indent, serializationStack);
} catch (JSONException e) {
if (JSONObject.isCyclicReferenceException(e)) {
throw e;
}
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
} catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
}
Expand Down
84 changes: 72 additions & 12 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2953,7 +2953,8 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
/**
* Make a JSON text of this JSONObject. For compactness, no whitespace is
* added. If this would not result in a syntactically correct JSON text,
* then null will be returned instead.
* then null will be returned instead. Cyclic references throw
* {@link JSONException}; other serialization failures still return null.
* <p><b>
* Warning: This method assumes that the data structure is acyclical.
* </b>
Expand All @@ -2962,11 +2963,17 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
* of the object, beginning with <code>{</code>&nbsp;<small>(left
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>.
* @throws JSONException if a cyclic reference is detected during serialization
*/
@Override
public String toString() {
try {
return this.toString(0);
} catch (JSONException e) {
if (isCyclicReferenceException(e)) {
throw e;
}
return null;
} catch (Exception e) {
return null;
}
Expand Down Expand Up @@ -3140,9 +3147,45 @@ public Writer write(Writer writer) throws JSONException {
return this.write(writer, 0, 0);
}

static Set<Object> newSerializationStack() {
return Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
}

static final String CYCLIC_REFERENCE_MESSAGE =
"Cyclic reference detected during serialization";

static final class CyclicReferenceException extends JSONException {
private static final long serialVersionUID = 1L;

CyclicReferenceException() {
super(CYCLIC_REFERENCE_MESSAGE);
}
}

static JSONException cyclicReferenceDuringSerializationException() {
return new CyclicReferenceException();
}

static boolean isCyclicReferenceException(Throwable throwable) {
while (throwable != null) {
if (throwable instanceof CyclicReferenceException) {
return true;
}
throwable = throwable.getCause();
}
return false;
}

@SuppressWarnings("resource")
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
return writeValue(writer, value, indentFactor, indent, newSerializationStack());
}

@SuppressWarnings("resource")
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent, Set<Object> serializationStack)
throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONString) {
Expand All @@ -3160,17 +3203,17 @@ static final Writer writeValue(Writer writer, Object value,
} else if (value instanceof Enum<?>) {
writer.write(quote(((Enum<?>)value).name()));
} else if (value instanceof JSONObject) {
((JSONObject) value).write(writer, indentFactor, indent);
((JSONObject) value).write(writer, indentFactor, indent, serializationStack);
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
((JSONArray) value).write(writer, indentFactor, indent, serializationStack);
} else if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
new JSONObject(map).write(writer, indentFactor, indent);
new JSONObject(map).write(writer, indentFactor, indent, serializationStack);
} else if (value instanceof Collection) {
Collection<?> coll = (Collection<?>) value;
new JSONArray(coll).write(writer, indentFactor, indent);
new JSONArray(coll).write(writer, indentFactor, indent, serializationStack);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
new JSONArray(value).write(writer, indentFactor, indent, serializationStack);
} else {
quote(value.toString(), writer);
}
Expand Down Expand Up @@ -3248,6 +3291,14 @@ static final void indent(Writer writer, int indent) throws IOException {
@SuppressWarnings("resource")
public Writer write(Writer writer, int indentFactor, int indent)
throws JSONException {
return write(writer, indentFactor, indent, newSerializationStack());
}

Writer write(Writer writer, int indentFactor, int indent, Set<Object> serializationStack)
throws JSONException {
if (!serializationStack.add(this)) {
throw cyclicReferenceDuringSerializationException();
}
try {
boolean needsComma = false;
final int length = this.length();
Expand All @@ -3262,14 +3313,16 @@ public Writer write(Writer writer, int indentFactor, int indent)
writer.write(' ');
}
// might throw an exception
attemptWriteValue(writer, indentFactor, indent, entry, key);
attemptWriteValue(writer, indentFactor, indent, entry, key, serializationStack);
} else if (length != 0) {
writeContent(writer, indentFactor, indent, needsComma);
writeContent(writer, indentFactor, indent, needsComma, serializationStack);
}
writer.write('}');
return writer;
} catch (IOException exception) {
throw new JSONException(exception);
} finally {
serializationStack.remove(this);
}
}

Expand All @@ -3286,7 +3339,8 @@ public Writer write(Writer writer, int indentFactor, int indent)
* @throws IOException
* If something goes wrong
*/
private void writeContent(Writer writer, int indentFactor, int indent, boolean needsComma) throws IOException {
private void writeContent(Writer writer, int indentFactor, int indent, boolean needsComma,
Set<Object> serializationStack) throws IOException {
final int newIndent = indent + indentFactor;
for (final Entry<String,?> entry : this.entrySet()) {
if (needsComma) {
Expand All @@ -3302,7 +3356,7 @@ private void writeContent(Writer writer, int indentFactor, int indent, boolean n
if (indentFactor > 0) {
writer.write(' ');
}
attemptWriteValue(writer, indentFactor, newIndent, entry, key);
attemptWriteValue(writer, indentFactor, newIndent, entry, key, serializationStack);
needsComma = true;
}
if (indentFactor > 0) {
Expand All @@ -3327,9 +3381,15 @@ private void writeContent(Writer writer, int indentFactor, int indent, boolean n
* occurs

*/
private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry<String, ?> entry, String key) {
private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry<String, ?> entry,
String key, Set<Object> serializationStack) {
try{
writeValue(writer, entry.getValue(), indentFactor, indent);
writeValue(writer, entry.getValue(), indentFactor, indent, serializationStack);
} catch (JSONException e) {
if (isCyclicReferenceException(e)) {
throw e;
}
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
} catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/json/Issue1056CyclicSerializationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.json;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;

import org.junit.Test;

/**
* Tests cyclic-reference detection during serialization. Lives in {@code org.json}
* so tests can assert {@link JSONObject.CyclicReferenceException} by type.
*/
public class Issue1056CyclicSerializationTest {

@Test
public void selfReferentialJSONObject() {
JSONObject jo = new JSONObject();
jo.put("key", "value");
jo.put("self", jo);
JSONException ex = assertThrows(JSONException.class, jo::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}

@Test
public void mutualJSONObjectCycle() {
JSONObject a = new JSONObject();
JSONObject b = new JSONObject();
a.put("b", b);
b.put("a", a);
JSONException ex = assertThrows(JSONException.class, a::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}

@Test
public void mixedJSONObjectJSONArrayCycle() {
JSONObject jo = new JSONObject();
JSONArray arr = new JSONArray();
jo.put("arr", arr);
arr.put(jo);
JSONException ex = assertThrows(JSONException.class, jo::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}

@Test
public void selfReferentialJSONArray() {
JSONArray arr = new JSONArray();
arr.put("x");
arr.put(arr);
JSONException ex = assertThrows(JSONException.class, arr::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}

@Test
public void valueToStringOnCyclicJSONObject() {
JSONObject jo = new JSONObject();
jo.put("self", jo);
JSONException ex = assertThrows(JSONException.class, () -> JSONObject.valueToString(jo));
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@
public void getArrayValues() {
JSONArray jsonArray = new JSONArray(this.arrayStr);
// booleans
assertTrue("Array true",

Check warning on line 380 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=AaBi0LZInE9PClEwN0uX&open=AaBi0LZInE9PClEwN0uX&pullRequest=1075
true == jsonArray.getBoolean(0));
assertTrue("Array false",

Check warning on line 382 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=AaBi0LZInE9PClEwN0uY&open=AaBi0LZInE9PClEwN0uY&pullRequest=1075
false == jsonArray.getBoolean(1));
assertTrue("Array string true",
true == jsonArray.getBoolean(2));
Expand All @@ -406,7 +406,7 @@
JSONObject nestedJsonObject = jsonArray.getJSONObject(10);
assertTrue("Array value JSONObject", nestedJsonObject != null);
// longs
assertTrue("Array value long",

Check warning on line 409 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=AaBi0LZInE9PClEwN0uj&open=AaBi0LZInE9PClEwN0uj&pullRequest=1075
Long.valueOf(0).equals(jsonArray.getLong(11)));
assertTrue("Array value string long",
Long.valueOf(-1).equals(jsonArray.getLong(12)));
Expand Down Expand Up @@ -525,7 +525,7 @@
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2")));
assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3")));

Check warning on line 528 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=AaBi0LZInE9PClEwN0uq&open=AaBi0LZInE9PClEwN0uq&pullRequest=1075
assertTrue("expected hello", "hello".equals(jsonArray.query("/4")));
assertTrue("expected 0.002345", BigDecimal.valueOf(0.002345).equals(jsonArray.query("/5")));
assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6")));
Expand Down Expand Up @@ -587,7 +587,7 @@
Boolean.TRUE.equals(jsonArray.optBooleanObject(0)));
assertTrue("Array opt boolean object default",
Boolean.FALSE.equals(jsonArray.optBooleanObject(-1, Boolean.FALSE)));
assertTrue("Array opt boolean object implicit default",

Check warning on line 590 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=AaBi0LZInE9PClEwN0vF&open=AaBi0LZInE9PClEwN0vF&pullRequest=1075
Boolean.FALSE.equals(jsonArray.optBooleanObject(-1)));

assertTrue("Array opt double",
Expand Down Expand Up @@ -657,12 +657,12 @@
0 == jsonArray.optLong(11));
assertTrue("Array opt long default",
-2 == jsonArray.optLong(-1, -2));
assertTrue("Array opt long default implicit",

Check warning on line 660 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=AaBi0LZInE9PClEwN0ve&open=AaBi0LZInE9PClEwN0ve&pullRequest=1075
0 == jsonArray.optLong(-1));

assertTrue("Array opt long object",
Long.valueOf(0).equals(jsonArray.optLongObject(11)));
assertTrue("Array opt long object default",

Check warning on line 665 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=AaBi0LZInE9PClEwN0vg&open=AaBi0LZInE9PClEwN0vg&pullRequest=1075
Long.valueOf(-2).equals(jsonArray.optLongObject(-1, -2L)));
assertTrue("Array opt long object default implicit",
Long.valueOf(0).equals(jsonArray.optLongObject(-1)));
Expand Down Expand Up @@ -755,14 +755,14 @@
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
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")));

Check warning on line 758 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=AaBi0LZInE9PClEwN0vy&open=AaBi0LZInE9PClEwN0vy&pullRequest=1075
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
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")));
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4")));

Check warning on line 763 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=AaBi0LZInE9PClEwN0v3&open=AaBi0LZInE9PClEwN0v3&pullRequest=1075
assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5")));
assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6")));

Check warning on line 765 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=AaBi0LZInE9PClEwN0v5&open=AaBi0LZInE9PClEwN0v5&pullRequest=1075
assertTrue("expected 3 items in [7]", ((Map<?,?>)(JsonPath.read(doc, "$[7]"))).size() == 3);
assertTrue("expected val10", "val10".equals(jsonArray.query("/7/key10")));
assertTrue("expected val20", "val20".equals(jsonArray.query("/7/key20")));
Expand Down Expand Up @@ -1063,7 +1063,7 @@
assertTrue("Array string double",
Double.valueOf(23.45).equals(Double.parseDouble((String)it.next())));

assertTrue("Array value int",

Check warning on line 1066 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=AaBi0LZInE9PClEwN0wo&open=AaBi0LZInE9PClEwN0wo&pullRequest=1075
Integer.valueOf(42).equals(it.next()));
assertTrue("Array value string int",
Integer.valueOf(43).equals(Integer.parseInt((String)it.next())));
Expand Down Expand Up @@ -1259,7 +1259,7 @@
assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2)));

Map<?,?> key1Value3Map = (Map<?,?>)val1List.get(2);
assertTrue("Map should not be null", key1Value3Map != null);

Check warning on line 1262 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=AaBi0LZInE9PClEwN0w1&open=AaBi0LZInE9PClEwN0w1&pullRequest=1075
assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));

Expand All @@ -1280,12 +1280,12 @@

List<?> val3List = (List<?>) list.get(2);
assertTrue("val3 should not be null", val3List != null);
assertTrue("val3 should have 2 elements", val3List.size() == 2);

Check warning on line 1283 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=AaBi0LZInE9PClEwN0xA&open=AaBi0LZInE9PClEwN0xA&pullRequest=1075

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=AaBi0LZInE9PClEwN0xD&open=AaBi0LZInE9PClEwN0xD&pullRequest=1075
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 All @@ -1295,7 +1295,7 @@

// assert that toList() is a deep copy
jsonArray.getJSONObject(1).put("key1", "still val1");
assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));

Check warning on line 1298 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=AaBi0LZInE9PClEwN0xI&open=AaBi0LZInE9PClEwN0xI&pullRequest=1075

// assert that the new list is mutable
assertTrue("Removing an entry should succeed", list.remove(2) != null);
Expand Down Expand Up @@ -1526,6 +1526,12 @@
}
}

@Test
public void issue1056BrokenToStringStillReturnsNullFromToString() {
JSONArray arr = new JSONArray().put(new JSONObject().put("k", new org.json.junit.data.BrokenToString()));
assertNull(arr.toString());
}

@Test(expected = JSONException.class)
public void testRecursiveDepthArrayFor1001Levels() {
ArrayList<Object> array = buildNestedArray(1001);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@

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

Check warning on line 519 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=AaBi0LVynE9PClEwN0ot&open=AaBi0LVynE9PClEwN0ot&pullRequest=1075
assertTrue("expected 0 key1 items", ((Map<?, ?>) (JsonPath.read(doc, "$.key1"))).size() == 0);
assertTrue("expected \"key2\":java.lang.Exception", "java.lang.Exception".equals(jsonObject.query("/key2")));
Util.checkJSONObjectMaps(jsonObject);
Expand Down Expand Up @@ -730,7 +730,7 @@

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

Check warning on line 733 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=AaBi0LVynE9PClEwN0pB&open=AaBi0LVynE9PClEwN0pB&pullRequest=1075
assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString")));
assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt")));
Util.checkJSONObjectMaps(jsonObject);
Expand Down Expand Up @@ -786,7 +786,7 @@
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3")));
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4")));
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5")));

Check warning on line 789 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=AaBi0LVynE9PClEwN0pS&open=AaBi0LVynE9PClEwN0pS&pullRequest=1075
Util.checkJSONObjectMaps(jsonObject);
}

Expand Down Expand Up @@ -864,7 +864,7 @@
assertTrue("doubleKey should be double", jsonObject.getDouble("doubleKey") == -23.45e7);
assertTrue("doubleStrKey should be double", jsonObject.getDouble("doubleStrKey") == 1);
assertTrue("doubleKey can be float", jsonObject.getFloat("doubleKey") == -23.45e7f);
assertTrue("doubleStrKey can be float", jsonObject.getFloat("doubleStrKey") == 1f);

Check warning on line 867 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=AaBi0LVynE9PClEwN0ph&open=AaBi0LVynE9PClEwN0ph&pullRequest=1075
assertTrue("opt doubleKey should be double", jsonObject.optDouble("doubleKey") == -23.45e7);
assertTrue("opt doubleKey with Default should be double",
jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
Expand All @@ -882,7 +882,7 @@
Double.compare(jsonObject.optDouble("negZeroStrKey"), -0.0d) == 0);
assertTrue("opt negZeroKey should be Double",
Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroKey")));
assertTrue("opt negZeroStrKey with Default should be Double",

Check warning on line 885 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=AaBi0LVynE9PClEwN0pp&open=AaBi0LVynE9PClEwN0pp&pullRequest=1075
Double.valueOf(-0.0d).equals(jsonObject.optDoubleObject("negZeroStrKey")));
assertTrue("optNumber negZeroKey should be -0.0",
Double.compare(jsonObject.optNumber("negZeroKey").doubleValue(), -0.0d) == 0);
Expand All @@ -893,12 +893,12 @@
jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
assertTrue("optFloat doubleKey should be Float",
Float.valueOf(-23.45e7f).equals(jsonObject.optFloatObject("doubleKey")));
assertTrue("optFloat doubleKey with Default should be Float",

Check warning on line 896 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=AaBi0LVynE9PClEwN0pv&open=AaBi0LVynE9PClEwN0pv&pullRequest=1075
Float.valueOf(1f).equals(jsonObject.optFloatObject("doubleStrKey", Float.NaN)));
assertTrue("intKey should be int", jsonObject.optInt("intKey") == 42);
assertTrue("opt intKey should be int", jsonObject.optInt("intKey", 0) == 42);
assertTrue("intKey should be Integer", Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey")));
assertTrue("opt intKey should be Integer",

Check warning on line 901 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=AaBi0LVynE9PClEwN0pz&open=AaBi0LVynE9PClEwN0pz&pullRequest=1075
Integer.valueOf(42).equals(jsonObject.optIntegerObject("intKey", 0)));
assertTrue("opt intKey with default should be int", jsonObject.getInt("intKey") == 42);
assertTrue("intStrKey should be int", jsonObject.getInt("intStrKey") == 43);
Expand Down Expand Up @@ -981,7 +981,7 @@
assertTrue("numberWithDecimals currently evaluates to double 299792.458",
jsonObject.get("numberWithDecimals").equals(new BigDecimal("299792.457999999984")));
Object obj = jsonObject.get("largeNumber");
assertTrue("largeNumber currently evaluates to BigInteger", new BigInteger("12345678901234567890").equals(obj));

Check warning on line 984 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=AaBi0LVynE9PClEwN0qC&open=AaBi0LVynE9PClEwN0qC&pullRequest=1075
// comes back as a double but loses precision
assertEquals("preciseNumber currently evaluates to double 0.2", 0.2, jsonObject.getDouble("preciseNumber"),
0.0);
Expand Down Expand Up @@ -1173,7 +1173,7 @@
jsonObject.put(key30, Double.valueOf(3.0));
jsonObject.put(key31, Double.valueOf(3.1));

assertTrue("3.0 should remain a double", jsonObject.getDouble(key30) == 3);

Check warning on line 1176 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=AaBi0LVynE9PClEwN0qQ&open=AaBi0LVynE9PClEwN0qQ&pullRequest=1075
assertTrue("3.1 should remain a double", jsonObject.getDouble(key31) == 3.1);

// turns 3.0 into 3.
Expand All @@ -1200,7 +1200,7 @@
BigInteger bigInteger = new BigInteger("123456789012345678901234567890");
JSONObject jsonObject0 = new JSONObject(bigInteger);
Object obj = jsonObject0.get("lowestSetBit");
assertTrue("JSONObject only has 1 value", jsonObject0.length() == 1);

Check warning on line 1203 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=AaBi0LVynE9PClEwN0qV&open=AaBi0LVynE9PClEwN0qV&pullRequest=1075
assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet", obj instanceof Integer);
assertTrue("this bigInteger lowestBitSet happens to be 1", obj.equals(1));

Expand Down Expand Up @@ -1491,7 +1491,7 @@
// validate JSON
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray0.toString());
List<?> docList = JsonPath.read(doc, "$");
assertTrue("expected 3 items", docList.size() == 3);

Check warning on line 1494 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=AaBi0LVynE9PClEwN0q3&open=AaBi0LVynE9PClEwN0q3&pullRequest=1075
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);
Expand Down Expand Up @@ -1523,7 +1523,7 @@
JSONArray jsonArray2 = new JSONArray(names);
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray2.toString());
docList = JsonPath.read(doc, "$");
assertTrue("expected 2 items", docList.size() == 2);

Check warning on line 1526 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=AaBi0LVznE9PClEwN0q_&open=AaBi0LVznE9PClEwN0q_&pullRequest=1075
assertTrue("expected to find publicString",
((List<?>) JsonPath.read(doc, "$[?(@=='publicString')]")).size() == 1);
assertTrue("expected to find publicInt", ((List<?>) JsonPath.read(doc, "$[?(@=='publicInt')]")).size() == 1);
Expand Down Expand Up @@ -1736,7 +1736,7 @@

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

Check warning on line 1739 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=AaBi0LVznE9PClEwN0rW&open=AaBi0LVznE9PClEwN0rW&pullRequest=1075
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);
Expand All @@ -1744,10 +1744,10 @@
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
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")));

Check warning on line 1747 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=AaBi0LVznE9PClEwN0re&open=AaBi0LVznE9PClEwN0re&pullRequest=1075
assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));

Check warning on line 1750 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=AaBi0LVznE9PClEwN0rh&open=AaBi0LVznE9PClEwN0rh&pullRequest=1075

jsonObject.remove("trueKey");
JSONObject expectedJsonObject = new JSONObject(expectedStr);
Expand Down Expand Up @@ -1790,16 +1790,16 @@

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

Check warning on line 1793 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=AaBi0LVznE9PClEwN0ri&open=AaBi0LVznE9PClEwN0ri&pullRequest=1075
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));

Check warning on line 1794 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=AaBi0LVznE9PClEwN0rj&open=AaBi0LVznE9PClEwN0rj&pullRequest=1075
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));

Check warning on line 1795 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=AaBi0LVznE9PClEwN0rk&open=AaBi0LVznE9PClEwN0rk&pullRequest=1075
assertTrue("expected 3 arrayKey items", ((List<?>) (JsonPath.read(doc, "$.arrayKey"))).size() == 3);
assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
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=AaBi0LVznE9PClEwN0rr&open=AaBi0LVznE9PClEwN0rr&pullRequest=1075
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=AaBi0LVznE9PClEwN0rw&open=AaBi0LVznE9PClEwN0rw&pullRequest=1075
Util.checkJSONObjectMaps(jsonObject);
}

Expand All @@ -1875,7 +1875,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 1 key item", ((List<?>) (JsonPath.read(doc, "$.key"))).size() == 1);

Check warning on line 1878 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=AaBi0LVznE9PClEwN0ry&open=AaBi0LVznE9PClEwN0ry&pullRequest=1075
assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0")));
Util.checkJSONObjectMaps(jsonObject);
}
Expand Down Expand Up @@ -1946,7 +1946,7 @@
@Test
public void wrapObject() {
// wrap(null) returns NULL
assertTrue("null wrap() incorrect", JSONObject.NULL == JSONObject.wrap(null));

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use assertSame instead.

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

// wrap(Integer) returns Integer
Integer in = Integer.valueOf(1);
Expand Down Expand Up @@ -1986,7 +1986,7 @@

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

Check warning on line 1989 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=AaBi0LVznE9PClEwN0sF&open=AaBi0LVznE9PClEwN0sF&pullRequest=1075
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")));
Expand All @@ -2009,7 +2009,7 @@
doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString());
assertTrue("expected 3 top level items", ((Map<?, ?>) (JsonPath.read(doc, "$"))).size() == 3);
assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1")));
assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2")));

Check warning on line 2012 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=AaBi0LVznE9PClEwN0sP&open=AaBi0LVznE9PClEwN0sP&pullRequest=1075
assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3")));
Util.checkJSONObjectsMaps(new ArrayList<JSONObject>(Arrays.asList(jsonObject, mapJsonObject)));
Util.checkJSONArrayMaps(jsonArray, jsonObject.getMapType());
Expand Down Expand Up @@ -2470,14 +2470,14 @@
assertTrue("optLongObject() should return default Long",
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",

Check warning on line 2473 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=AaBi0LVznE9PClEwN0sl&open=AaBi0LVznE9PClEwN0sl&pullRequest=1075
Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
assertTrue("optFloat() should return default float", 42.3f == jsonObject.optFloat("myKey", 42.3f));

Check warning on line 2475 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=AaBi0LVznE9PClEwN0sm&open=AaBi0LVznE9PClEwN0sm&pullRequest=1075
assertTrue("optFloatObject() should return default Float",
Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
assertTrue("optNumber() should return default Number",
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
assertTrue("optString() should return default string", "hi".equals(jsonObject.optString("hiKey", "hi")));

Check warning on line 2480 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=AaBi0LVznE9PClEwN0sp&open=AaBi0LVznE9PClEwN0sp&pullRequest=1075
Util.checkJSONObjectMaps(jsonObject);
}

Expand Down Expand Up @@ -2514,7 +2514,7 @@
assertTrue("optDouble() should return default double", 42.3d == jsonObject.optDouble("myKey", 42.3d));
assertTrue("optDoubleObject() should return default Double",
Double.valueOf(42.3d).equals(jsonObject.optDoubleObject("myKey", 42.3d)));
assertTrue("optFloat() should return default float", 42.3f == jsonObject.optFloat("myKey", 42.3f));

Check warning on line 2517 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=AaBi0LVznE9PClEwN0s2&open=AaBi0LVznE9PClEwN0s2&pullRequest=1075
assertTrue("optFloatObject() should return default Float",
Float.valueOf(42.3f).equals(jsonObject.optFloatObject("myKey", 42.3f)));
assertTrue("optNumber() should return default Number",
Expand All @@ -2532,10 +2532,10 @@
assertTrue("unexpected optBoolean value", jo.optBoolean("true", false) == true);
assertTrue("unexpected optBooleanObject value",
Boolean.valueOf(true).equals(jo.optBooleanObject("true", false)));
assertTrue("unexpected optBoolean value", jo.optBoolean("false", true) == false);

Check warning on line 2535 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=AaBi0LVznE9PClEwN0s8&open=AaBi0LVznE9PClEwN0s8&pullRequest=1075
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=AaBi0LVznE9PClEwN0s-&open=AaBi0LVznE9PClEwN0s-&pullRequest=1075
assertTrue("unexpected optIntegerObject value", Integer.valueOf(123).equals(jo.optIntegerObject("int", 0)));
assertTrue("unexpected optLong value", jo.optLong("int", 0) == 123l);
assertTrue("unexpected optLongObject value", Long.valueOf(123l).equals(jo.optLongObject("int", 0L)));
Expand Down Expand Up @@ -2728,7 +2728,7 @@
*/
@Test
public void toJSONArray() {
assertTrue("toJSONArray() with null names should be null", null == new JSONObject().toJSONArray(null));

Check warning on line 2731 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=AaBi0LVznE9PClEwN0tU&open=AaBi0LVznE9PClEwN0tU&pullRequest=1075
}

/**
Expand Down Expand Up @@ -2800,7 +2800,7 @@

JSONObject object = new JSONObject().put("somethingElse", "a value").put("someKey",
new JSONArray().put(new JSONObject().put("key1", new BrokenToString())));
try {

Check warning on line 2803 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=AaBi0LVznE9PClEwN0tX&open=AaBi0LVznE9PClEwN0tX&pullRequest=1075
object.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
Expand Down Expand Up @@ -2912,7 +2912,7 @@
public void equals() {
String str = "{\"key\":\"value\"}";
JSONObject aJsonObject = new JSONObject(str);
assertTrue("Same JSONObject should be equal to itself", aJsonObject.equals(aJsonObject));

Check warning on line 2915 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=AaBi0LVznE9PClEwN0tZ&open=AaBi0LVznE9PClEwN0tZ&pullRequest=1075
Util.checkJSONObjectMaps(aJsonObject);
}

Expand Down Expand Up @@ -3064,7 +3064,7 @@

List<?> key3Val2List = (List<?>) key3List.get(1);
assertTrue("key3 list val 2 should not be null", key3Val2List != null);
assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1);

Check warning on line 3067 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=AaBi0LVznE9PClEwN0t3&open=AaBi0LVznE9PClEwN0t3&pullRequest=1075
assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null);

// Assert that toMap() is a deep copy
Expand Down Expand Up @@ -3440,7 +3440,7 @@
* Tests for incorrect object/array nesting. See
* https://github.com/stleary/JSON-java/issues/654
*/
@Test(expected = JSONException.class)

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move assertions into separate method or use assertThrows or try-catch instead.

See more on https://sonarcloud.io/project/issues?id=stleary_JSON-java&issues=AaBi0LVznE9PClEwN0t_&open=AaBi0LVznE9PClEwN0t_&pullRequest=1075
public void issue654IncorrectNestingNoKey1() {
JSONObject json_input = new JSONObject("{{\"a\":0}}");
assertNotNull(json_input);
Expand Down Expand Up @@ -3517,6 +3517,24 @@
String jsonString = object.toString();
}

@Test
public void issue1056BrokenToStringStillReturnsNullFromToString() {
JSONObject jo = new JSONObject().put("k", new BrokenToString());
assertNull(jo.toString());
}

@Test
public void issue1056DiamondSharedChildStillSerializes() {
JSONObject parent = new JSONObject();
JSONObject child = new JSONObject().put("x", 1);
parent.put("a", child);
parent.put("b", child);
String json = parent.toString();
assertTrue(json.contains("\"a\":"));
assertTrue(json.contains("\"b\":"));
assertTrue(json.contains("\"x\":1"));
}

@Test(expected = JSONException.class)
public void testCircularReferenceMultipleLevel() {
HashMap<String, Object> inside = new HashMap<>();
Expand Down
Loading