33import org .json .JSONArray ;
44import org .json .JSONException ;
55import org .json .JSONObject ;
6+ import org .skyscreamer .jsonassert .comparator .JSONComparator ;
67
78/**
89 * <p>A set of assertion methods useful for writing tests methods that return JSON.</p>
@@ -47,8 +48,7 @@ private JSONAssert() {}
4748 * @throws JSONException
4849 */
4950 public static void assertEquals (String expectedStr , JSONObject actual , boolean strict )
50- throws JSONException
51- {
51+ throws JSONException {
5252 assertEquals (expectedStr , actual , strict ? JSONCompareMode .STRICT : JSONCompareMode .LENIENT );
5353 }
5454
@@ -62,8 +62,7 @@ public static void assertEquals(String expectedStr, JSONObject actual, boolean s
6262 * @throws JSONException
6363 */
6464 public static void assertEquals (String expectedStr , JSONObject actual , JSONCompareMode compareMode )
65- throws JSONException
66- {
65+ throws JSONException {
6766 Object expected = JSONParser .parseJSON (expectedStr );
6867 if (expected instanceof JSONObject ) {
6968 assertEquals ((JSONObject )expected , actual , compareMode );
@@ -83,8 +82,7 @@ public static void assertEquals(String expectedStr, JSONObject actual, JSONCompa
8382 * @throws JSONException
8483 */
8584 public static void assertEquals (String expectedStr , JSONArray actual , boolean strict )
86- throws JSONException
87- {
85+ throws JSONException {
8886 assertEquals (expectedStr , actual , strict ? JSONCompareMode .STRICT : JSONCompareMode .LENIENT );
8987 }
9088
@@ -98,8 +96,7 @@ public static void assertEquals(String expectedStr, JSONArray actual, boolean st
9896 * @throws JSONException
9997 */
10098 public static void assertEquals (String expectedStr , JSONArray actual , JSONCompareMode compareMode )
101- throws JSONException
102- {
99+ throws JSONException {
103100 Object expected = JSONParser .parseJSON (expectedStr );
104101 if (expected instanceof JSONArray ) {
105102 assertEquals ((JSONArray )expected , actual , compareMode );
@@ -119,8 +116,7 @@ public static void assertEquals(String expectedStr, JSONArray actual, JSONCompar
119116 * @throws JSONException
120117 */
121118 public static void assertEquals (String expectedStr , String actualStr , boolean strict )
122- throws JSONException
123- {
119+ throws JSONException {
124120 assertEquals (expectedStr , actualStr , strict ? JSONCompareMode .STRICT : JSONCompareMode .LENIENT );
125121 }
126122
@@ -134,14 +130,30 @@ public static void assertEquals(String expectedStr, String actualStr, boolean st
134130 * @throws JSONException
135131 */
136132 public static void assertEquals (String expectedStr , String actualStr , JSONCompareMode compareMode )
137- throws JSONException
138- {
133+ throws JSONException {
139134 JSONCompareResult result = JSONCompare .compareJSON (expectedStr , actualStr , compareMode );
140135 if (result .failed ()) {
141136 throw new AssertionError (result .getMessage ());
142137 }
143138 }
144139
140+ /**
141+ * Asserts that the json string provided matches the expected string. If it isn't it throws an
142+ * {@link AssertionError}.
143+ *
144+ * @param expectedStr Expected JSON string
145+ * @param actualStr String to compare
146+ * @param comparator Comparator
147+ * @throws JSONException
148+ */
149+ public static void assertEquals (String expectedStr , String actualStr , JSONComparator comparator )
150+ throws JSONException {
151+ JSONCompareResult result = JSONCompare .compareJSON (expectedStr , actualStr , comparator );
152+ if (result .failed ()) {
153+ throw new AssertionError (result .getMessage ());
154+ }
155+ }
156+
145157 /**
146158 * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an
147159 * {@link AssertionError}.
@@ -152,8 +164,7 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar
152164 * @throws JSONException
153165 */
154166 public static void assertEquals (JSONObject expected , JSONObject actual , boolean strict )
155- throws JSONException
156- {
167+ throws JSONException {
157168 assertEquals (expected , actual , strict ? JSONCompareMode .STRICT : JSONCompareMode .LENIENT );
158169 }
159170
@@ -185,8 +196,7 @@ public static void assertEquals(JSONObject expected, JSONObject actual, JSONComp
185196 * @throws JSONException
186197 */
187198 public static void assertEquals (JSONArray expected , JSONArray actual , boolean strict )
188- throws JSONException
189- {
199+ throws JSONException {
190200 assertEquals (expected , actual , strict ? JSONCompareMode .STRICT : JSONCompareMode .LENIENT );
191201 }
192202
@@ -200,8 +210,7 @@ public static void assertEquals(JSONArray expected, JSONArray actual, boolean st
200210 * @throws JSONException
201211 */
202212 public static void assertEquals (JSONArray expected , JSONArray actual , JSONCompareMode compareMode )
203- throws JSONException
204- {
213+ throws JSONException {
205214 JSONCompareResult result = JSONCompare .compareJSON (expected , actual , compareMode );
206215 if (result .failed ()) {
207216 throw new AssertionError (result .getMessage ());
0 commit comments