diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java index 41b0ea0f..4f68e2d1 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java @@ -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); } } @@ -138,10 +138,17 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA JSONCompareResult result) throws JSONException { Set matched = new HashSet(); 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; } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java index d7a210dc..bc71eae4 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java @@ -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); diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java index 617df012..a6bcc4ce 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java @@ -121,11 +121,23 @@ public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) thr public static List jsonArrayToList(JSONArray expected) throws JSONException { List jsonObjects = new ArrayList(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. * @@ -136,7 +148,7 @@ public static List 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; } } diff --git a/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java b/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java new file mode 100644 index 00000000..c5a5da5f --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/JSONArrayWithNullTest.java @@ -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; + } +}