From 6f485c8df2d391058b57ff35209ea20d7037aeb5 Mon Sep 17 00:00:00 2001 From: Patrik Helsing Date: Sat, 28 May 2016 17:52:00 +0200 Subject: [PATCH] Empty json arrays and empty json objects are considered valid json. --- .../jsonpath/matchers/JsonPathMatchers.java | 4 ++- .../matchers/JsonPathMatchersTest.java | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java index c89c42cc..562305a5 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java +++ b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java @@ -6,6 +6,8 @@ import com.jayway.jsonpath.ReadContext; import org.hamcrest.Matcher; import java.io.File; +import java.util.List; +import java.util.Map; import static org.hamcrest.Matchers.*; @@ -30,7 +32,7 @@ public class JsonPathMatchers { } public static Matcher isJson() { - return isJson(withJsonPath("$..*")); + return isJson(withJsonPath("$", anyOf(instanceOf(Map.class), instanceOf(List.class)))); } public static Matcher isJson(final Matcher matcher) { diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java index f52edafd..8b4d2b32 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java @@ -7,7 +7,6 @@ import org.junit.BeforeClass; import org.junit.Test; import java.io.File; -import java.nio.file.Paths; import java.util.Collection; import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; @@ -34,6 +33,26 @@ public class JsonPathMatchersTest { Configuration.setDefaults(null); } + @Test + public void shouldMatchOnEmptyJsonObject() { + assertThat("{}", isJson()); + } + + @Test + public void shouldMatchOnJsonObject() { + assertThat("{ \"hi\" : \"there\" }", isJson()); + } + + @Test + public void shouldMatchOnEmptyJsonArray() { + assertThat("[]", isJson()); + } + + @Test + public void shouldMatchOnJsonArray() { + assertThat("[\"hi\", \"there\"]", isJson()); + } + @Test public void shouldMatchValidJson() { assertThat(VALID_JSON, isJson()); @@ -44,7 +63,11 @@ public class JsonPathMatchersTest { public void shouldNotMatchInvalidJson() { assertThat(INVALID_JSON, not(isJson())); assertThat(new Object(), not(isJson())); - assertThat("{}", not(isJson())); + assertThat(new Object[]{}, not(isJson())); + assertThat("hi there", not(isJson())); + assertThat(new Integer(42), not(isJson())); + assertThat(Boolean.TRUE, not(isJson())); + assertThat(false, not(isJson())); assertThat(null, not(isJson())); } @@ -122,7 +145,7 @@ public class JsonPathMatchersTest { @Test public void shouldNotMatchJsonPathOnNonExistingFile() { - File nonExistingFile = Paths.get("missing-file").toFile(); + File nonExistingFile = new File("missing-file"); assertThat(nonExistingFile, not(hasJsonPath("$..*", anything()))); }