diff --git a/pom.xml b/pom.xml index 6c369586..3970acdd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.skyscreamer jsonassert - 1.5.1-SNAPSHOT + 1.5.0 jar JSONassert @@ -57,9 +57,14 @@ junit junit - 4.13.1 + 4.10 test + + com.alibaba + fastjson + 1.1.72.android + @@ -67,7 +72,7 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.1 + 3.8.1 1.6 1.6 diff --git a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java index b963be9f..b6810bc8 100644 --- a/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java +++ b/src/main/java/org/skyscreamer/jsonassert/JSONCompare.java @@ -10,10 +10,11 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package org.skyscreamer.jsonassert; +import com.alibaba.fastjson.JSON; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -37,11 +38,12 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) { /** * Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of * the comparison. + * * @param expectedStr Expected JSON string - * @param actualStr JSON string to compare - * @param comparator Comparator to use + * @param actualStr JSON string to compare + * @param comparator Comparator to use * @return result of the comparison - * @throws JSONException JSON parsing error + * @throws JSONException JSON parsing error * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) @@ -50,26 +52,23 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { return compareJSON((JSONObject) expected, (JSONObject) actual, comparator); - } - else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { - return compareJSON((JSONArray)expected, (JSONArray)actual, comparator); - } - else if (expected instanceof JSONString && actual instanceof JSONString) { + } else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { + return compareJSON((JSONArray) expected, (JSONArray) actual, comparator); + } else if (expected instanceof JSONString && actual instanceof JSONString) { return compareJson((JSONString) expected, (JSONString) actual); - } - else if (expected instanceof JSONObject) { + } else if (expected instanceof JSONObject) { return new JSONCompareResult().fail("", expected, actual); - } - else { + } else { return new JSONCompareResult().fail("", expected, actual); } } - /** + /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. - * @param expected expected json object - * @param actual actual json object + * + * @param expected expected json object + * @param actual actual json object * @param comparator comparator to use * @return result of the comparison * @throws JSONException JSON parsing error @@ -82,8 +81,9 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu /** * Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of * the comparison. - * @param expected expected json array - * @param actual actual json array + * + * @param expected expected json array + * @param actual actual json array * @param comparator comparator to use * @return result of the comparison * @throws JSONException JSON parsing error @@ -106,11 +106,12 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO final String expectedJson = expected.toJSONString(); final String actualJson = actual.toJSONString(); if (!expectedJson.equals(actualJson)) { - result.fail(""); + result.fail(""); } return result; } + //CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 /** * Compares JSON string provided to the expected JSON string, and returns the results of the comparison. * @@ -120,11 +121,17 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO * @return result of the comparison * @throws JSONException JSON parsing error */ - public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) + public static JSONCompareResult compareJSON(String expectedStr, String + actualStr, JSONCompareMode mode) throws JSONException { - return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); + if ((mode == JSONCompareMode.STRICT) && (!isJSON(expectedStr) || !isJSON(actualStr))) { + return new JSONCompareResult().fail("", expectedStr, actualStr); + } + return compareJSON(expectedStr, actualStr, + getComparatorForMode(mode)); } + /** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. * @@ -154,4 +161,22 @@ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual return compareJSON(expected, actual, getComparatorForMode(mode)); } + //CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 + + /** + * Judge if the given string can be converted into JSON object. + * + * @param str JSON string + * @return if the string can be convert into JSON Object + */ + public static boolean isJSON(String str) { + boolean result = false; + try { + Object obj = JSON.parse(str); + result = true; + } catch (Exception e) { + result = false; + } + return result; + } } diff --git a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java index 35a62981..27ee9c5e 100644 --- a/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java +++ b/src/main/java/org/skyscreamer/jsonassert/comparator/ArraySizeComparator.java @@ -53,7 +53,7 @@ public class ArraySizeComparator extends DefaultComparator { * Create new ArraySizeComparator. * * @param mode - * comparison mode, has no impact on ArraySizeComparator but is + * FindBugs comparison mode, has no impact on ArraySizeComparator but is * used by instance of superclass DefaultComparator to control * comparison of JSON items other than arrays. */ diff --git a/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java b/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java new file mode 100644 index 00000000..224e251f --- /dev/null +++ b/src/test/java/org/skyscreamer/jsonassert/comparator/CrashedJSONCompareTest.java @@ -0,0 +1,31 @@ +package org.skyscreamer.jsonassert.comparator; + +import org.json.JSONException; +import org.junit.Test; +import org.skyscreamer.jsonassert.JSONCompare; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.skyscreamer.jsonassert.JSONCompareResult; + +import static org.junit.Assert.assertTrue; +import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; + +//CS304 Issue link:https://github.com/skyscreamer/JSONassert/issues/103 +public class CrashedJSONCompareTest { + @Test + public void shouldJudgeValidJson() throws JSONException { + String validJson = "{\"id\":\"abc\"}"; + String crashedJson1 = "{\"id\":'abc'}"; + String crashedJson2 = "{\"id\":abc}"; + assertTrue(JSONCompare.isJSON(validJson)); + assertTrue(JSONCompare.isJSON(crashedJson1)); + assertTrue(!JSONCompare.isJSON(crashedJson2)); + } + @Test + public void shouldFailOnCrashedJson() throws JSONException{ + String validJson = "{\"id\":\"abc\"}"; + String crashedJson = "{\"id\":abc}"; + JSONCompareResult jsonCompareResult = compareJSON(validJson, + crashedJson, JSONCompareMode.STRICT); + assertTrue(jsonCompareResult.failed()); + } +}