Skip to content

Commit

Permalink
Finally moving to official org.json implementation! (#194)
Browse files Browse the repository at this point in the history
* Replaced org.json with stleary/JSON-java library.

* Checkpoint. Beginning to removing JSONException imports since it is now a RuntimeException.

* Completed removal of explicit throws JSONException since it is now a RuntimeException.

* Updated README to account for new dependency and removal of old.

* One last unnecessary JSONException reference.

* Library change breaks some behavior and is a major shift. Incrementing major version.
  • Loading branch information
carterpage authored Jul 19, 2024
1 parent b1750da commit 8b9be3c
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 431 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

Version 2.0.0 - TBD
-------------------
- TODO - placeholder

Version 1.5.3 - 6/28/2024
-------------------------
- Revert Java release version from 21 to 8 due to breaking older compilers.
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ To use, [download the JAR](https://github.com/skyscreamer/JSONassert/releases) o
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
<version>2.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

Expand All @@ -97,16 +97,15 @@ Who uses JSONassert?
+ [GroupDocs](http://groupdocs.com/)
+ [Shazam](http://www.shazam.com/)
+ [Thucydides](http://thucydides.net/)
+ [and over a thousand more](https://mvnrepository.com/artifact/org.skyscreamer/jsonassert)...

* * *

org.json
--------

This implementation uses a clean-room implementation of the org.json
library implemented for the Android system, released under the Apache 2.0 license. See
[com.vaadin.external.google:android-json](http://search.maven.org/#artifactdetails%7Ccom.vaadin.external.google%7Candroid-json%7C0.0.20131108.vaadin1%7Cjar)
That jar does **not** include the org.json.JSONString interface, so a new implementation of that interface is added to this source.
As of v2, JSONAssert uses @stleary's [JSON-java](https://github.com/stleary/JSON-java) implementation of org.json, the
most commonly used reference implementation for JSON in Java.

Resources
---------
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JSONassert</name>
Expand Down Expand Up @@ -47,9 +47,9 @@
<!-- For JSONAssert -->
<dependencies>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/json/JSONString.java

This file was deleted.

200 changes: 56 additions & 144 deletions src/main/java/org/skyscreamer/jsonassert/JSONAssert.java

Large diffs are not rendered by default.

25 changes: 6 additions & 19 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.skyscreamer.jsonassert;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
Expand All @@ -41,11 +40,9 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) {
* @param actualStr JSON string to compare
* @param comparator Comparator to use
* @return result of the comparison
* @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)
throws JSONException {
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) {
Object expected = JSONParser.parseJSON(expectedStr);
Object actual = JSONParser.parseJSON(actualStr);
if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) {
Expand All @@ -72,10 +69,8 @@ else if (expected instanceof JSONObject) {
* @param actual actual json object
* @param comparator comparator to use
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator)
throws JSONException {
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator) {
return comparator.compareJSON(expected, actual);
}

Expand All @@ -86,10 +81,8 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu
* @param actual actual json array
* @param comparator comparator to use
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONComparator comparator)
throws JSONException {
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONComparator comparator) {
return comparator.compareJSON(expected, actual);
}

Expand Down Expand Up @@ -118,10 +111,8 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO
* @param actualStr JSON string to compare
* @param mode Defines comparison behavior
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode)
throws JSONException {
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) {
return compareJSON(expectedStr, actualStr, getComparatorForMode(mode));
}

Expand All @@ -132,10 +123,8 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr
* @param actual JSONObject to compare
* @param mode Defines comparison behavior
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode)
throws JSONException {
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode) {
return compareJSON(expected, actual, getComparatorForMode(mode));
}

Expand All @@ -147,10 +136,8 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu
* @param actual JSONArray to compare
* @param mode Defines comparison behavior
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode)
throws JSONException {
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode) {
return compareJSON(expected, actual, getComparatorForMode(mode));
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/skyscreamer/jsonassert/JSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ private JSONParser() {}
*
* @param s Raw JSON string to be parsed
* @return JSONObject or JSONArray
* @throws JSONException JSON parsing error
*/
public static Object parseJSON(final String s) throws JSONException {
public static Object parseJSON(final String s) {
if (s.trim().startsWith("{")) {
return new JSONObject(s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.skyscreamer.jsonassert.comparator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareResult;

Expand All @@ -42,10 +41,9 @@ public AbstractComparator() {
*
* @param expected Expected JSONObject
* @param actual JSONObject to compare
* @throws JSONException JSON parsing error
*/
@Override
public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException {
public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) {
JSONCompareResult result = new JSONCompareResult();
compareJSON("", expected, actual, result);
return result;
Expand All @@ -56,10 +54,9 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua
*
* @param expected Expected JSONArray
* @param actual JSONArray to compare
* @throws JSONException JSON parsing error
*/
@Override
public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) throws JSONException {
public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) {
JSONCompareResult result = new JSONCompareResult();
compareJSONArray("", expected, actual, result);
return result;
Expand All @@ -86,9 +83,8 @@ protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject exp
* @param expected
* @param actual
* @param result
* @throws JSONException
*/
protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException {
protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
Set<String> expectedKeys = getKeys(expected);
for (String key : expectedKeys) {
Object expectedValue = expected.get(key);
Expand All @@ -101,7 +97,7 @@ protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject exp
}
}

protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) {
String uniqueKey = findUniqueKey(expected);
if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) {
// An expensive last resort
Expand All @@ -126,7 +122,7 @@ protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSO
}
}

protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) {
Map<Object, Integer> expectedCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(expected));
Map<Object, Integer> actualCount = JSONCompareUtil.getCardinalityMap(jsonArrayToList(actual));
for (Object o : expectedCount.keySet()) {
Expand All @@ -144,7 +140,7 @@ protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JS
}
}

protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) {
for (int i = 0; i < expected.length(); ++i) {
Object expectedValue = JSONCompareUtil.getObjectOrNull(expected, i);
Object actualValue = JSONCompareUtil.getObjectOrNull(actual, i);
Expand All @@ -157,7 +153,7 @@ protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, J
// This is expensive (O(n^2) -- yuck), but may be the only resort for some cases with loose array ordering, and no
// easy way to uniquely identify each element.
protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONArray actual,
JSONCompareResult result) throws JSONException {
JSONCompareResult result) {
Set<Integer> matched = new HashSet<Integer>();
for (int i = 0; i < expected.length(); ++i) {
Object expectedElement = JSONCompareUtil.getObjectOrNull(expected, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.text.MessageFormat;

import org.json.JSONArray;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;

Expand Down Expand Up @@ -69,7 +68,7 @@ public ArraySizeComparator(JSONCompareMode mode) {
*/
@Override
public void compareJSONArray(String prefix, JSONArray expected,
JSONArray actual, JSONCompareResult result) throws JSONException {
JSONArray actual, JSONCompareResult result) {
String arrayPrefix = prefix + "[]";
if (expected.length() < 1 || expected.length() > 2) {
result.fail(MessageFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package org.skyscreamer.jsonassert.comparator;

import org.json.JSONException;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
Expand All @@ -33,7 +32,7 @@ public CustomComparator(JSONCompareMode mode, Customization... customizations)
}

@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException {
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {
Customization customization = getCustomization(prefix);
if (customization != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.skyscreamer.jsonassert.comparator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
Expand All @@ -36,8 +35,7 @@ public DefaultComparator(JSONCompareMode mode) {
}

@Override
public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result)
throws JSONException {
public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
// Check that actual contains all the expected values
checkJsonObjectKeysExpectedInActual(prefix, expected, actual, result);

Expand All @@ -48,8 +46,7 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
}

@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {
if (expectedValue == actualValue) {
return;
}
Expand All @@ -74,8 +71,7 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu
}

@Override
public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result)
throws JSONException {
public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) {
if (expected.length() != actual.length()) {
result.fail(prefix + "[]: Expected " + expected.length() + " values but got " + actual.length());
return;
Expand Down
Loading

0 comments on commit 8b9be3c

Please sign in to comment.