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-
public String toJSONString();
18+
String toJSONString();
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
public ArrayValueMatcher(JSONComparator comparator, int from, int to) {
154154
assert comparator != 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
String regex = "\\*\\*\\.";
2525
String replacement = "(?:.+\\.)?";
2626

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

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

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

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

48+
return buildPattern(s, regex, replacement, 3);
49+
}
50+
51+
private String buildPattern(String path, String regex, String replacement, int level) {
6952
StringBuilder sb = new StringBuilder();
70-
String[] parts = s.split(regex);
53+
String[] parts = path.split(regex);
7154
for (int i = 0; i < parts.length; i++) {
72-
String part = 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
return sb.toString();
8061
}
8162

63+
private String buildPatternForLevel(int level, String part) {
64+
switch (level) {
65+
case 1:
66+
return buildPatternLevel2(part);
67+
case 2:
68+
return buildPatternLevel3(part);
69+
case 3:
70+
return Pattern.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
public static Customization customization(String path, ValueMatcher<Object> comparator) {
8384
return new Customization(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
private final boolean _extensible;
5353
private final boolean _strictOrder;
5454

55-
private JSONCompareMode(boolean extensible, boolean strictOrder) {
55+
JSONCompareMode(boolean extensible, boolean strictOrder) {
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
package org.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
public interface ValueMatcher<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
boolean equal(T o1, T o2);
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
public class DefaultComparator extends AbstractComparator {
1717

@@ -36,8 +36,8 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
3636
@Override
3737
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
3838
throws JSONException {
39-
if (expectedValue instanceof Number && actualValue instanceof Number) {
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
} else if (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+
protected boolean areNumbers(Object expectedValue, Object actualValue) {
79+
return expectedValue instanceof Number && actualValue instanceof Number;
80+
}
81+
82+
protected boolean areNotSameDoubles(Object expectedValue, Object actualValue) {
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
public interface JSONComparator {
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
JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException;
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
JSONCompareResult compareJSON(JSONArray expected, JSONArray actual) throws JSONException;
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
void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException;
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
void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) throws JSONException;
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
void compareJSONArray(String prefix, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException;
2571
}

0 commit comments

Comments
 (0)