forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONCompare.java
More file actions
157 lines (144 loc) · 6.3 KB
/
JSONCompare.java
File metadata and controls
157 lines (144 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* 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;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
/**
* Provides API to compare two JSON entities. This is the backend to {@link JSONAssert}, but it can
* be programmed against directly to access the functionality. (eg, to make something that works with a
* non-JUnit test framework)
*/
public final class JSONCompare {
private JSONCompare() {
}
private static JSONComparator getComparatorForMode(JSONCompareMode mode) {
return new DefaultComparator(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
* @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 {
Object expected = JSONParser.parseJSON(expectedStr);
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) {
return compareJson((JSONString) expected, (JSONString) actual);
}
else if (expected instanceof JSONObject) {
return new JSONCompareResult().fail("", expected, actual);
}
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 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 {
return comparator.compareJSON(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 array
* @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 {
return comparator.compareJSON(expected, actual);
}
/**
* Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the
* {@link org.json.JSONString#toJSONString()} are equal.
*
* @param expected Expected {@code JSONstring}
* @param actual {@code JSONstring} to compare
* @return result of the comparison
*/
public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual) {
final JSONCompareResult result = new JSONCompareResult();
final String expectedJson = expected.toJSONString();
final String actualJson = actual.toJSONString();
if (!expectedJson.equals(actualJson)) {
result.fail("");
}
return result;
}
/**
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
*
* @param expectedStr Expected JSON string
* @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 {
return compareJSON(expectedStr, actualStr, getComparatorForMode(mode));
}
/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
* @param expected Expected JSONObject
* @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 {
return compareJSON(expected, actual, getComparatorForMode(mode));
}
/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
* @param expected Expected JSONArray
* @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 {
return compareJSON(expected, actual, getComparatorForMode(mode));
}
}