Skip to content

Commit d889f22

Browse files
authored
Merge pull request #111 from suraj1291993/master
Fixes for issue #105
2 parents abd09d8 + d46714c commit d889f22

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

src/main/java/org/skyscreamer/jsonassert/comparator/AbstractComparator.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JS
124124

125125
protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
126126
for (int i = 0; i < expected.length(); ++i) {
127-
Object expectedValue = expected.get(i);
128-
Object actualValue = actual.get(i);
127+
Object expectedValue = JSONCompareUtil.getObjectOrNull(expected, i);
128+
Object actualValue = JSONCompareUtil.getObjectOrNull(actual, i);
129129
compareValues(key + "[" + i + "]", expectedValue, actualValue, result);
130130
}
131131
}
@@ -138,10 +138,17 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA
138138
JSONCompareResult result) throws JSONException {
139139
Set<Integer> matched = new HashSet<Integer>();
140140
for (int i = 0; i < expected.length(); ++i) {
141-
Object expectedElement = expected.get(i);
141+
Object expectedElement = JSONCompareUtil.getObjectOrNull(expected, i);
142142
boolean matchFound = false;
143143
for (int j = 0; j < actual.length(); ++j) {
144-
Object actualElement = actual.get(j);
144+
Object actualElement = JSONCompareUtil.getObjectOrNull(actual, j);
145+
if (expectedElement == actualElement) {
146+
matchFound = true;
147+
break;
148+
}
149+
if ((expectedElement == null && actualElement != null) || (expectedElement != null && actualElement == null)) {
150+
continue;
151+
}
145152
if (matched.contains(j) || !actualElement.getClass().equals(expectedElement.getClass())) {
146153
continue;
147154
}

src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
5050
@Override
5151
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
5252
throws JSONException {
53+
if (expectedValue == actualValue) {
54+
return;
55+
}
56+
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
57+
result.fail(prefix, expectedValue, actualValue);
58+
}
5359
if (areNumbers(expectedValue, actualValue)) {
5460
if (areNotSameDoubles(expectedValue, actualValue)) {
5561
result.fail(prefix, expectedValue, actualValue);

src/main/java/org/skyscreamer/jsonassert/comparator/JSONCompareUtil.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,23 @@ public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) thr
121121
public static List<Object> jsonArrayToList(JSONArray expected) throws JSONException {
122122
List<Object> jsonObjects = new ArrayList<Object>(expected.length());
123123
for (int i = 0; i < expected.length(); ++i) {
124-
jsonObjects.add(expected.get(i));
124+
jsonObjects.add(getObjectOrNull(expected, i));
125125
}
126126
return jsonObjects;
127127
}
128128

129+
/**
130+
* Returns the value present in the given index position. If null value is present, it will return null
131+
*
132+
* @param jsonArray the JSON array to get value from
133+
* @param index index of object to retrieve
134+
* @return value at the given index position
135+
* @throws JSONException JSON parsing error
136+
*/
137+
public static Object getObjectOrNull(JSONArray jsonArray, int index) throws JSONException {
138+
return jsonArray.isNull(index) ? null : jsonArray.get(index);
139+
}
140+
129141
/**
130142
* Returns whether all of the elements in the given array are simple values.
131143
*
@@ -136,7 +148,7 @@ public static List<Object> jsonArrayToList(JSONArray expected) throws JSONExcept
136148
*/
137149
public static boolean allSimpleValues(JSONArray array) throws JSONException {
138150
for (int i = 0; i < array.length(); ++i) {
139-
if (!isSimpleValue(array.get(i))) {
151+
if (!array.isNull(i) && !isSimpleValue(array.get(i))) {
140152
return false;
141153
}
142154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.skyscreamer.jsonassert;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
6+
import org.junit.Test;
7+
8+
public class JSONArrayWithNullTest {
9+
@Test
10+
public void testJSONArrayWithNullValue() throws JSONException {
11+
JSONArray jsonArray1 = getJSONArray1();
12+
JSONArray jsonArray2 = getJSONArray2();
13+
14+
JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
15+
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
16+
}
17+
18+
@Test
19+
public void testJSONArrayWithNullValueAndJsonObject() throws JSONException {
20+
JSONArray jsonArray1 = getJSONArray1();
21+
JSONObject jsonObject1 = new JSONObject();
22+
jsonObject1.put("hey", "value");
23+
24+
JSONArray jsonArray2 = getJSONArray2();
25+
JSONObject jsonObject2 = new JSONObject();
26+
jsonObject2.put("hey", "value");
27+
28+
JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
29+
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
30+
}
31+
32+
private JSONArray getJSONArray1() {
33+
JSONArray jsonArray1 = new JSONArray();
34+
jsonArray1.put(1);
35+
jsonArray1.put(null);
36+
jsonArray1.put(3);
37+
jsonArray1.put(2);
38+
return jsonArray1;
39+
}
40+
41+
private JSONArray getJSONArray2() {
42+
JSONArray jsonArray1 = new JSONArray();
43+
jsonArray1.put(1);
44+
jsonArray1.put(null);
45+
jsonArray1.put(3);
46+
jsonArray1.put(2);
47+
return jsonArray1;
48+
}
49+
}

0 commit comments

Comments
 (0)