Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.comparator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareResult;
import java.util.*;
import static org.skyscreamer.jsonassert.comparator.JSONCompareUtil.*;
/**
* This class provides a skeletal implementation of the {@link JSONComparator}
* interface, to minimize the effort required to implement this interface.
*/
public abstract class AbstractComparator implements JSONComparator {
/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
* @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 {
JSONCompareResult result = new JSONCompareResult();
compareJSON("", expected, actual, result);
return result;
}
/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
* @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 {
JSONCompareResult result = new JSONCompareResult();
compareJSONArray("", expected, actual, result);
return result;
}
protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
Set actualKeys = getKeys(actual);
for (String key : actualKeys) {
if (!expected.has(key)) {
result.unexpected(prefix, key);
}
}
}
protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException {
Set expectedKeys = getKeys(expected);
for (String key : expectedKeys) {
Object expectedValue = expected.get(key);
if (actual.has(key)) {
Object actualValue = actual.get(key);
compareValues(qualify(prefix, key), expectedValue, actualValue, result);
} else {
result.missing(prefix, key);
}
}
}
protected void compareJSONArrayOfJsonObjects(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
String uniqueKey = findUniqueKey(expected);
if (uniqueKey == null || !isUsableAsUniqueKey(uniqueKey, actual)) {
// An expensive last resort
recursivelyCompareJSONArray(key, expected, actual, result);
return;
}
Map