Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JS

protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
for (int i = 0; i < expected.length(); ++i) {
Object expectedValue = expected.get(i);
Object actualValue = actual.get(i);
Object expectedValue = JSONCompareUtil.getObjectOrNull(expected, i);
Object actualValue = JSONCompareUtil.getObjectOrNull(actual, i);
compareValues(key + "[" + i + "]", expectedValue, actualValue, result);
}
}
Expand All @@ -138,10 +138,17 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA
JSONCompareResult result) throws JSONException {
Set<Integer> matched = new HashSet<Integer>();
for (int i = 0; i < expected.length(); ++i) {
Object expectedElement = expected.get(i);
Object expectedElement = JSONCompareUtil.getObjectOrNull(expected, i);
boolean matchFound = false;
for (int j = 0; j < actual.length(); ++j) {
Object actualElement = actual.get(j);
Object actualElement = JSONCompareUtil.getObjectOrNull(actual, j);
if (expectedElement == actualElement) {
matchFound = true;
break;
}
if ((expectedElement == null && actualElement != null) || (expectedElement != null && actualElement == null)) {
continue;
}
if (matched.contains(j) || !actualElement.getClass().equals(expectedElement.getClass())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
if (expectedValue == actualValue) {
return;
}
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
result.fail(prefix, expectedValue, actualValue);
}
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
result.fail(prefix, expectedValue, actualValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,23 @@ public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) thr
public static List<Object> jsonArrayToList(JSONArray expected) throws JSONException {
List<Object> jsonObjects = new ArrayList<Object>(expected.length());
for (int i = 0; i < expected.length(); ++i) {
jsonObjects.add(expected.get(i));
jsonObjects.add(getObjectOrNull(expected, i));
}
return jsonObjects;
}

/**
* Returns the value present in the given index position. If null value is present, it will return null
*
* @param jsonArray the JSON array to get value from
* @param index index of object to retrieve
* @return value at the given index position
* @throws JSONException JSON parsing error
*/
public static Object getObjectOrNull(JSONArray jsonArray, int index) throws JSONException {
return jsonArray.isNull(index) ? null : jsonArray.get(index);
}

/**
* Returns whether all of the elements in the given array are simple values.
*
Expand All @@ -136,7 +148,7 @@ public static List<Object> jsonArrayToList(JSONArray expected) throws JSONExcept
*/
public static boolean allSimpleValues(JSONArray array) throws JSONException {
for (int i = 0; i < array.length(); ++i) {
if (!isSimpleValue(array.get(i))) {
if (!array.isNull(i) && !isSimpleValue(array.get(i))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.skyscreamer.jsonassert;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

public class JSONArrayWithNullTest {
@Test
public void testJSONArrayWithNullValue() throws JSONException {
JSONArray jsonArray1 = getJSONArray1();
JSONArray jsonArray2 = getJSONArray2();

JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
}

@Test
public void testJSONArrayWithNullValueAndJsonObject() throws JSONException {
JSONArray jsonArray1 = getJSONArray1();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("hey", "value");

JSONArray jsonArray2 = getJSONArray2();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("hey", "value");

JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
}

private JSONArray getJSONArray1() {
JSONArray jsonArray1 = new JSONArray();
jsonArray1.put(1);
jsonArray1.put(null);
jsonArray1.put(3);
jsonArray1.put(2);
return jsonArray1;
}

private JSONArray getJSONArray2() {
JSONArray jsonArray1 = new JSONArray();
jsonArray1.put(1);
jsonArray1.put(null);
jsonArray1.put(3);
jsonArray1.put(2);
return jsonArray1;
}
}