Skip to content

Commit 8af86e3

Browse files
authored
Merge pull request skyscreamer#76 from picimako/feature/various-updates
Feature/various updates
2 parents f88b669 + b4307f9 commit 8af86e3

File tree

11 files changed

+197
-57
lines changed

11 files changed

+197
-57
lines changed

‎src/main/java/org/json/JSONString.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* the behavior of JSONObject.toString(), JSONArray.toString(), and JSONWriter.value(Object).
66
* The toJSONString method will be used instead of the default behavior of using the
77
* Object's toString() method and quoting the result.
8-
*
8+
*
99
* @author sasdjb
1010
*
1111
*/
@@ -15,6 +15,6 @@ public interface JSONString{
1515
* The toJSONString method allows a class to produce its own JSON
1616
* serialization.
1717
* */
18-
publicStringtoJSONString();
18+
StringtoJSONString();
1919

2020
}

‎src/main/java/org/skyscreamer/jsonassert/ArrayValueMatcher.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public ArrayValueMatcher(JSONComparator comparator, int index){
147147
*
148148
* @param comparator
149149
* comparator to use to compare elements
150-
* @from first element in actual array to compared
151-
* @to last element in actual array to compared
150+
* @param from first element in actual array to compared
151+
* @param to last element in actual array to compared
152152
*/
153153
publicArrayValueMatcher(JSONComparatorcomparator, intfrom, intto){
154154
assertcomparator != null : "comparator null";

‎src/main/java/org/skyscreamer/jsonassert/Customization.java‎

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@ private String buildPatternLevel1(String path){
2424
Stringregex = "\\*\\*\\.";
2525
Stringreplacement = "(?:.+\\.)?";
2626

27-
StringBuildersb = newStringBuilder();
28-
String[] parts = path.split(regex);
29-
for (inti = 0; i < parts.length; i++){
30-
Stringpart = parts[i];
31-
32-
sb.append(buildPatternLevel2(part));
33-
if (i < parts.length - 1){
34-
sb.append(replacement);
35-
}
36-
}
37-
38-
returnsb.toString();
27+
returnbuildPattern(path, regex, replacement, 1);
3928
}
4029

4130
privateStringbuildPatternLevel2(Strings){
@@ -45,17 +34,7 @@ private String buildPatternLevel2(String s){
4534
Stringregex = "\\*\\*";
4635
Stringreplacement = ".+";
4736

48-
StringBuildersb = newStringBuilder();
49-
String[] parts = s.split(regex);
50-
for (inti = 0; i < parts.length; i++){
51-
Stringpart = parts[i];
52-
53-
sb.append(buildPatternLevel3(part));
54-
if (i < parts.length - 1){
55-
sb.append(replacement);
56-
}
57-
}
58-
returnsb.toString();
37+
returnbuildPattern(s, regex, replacement, 2);
5938
}
6039

6140
privateStringbuildPatternLevel3(Strings){
@@ -66,19 +45,41 @@ private String buildPatternLevel3(String s){
6645
Stringregex = "\\*";
6746
Stringreplacement = "[^\\.]+";
6847

48+
returnbuildPattern(s, regex, replacement, 3);
49+
}
50+
51+
privateStringbuildPattern(Stringpath, Stringregex, Stringreplacement, intlevel){
6952
StringBuildersb = newStringBuilder();
70-
String[] parts = s.split(regex);
53+
String[] parts = path.split(regex);
7154
for (inti = 0; i < parts.length; i++){
72-
Stringpart = parts[i];
73-
74-
sb.append(Pattern.quote(part));
55+
sb.append(buildPatternForLevel(level, parts[i]));
7556
if (i < parts.length - 1){
7657
sb.append(replacement);
77-
}
78-
}
58+
}
59+
}
7960
returnsb.toString();
8061
}
8162

63+
privateStringbuildPatternForLevel(intlevel, Stringpart){
64+
switch (level){
65+
case1:
66+
returnbuildPatternLevel2(part);
67+
case2:
68+
returnbuildPatternLevel3(part);
69+
case3:
70+
returnPattern.quote(part);
71+
default:
72+
return"Incorrect level.";
73+
}
74+
}
75+
76+
/**
77+
* Creates a new{@link Customization} instance for{@code path} and{@code comparator}.
78+
*
79+
* @param path the json path
80+
* @param comparator the comparator
81+
* @return a new Customization
82+
*/
8283
publicstaticCustomizationcustomization(Stringpath, ValueMatcher<Object> comparator){
8384
returnnewCustomization(path, comparator);
8485
}

‎src/main/java/org/skyscreamer/jsonassert/JSONAssert.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void assertEquals(String message, String expectedStr, JSONObject a
7171
* Asserts that the JSONObject provided does not match the expected string. If it is it throws an
7272
*{@link AssertionError}.
7373
*
74-
* @see #assertEquals(String JSONObject, boolean)
74+
* @see #assertEquals(String, JSONObject, boolean)
7575
*
7676
* @param expectedStr Expected JSON string
7777
* @param actual JSONObject to compare

‎src/main/java/org/skyscreamer/jsonassert/JSONCompareMode.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public enum JSONCompareMode{
5252
privatefinalboolean_extensible;
5353
privatefinalboolean_strictOrder;
5454

55-
privateJSONCompareMode(booleanextensible, booleanstrictOrder){
55+
JSONCompareMode(booleanextensible, booleanstrictOrder){
5656
_extensible = extensible;
5757
_strictOrder = strictOrder;
5858
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
packageorg.skyscreamer.jsonassert;
22

3+
/**
4+
* Represents a value matcher that can compare two objects for equality.
5+
*
6+
* @param <T> the object type to compare
7+
*/
38
publicinterfaceValueMatcher<T>{
49

10+
/**
11+
* Compares the two provided objects whether they are equal.
12+
*
13+
* @param o1 the first object to check
14+
* @param o2 the object to check the first against
15+
* @return true if the objects are equal, false otherwise
16+
*/
517
booleanequal(To1, To2);
618

719
}

‎src/main/java/org/skyscreamer/jsonassert/comparator/DefaultComparator.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* This class is the default json comparator implementation. <p/>
14-
* Comparison is performed according to {@link JSONCompareMode} that is passed as constructor's argument.
14+
* Comparison is performed according to{@link JSONCompareMode} that is passed as constructor's argument.
1515
*/
1616
publicclassDefaultComparatorextendsAbstractComparator{
1717

@@ -36,8 +36,8 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
3636
@Override
3737
publicvoidcompareValues(Stringprefix, ObjectexpectedValue, ObjectactualValue, JSONCompareResultresult)
3838
throwsJSONException{
39-
if (expectedValueinstanceofNumber && actualValueinstanceofNumber){
40-
if (((Number)expectedValue).doubleValue() != ((Number)actualValue).doubleValue()){
39+
if (areNumbers(expectedValue, actualValue)){
40+
if (areNotSameDoubles(expectedValue, actualValue)){
4141
result.fail(prefix, expectedValue, actualValue);
4242
}
4343
} elseif (expectedValue.getClass().isAssignableFrom(actualValue.getClass())){
@@ -74,4 +74,12 @@ public void compareJSONArray(String prefix, JSONArray expected, JSONArray actual
7474
recursivelyCompareJSONArray(prefix, expected, actual, result);
7575
}
7676
}
77+
78+
protectedbooleanareNumbers(ObjectexpectedValue, ObjectactualValue){
79+
returnexpectedValueinstanceofNumber && actualValueinstanceofNumber;
80+
}
81+
82+
protectedbooleanareNotSameDoubles(ObjectexpectedValue, ObjectactualValue){
83+
return ((Number) expectedValue).doubleValue() != ((Number) actualValue).doubleValue();
84+
}
7785
}

‎src/main/java/org/skyscreamer/jsonassert/comparator/JSONComparator.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,59 @@
1313
*/
1414
publicinterfaceJSONComparator{
1515

16+
/**
17+
* Compares two{@link JSONObject}s and returns the result of the comparison in a{@link JSONCompareResult} object.
18+
*
19+
* @param expected the expected JSON object
20+
* @param actual the actual JSON object
21+
* @return the result of the comparison
22+
* @throws JSONException
23+
*/
1624
JSONCompareResultcompareJSON(JSONObjectexpected, JSONObjectactual) throwsJSONException;
1725

26+
/**
27+
* Compares two{@link JSONArray}s and returns the result of the comparison in a{@link JSONCompareResult} object.
28+
*
29+
* @param expected the expected JSON array
30+
* @param actual the actual JSON array
31+
* @return the result of the comparison
32+
* @throws JSONException
33+
*/
1834
JSONCompareResultcompareJSON(JSONArrayexpected, JSONArrayactual) throwsJSONException;
1935

36+
/**
37+
* Compares two{@link JSONObject}s on the provided path represented by{@code prefix} and
38+
* updates the result of the comparison in the{@code result}{@link JSONCompareResult} object.
39+
*
40+
* @param prefix the path in the json where the comparison happens
41+
* @param expected the expected JSON object
42+
* @param actual the actual JSON object
43+
* @param result stores the actual state of the comparison result
44+
* @throws JSONException
45+
*/
2046
voidcompareJSON(Stringprefix, JSONObjectexpected, JSONObjectactual, JSONCompareResultresult) throwsJSONException;
2147

48+
/**
49+
* Compares two{@link Object}s on the provided path represented by{@code prefix} and
50+
* updates the result of the comparison in the{@code result}{@link JSONCompareResult} object.
51+
*
52+
* @param prefix the path in the json where the comparison happens
53+
* @param expectedValue the expected value
54+
* @param actualValue the actual value
55+
* @param result stores the actual state of the comparison result
56+
* @throws JSONException
57+
*/
2258
voidcompareValues(Stringprefix, ObjectexpectedValue, ObjectactualValue, JSONCompareResultresult) throwsJSONException;
2359

60+
/**
61+
* Compares two{@link JSONArray}s on the provided path represented by{@code prefix} and
62+
* updates the result of the comparison in the{@code result}{@link JSONCompareResult} object.
63+
*
64+
* @param prefix the path in the json where the comparison happens
65+
* @param expected the expected JSON array
66+
* @param actual the actual JSON array
67+
* @param result stores the actual state of the comparison result
68+
* @throws JSONException
69+
*/
2470
voidcompareJSONArray(Stringprefix, JSONArrayexpected, JSONArrayactual, JSONCompareResultresult) throwsJSONException;
2571
}

0 commit comments

Comments
(0)