Skip to content

Commit 1a56edd

Browse files
committed
Merge pull request skyscreamer#38 from acourtneybrown/master
Added support for comparing strings and numbers as represented in JSON strings
2 parents 750610b + 92d626e commit 1a56edd

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONCompare.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.json.JSONArray;
44
import org.json.JSONException;
55
import org.json.JSONObject;
6+
import org.json.JSONString;
67
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
78
import org.skyscreamer.jsonassert.comparator.JSONComparator;
89

@@ -39,6 +40,9 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr
3940
else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) {
4041
return compareJSON((JSONArray)expected, (JSONArray)actual, comparator);
4142
}
43+
else if (expected instanceof JSONString && actual instanceof JSONString) {
44+
return compareJson((JSONString) expected, (JSONString) actual);
45+
}
4246
else if (expected instanceof JSONObject) {
4347
return new JSONCompareResult().fail("", expected, actual);
4448
}
@@ -47,7 +51,7 @@ else if (expected instanceof JSONObject) {
4751
}
4852
}
4953

50-
/**
54+
/**
5155
* Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of
5256
* the comparison.
5357
* @param expected expected json object
@@ -75,6 +79,23 @@ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual
7579
return comparator.compareJSON(expected, actual);
7680
}
7781

82+
/**
83+
* Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the
84+
* {@link org.json.JSONString#toJSONString()} are equal.
85+
*
86+
* @param expected Expected {@code JSONstring}
87+
* @param actual {@code JSONstring} to compare
88+
*/
89+
public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual) {
90+
final JSONCompareResult result = new JSONCompareResult();
91+
final String expectedJson = expected.toJSONString();
92+
final String actualJson = actual.toJSONString();
93+
if (!expectedJson.equals(actualJson)) {
94+
result.fail("");
95+
}
96+
return result;
97+
}
98+
7899
/**
79100
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
80101
*

src/main/java/org/skyscreamer/jsonassert/JSONParser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import org.json.JSONArray;
44
import org.json.JSONException;
55
import org.json.JSONObject;
6+
import org.json.JSONString;
67

78
/**
89
* Simple JSON parsing utility.
910
*/
1011
public class JSONParser {
12+
// regular expression to match a number in JSON format. see http://www.json.org/fatfree.html.
13+
// "A number can be represented as integer, real, or floating point. JSON does not support octal or hex
14+
// ... [or] NaN or Infinity".
15+
private static final String NUMBER_REGEX = "-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?";
16+
1117
private JSONParser() {}
1218

1319
/**
@@ -18,12 +24,20 @@ private JSONParser() {}
1824
* @return JSONObject or JSONArray
1925
* @throws JSONException
2026
*/
21-
public static Object parseJSON(String s) throws JSONException {
27+
public static Object parseJSON(final String s) throws JSONException {
2228
if (s.trim().startsWith("{")) {
2329
return new JSONObject(s);
2430
}
2531
else if (s.trim().startsWith("[")) {
2632
return new JSONArray(s);
33+
} else if (s.trim().startsWith("\"")
34+
|| s.trim().matches(NUMBER_REGEX)) {
35+
return new JSONString() {
36+
@Override
37+
public String toJSONString() {
38+
return s;
39+
}
40+
};
2741
}
2842
throw new JSONException("Unparsable JSON string: " + s);
2943
}

src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@
1414
* Unit tests for {@link JSONAssert}
1515
*/
1616
public class JSONAssertTest {
17+
@Test
18+
public void testString() throws JSONException {
19+
testPass("\"Joe\"", "\"Joe\"", STRICT);
20+
testPass("\"Joe\"", "\"Joe\"", LENIENT);
21+
testPass("\"Joe\"", "\"Joe\"", NON_EXTENSIBLE);
22+
testPass("\"Joe\"", "\"Joe\"", STRICT_ORDER);
23+
testFail("\"Joe\"", "\"Joe1\"", STRICT);
24+
testFail("\"Joe\"", "\"Joe2\"", LENIENT);
25+
testFail("\"Joe\"", "\"Joe3\"", NON_EXTENSIBLE);
26+
testFail("\"Joe\"", "\"Joe4\"", STRICT_ORDER);
27+
}
28+
29+
@Test
30+
public void testNumber() throws JSONException {
31+
testPass("123", "123", STRICT);
32+
testPass("123", "123", LENIENT);
33+
testPass("123", "123", NON_EXTENSIBLE);
34+
testPass("123", "123", STRICT_ORDER);
35+
testFail("123", "1231", STRICT);
36+
testFail("123", "1232", LENIENT);
37+
testFail("123", "1233", NON_EXTENSIBLE);
38+
testFail("123", "1234", STRICT_ORDER);
39+
testPass("0", "0", STRICT);
40+
testPass("-1", "-1", STRICT);
41+
testPass("0.1", "0.1", STRICT);
42+
testPass("1.2e5", "1.2e5", STRICT);
43+
testPass("20.4e-1", "20.4e-1", STRICT);
44+
testFail("310.1e-1", "31.01", STRICT); // should fail though numbers are the same?
45+
}
46+
1747
@Test
1848
public void testSimple() throws JSONException {
1949
testPass("{id:1}", "{id:1}", STRICT);

0 commit comments

Comments
 (0)