Browse Source

Empty json arrays and empty json objects are considered valid json.

pull/237/head
Patrik Helsing 8 years ago
parent
commit
6f485c8df2
  1. 4
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java
  2. 29
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java

4
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<Object> isJson() {
return isJson(withJsonPath("$..*"));
return isJson(withJsonPath("$", anyOf(instanceOf(Map.class), instanceOf(List.class))));
}
public static Matcher<Object> isJson(final Matcher<? super ReadContext> matcher) {

29
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())));
}

Loading…
Cancel
Save