Skip to content

Commit 659b782

Browse files
committed
initial implementation of ValidationException#toJSON(); fixed bug in ObjectComparator, now the comparison of JSONObjects is independent from the order of keys returned by JSONObject#getNames()
1 parent d0ccf94 commit 659b782

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

core/src/main/java/org/everit/json/schema/ObjectComparator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@ private static boolean deepEqualArrays(final JSONArray arr1, final JSONArray arr
6262
return true;
6363
}
6464

65+
private static String[] sortedNamesOf(final JSONObject obj) {
66+
String[] raw = JSONObject.getNames(obj);
67+
if (raw == null) {
68+
return null;
69+
}
70+
Arrays.sort(raw, String.CASE_INSENSITIVE_ORDER);
71+
return raw;
72+
}
73+
6574
private static boolean deepEqualObjects(final JSONObject jsonObj1, final JSONObject jsonObj2) {
66-
String[] obj1Names = JSONObject.getNames(jsonObj1);
67-
if (!Arrays.equals(obj1Names, JSONObject.getNames(jsonObj2))) {
75+
String[] obj1Names = sortedNamesOf(jsonObj1);
76+
if (!Arrays.equals(obj1Names, sortedNamesOf(jsonObj2))) {
6877
return false;
6978
}
7079
if (obj1Names == null) {

core/src/main/java/org/everit/json/schema/ValidationException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import java.util.Objects;
2222
import java.util.stream.Collectors;
2323

24+
import org.json.JSONArray;
25+
import org.json.JSONObject;
26+
2427
/**
2528
* Thrown by {@link Schema} subclasses on validation failure.
2629
*/
@@ -318,4 +321,13 @@ public int getViolationCount() {
318321
public String getKeyword() {
319322
return keyword;
320323
}
324+
325+
public JSONObject toJSON() {
326+
JSONObject rval = new JSONObject();
327+
rval.put("keyword", keyword);
328+
rval.put("pointerToViolation", getPointerToViolation());
329+
rval.put("message", getMessage());
330+
rval.put("causingExceptions", new JSONArray());
331+
return rval;
332+
}
321333
}

core/src/test/java/org/everit/json/schema/ValidationExceptionTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import org.json.JSONObject;
23+
import org.json.JSONTokener;
2224
import org.junit.Assert;
2325
import org.junit.Test;
2426

@@ -185,4 +187,25 @@ public void toStringWithCauses() {
185187
Assert.assertEquals("#: 3 schema violations found", subject.getMessage());
186188
}
187189

190+
@Test
191+
public void testToJSON() {
192+
ValidationException subject =
193+
new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a/b"),
194+
"exception message", Collections.emptyList(), "type");
195+
JSONObject expected = new JSONObject(new JSONTokener(
196+
getClass().getResourceAsStream("/org/everit/jsonvalidator/exception-to-json.json")));
197+
198+
JSONObject actual = subject.toJSON();
199+
Assert.assertEquals(4, JSONObject.getNames(actual).length);
200+
Assert.assertEquals(expected.get("keyword"), actual.get("keyword"));
201+
Assert.assertEquals(expected.get("pointerToViolation"), actual.get("pointerToViolation"));
202+
Assert.assertEquals(expected.get("message"), actual.get("message"));
203+
// Assert.assertEquals(expected.getJSONArray("causingExceptions"),
204+
// actual.getJSONArray("causingExceptions"));
205+
Assert.assertTrue(ObjectComparator.deepEquals(expected.getJSONArray("causingExceptions"),
206+
actual.getJSONArray("causingExceptions")));
207+
Assert.assertTrue(ObjectComparator.deepEquals(expected,
208+
actual));
209+
}
210+
188211
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"pointerToViolation" : "#/a/b",
3+
"message" : "#/a/b: exception message",
4+
"keyword" : "type",
5+
"causingExceptions" : []
6+
}

0 commit comments

Comments
 (0)