Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cs304 pull request for issue 103 #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>JSONassert</name>
Expand Down Expand Up @@ -57,17 +57,22 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.72.android</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
Expand Down
67 changes: 46 additions & 21 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}