From e55e0a2aacd7133a82bf1119fc170a0c02a5a738 Mon Sep 17 00:00:00 2001 From: kalle Date: Wed, 9 Feb 2011 14:58:57 +0100 Subject: [PATCH] removed readOne from JsonPath --- .../jsonassert/impl/JsonAsserterImpl.java | 31 ++++--- .../com/jayway/jsonassert/JsonAssertTest.java | 4 +- .../java/com/jayway/jsonpath/JsonPath.java | 68 ++++++++------ .../jayway/jsonpath/filter/FilterOutput.java | 39 ++++++++ .../jsonpath/filter/JsonPathFilterBase.java | 2 +- .../jsonpath/filter/JsonPathFilterChain.java | 14 +-- .../jayway/jsonpath/filter/ListFilter.java | 91 +++++++++++-------- .../jsonpath/filter/PropertyFilter.java | 40 ++++++-- .../jayway/jsonpath/filter/RootFilter.java | 4 +- .../jsonpath/filter/TraverseFilter.java | 6 +- .../com/jayway/jsonpath/JsonPathTest.java | 56 +++++++----- 11 files changed, 228 insertions(+), 127 deletions(-) create mode 100644 json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java index f2c38e2d..f567b5c8 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java @@ -3,7 +3,6 @@ package com.jayway.jsonassert.impl; import com.jayway.jsonassert.JsonAsserter; import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.PathUtil; import org.hamcrest.Matcher; import static java.lang.String.format; @@ -38,17 +37,25 @@ public class JsonAsserterImpl implements JsonAsserter { String reason = "When processing json path: " + path; - if (PathUtil.isPathDefinite(path)) { - if (!matcher.matches(JsonPath.readOne(jsonObject, path))) { - throw new AssertionError(reason + matcher.toString()); - } - //MatcherAssert.assertThat(reason, JsonPath.readOne(jsonObject, path), matcher); - } else { - if (!matcher.matches(JsonPath.read(jsonObject, path))) { - throw new AssertionError(reason + matcher.toString()); - } - //MatcherAssert.assertThat(reason, (T) JsonPath.read(jsonObject, path), matcher); + if (!matcher.matches(JsonPath.read(jsonObject, path))) { + + System.out.println(JsonPath.read(jsonObject, path).toString()); + + throw new AssertionError(reason + matcher.toString()); } + + /* + if (PathUtil.isPathDefinite(path)) { + if (!matcher.matches(JsonPath.readOne(jsonObject, path))) { + throw new AssertionError(reason + matcher.toString()); + } + //MatcherAssert.assertThat(reason, JsonPath.readOne(jsonObject, path), matcher); + } else { + if (!matcher.matches(JsonPath.read(jsonObject, path))) { + throw new AssertionError(reason + matcher.toString()); + } + //MatcherAssert.assertThat(reason, (T) JsonPath.read(jsonObject, path), matcher); + } */ return this; } @@ -63,7 +70,7 @@ public class JsonAsserterImpl implements JsonAsserter { * {@inheritDoc} */ public JsonAsserter assertNotDefined(String path) { - Object o = JsonPath.readOne(jsonObject, path); + Object o = JsonPath.read(jsonObject, path); if (o != null) { throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); diff --git a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java index 513e5385..d01ae480 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java @@ -104,7 +104,7 @@ public class JsonAssertTest { with(JSON).assertThat("$.store.book[0]", hasEntry("category", "reference")) .assertThat("$.store.book[0]", hasEntry("title", "Sayings of the Century")) .and() - .assertThat("$..book[0]", hasItems(hasEntry("category", "reference"))) + .assertThat("$..book[0]", hasEntry("category", "reference")) .and() .assertThat("$.store.book[0]", mapContainingKey(equalTo("category"))) .and() @@ -113,7 +113,7 @@ public class JsonAssertTest { with(JSON).assertThat("$.['store'].['book'][0]", hasEntry("category", "reference")) .assertThat("$.['store'].['book'][0]", hasEntry("title", "Sayings of the Century")) .and() - .assertThat("$..['book'][0]", hasItems(hasEntry("category", "reference"))) + .assertThat("$..['book'][0]", hasEntry("category", "reference")) .and() .assertThat("$.['store'].['book'][0]", mapContainingKey(equalTo("category"))) .and() diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index b24f3220..e1422b69 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -1,6 +1,7 @@ package com.jayway.jsonpath; +import com.jayway.jsonpath.filter.FilterOutput; import com.jayway.jsonpath.filter.JsonPathFilterChain; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -104,8 +105,14 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public List read(Object json) { - return (List) filters.filter(json); + public T read(Object json) { + FilterOutput filterOutput = filters.filter(json); + + if(filterOutput == null || filterOutput.getResult() == null){ + return null; + } + + return (T)filterOutput.getResult(); } /** @@ -115,8 +122,8 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public List read(String json) throws java.text.ParseException { - return read(parse(json)); + public T read(String json) throws java.text.ParseException { + return (T)read(parse(json)); } /** @@ -137,8 +144,8 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public static List read(String json, String jsonPath) throws java.text.ParseException { - return compile(jsonPath).read(json); + public static T read(String json, String jsonPath) throws java.text.ParseException { + return (T)compile(jsonPath).read(json); } /** @@ -149,10 +156,11 @@ public class JsonPath { * @param * @return list of objects matched by the given path */ - public static List read(Object json, String jsonPath) { - return compile(jsonPath).read(json); + public static T read(Object json, String jsonPath) { + return (T)compile(jsonPath).read(json); } + /** * Creates a new JsonPath and applies it to the provided Json object. Note this method * will throw an exception if the provided path returns more than one object. This method @@ -163,23 +171,31 @@ public class JsonPath { * @param * @return the object matched by the given path */ - public static T readOne(Object json, String jsonPath) { - List result = compile(jsonPath).read(json, jsonPath); - if (log.isLoggable(Level.WARNING)) { - if (!PathUtil.isPathDefinite(jsonPath)) { - log.warning("Using readOne() on a not definite json path may give incorrect results. Path : " + jsonPath); - } - } +// public static T readOne(Object json, String jsonPath) { +// Object result = compile(jsonPath).read(json, jsonPath); +// +// if (log.isLoggable(Level.WARNING)) { +// if (!PathUtil.isPathDefinite(jsonPath)) { +// log.warning("Using readOne() on a not definite json path may give incorrect results. Path : " + jsonPath); +// } +// } +// +// return (T)result; +// +// /* +// if(result instanceof List){ +// if (result.size() > 1) { +// throw new RuntimeException(format("Expected one result when reading path: %s but was: ", jsonPath, result.size())); +// } +// else if (result.isEmpty()){ +// return null; +// } +// return (T) result.get(0); +// } +// */ +// } - if (result.size() > 1) { - throw new RuntimeException(format("Expected one result when reading path: %s but was: ", jsonPath, result.size())); - } - else if (result.isEmpty()){ - return null; - } - return (T) result.get(0); - } /** * Creates a new JsonPath and applies it to the provided Json object. Note this method @@ -191,9 +207,9 @@ public class JsonPath { * @param * @return the object matched by the given path */ - public static T readOne(String json, String jsonPath) throws java.text.ParseException { - return (T) readOne(parse(json), jsonPath); - } +// public static T readOne(String json, String jsonPath) throws java.text.ParseException { +// return (T) readOne(parse(json), jsonPath); +// } private static Object parse(String json) throws java.text.ParseException { try { diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java b/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java new file mode 100644 index 00000000..4d868dfe --- /dev/null +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java @@ -0,0 +1,39 @@ +package com.jayway.jsonpath.filter; + +import java.util.List; + +import static java.lang.String.format; + +/** + * User: kalle stenflo + * Date: 2/9/11 + * Time: 12:28 PM + */ +public class FilterOutput { + + private final Object result; + + public FilterOutput(Object result) { + this.result = result; + } + + + public boolean isList(){ + + return (result instanceof List); + + } + + public Object getResult() { + return result; + } + public List getResultAsList() { + if(!isList()){ + throw new RuntimeException(format("Can not convert a %s to a %s", result.getClass().getName(), List.class.getName())); + } + return (List)result; + } + + + +} diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java index 933eec28..9b920ab4 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java @@ -9,5 +9,5 @@ import java.util.List; * Time: 2:01 PM */ public abstract class JsonPathFilterBase { - public abstract List apply(List filter); + public abstract FilterOutput apply(FilterOutput filterItems); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java index c18d78a2..9ee917eb 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java @@ -29,20 +29,20 @@ public class JsonPathFilterChain { return configured; } - public List filter(Object root) { + public FilterOutput filter(Object root) { - List rootList = new JSONArray(); - rootList.add(root); - - List result = rootList; + FilterOutput out = new FilterOutput(root); for (JsonPathFilterBase filter : filters) { if (filter == null) { throw new InvalidPathException(); } - result = filter.apply(result); + if(out.getResult() == null){ + return null; + } + out = filter.apply(out); } - return result; + return out; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java index 4b4ebfa2..181af408 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/ListFilter.java @@ -4,14 +4,15 @@ import com.jayway.jsonpath.JsonUtil; import com.jayway.jsonpath.eval.ExpressionEvaluator; import org.json.simple.JSONArray; -//import javax.script.ScriptEngine; -//import javax.script.ScriptEngineManager; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +//import javax.script.ScriptEngine; +//import javax.script.ScriptEngineManager; + /** * User: kalle stenflo * Date: 2/2/11 @@ -37,31 +38,31 @@ public class ListFilter extends JsonPathFilterBase { } @Override - public List apply(List items) { - List result = new JSONArray(); + public FilterOutput apply(FilterOutput items) { + Object result = new JSONArray(); if (LIST_INDEX_PATTERN.matcher(pathFragment).matches()) { - return filterByListIndex(items); + result = filterByListIndex(items.getResultAsList()); } else if (LIST_WILDCARD_PATTERN.matcher(pathFragment).matches()) { - return filterByWildcard(items); + result = filterByWildcard(items.getResultAsList()); } else if (LIST_TAIL_PATTERN.matcher(pathFragment).matches()) { - return filterByListTailIndex(items); + result = filterByListTailIndex(items.getResultAsList()); } else if (LIST_PULL_PATTERN.matcher(pathFragment).matches()) { - return filterByPullIndex(items); + result = filterByPullIndex(items.getResultAsList()); } else if (LIST_ITEM_HAS_PROPERTY_PATTERN.matcher(pathFragment).matches()) { - return filterByItemProperty(items); + result = filterByItemProperty(items.getResultAsList()); } else if (LIST_ITEM_MATCHES_EVAL.matcher(pathFragment).matches()) { - return filterByItemEvalMatch(items); + result = filterByItemEvalMatch(items.getResultAsList()); } - return result; + return new FilterOutput(result); } private List filterByItemEvalMatch(List items) { List result = new JSONArray(); for (Object current : items) { - for (Object item : JsonUtil.toList(current)) { + for (Object item : items) { if (isEvalMatch(item)) { result.add(item); } @@ -76,8 +77,8 @@ public class ListFilter extends JsonPathFilterBase { String prop = getFilterProperty(); - for (Object current : items) { - for (Object item : JsonUtil.toList(current)) { + //for (Object current : items) { + for (Object item : JsonUtil.toList(items)) { if (JsonUtil.isMap(item)) { if (JsonUtil.toMap(item).containsKey(prop)) { @@ -85,7 +86,7 @@ public class ListFilter extends JsonPathFilterBase { } } } - } + //} return result; } @@ -94,50 +95,62 @@ public class ListFilter extends JsonPathFilterBase { List result = new JSONArray(); for (Object current : items) { - result.addAll(JsonUtil.toList(current)); + if(current instanceof List){ + result.addAll(JsonUtil.toList(current)); + } + else { + result.add(current); + } } return result; } - private List filterByListTailIndex(List items) { - List result = new JSONArray(); + private Object filterByListTailIndex(List items) { + //for (Object current : items) { + // Map array = JsonUtil.toMap(current); - for (Object current : items) { - List array = JsonUtil.toList(current); + int index = getTailIndex(items.size()); + + return items.get(index); - result.add(array.get(getTailIndex(array.size()))); - } - return result; } - private List filterByListIndex(List items) { - List result = new JSONArray(); + private Object filterByListIndex(List items) { + Object result = null; - for (Object current : items) { - List target = JsonUtil.toList(current); - Integer[] index = getArrayIndex(); + //for (Object current : items) { + //List target = JsonUtil.toList(current); + Integer[] index = getArrayIndex(); + if (index.length > 1) { + List tmp = new JSONArray(); for (int i : index) { - if(indexIsInRange(target, i)){ - result.add(target.get(i)); + if (indexIsInRange(items, i)) { + tmp.add(items.get(i)); } } + return result = tmp; + } else { + if (indexIsInRange(items, index[0])) { + result = items.get(index[0]); + } } + //} return result; } private List filterByPullIndex(List items) { List result = new JSONArray(); - for (Object current : items) { - List target = JsonUtil.toList(current); + //for (Object current : items) { + //List target = JsonUtil.toList(current); Integer[] index = getListPullIndex(); for (int i : index) { - if(indexIsInRange(target, i)){ - result.add(target.get(i)); + if (indexIsInRange(items, i)) { + result.add(items.get(i)); } } - } + //} return result; } @@ -232,12 +245,12 @@ public class ListFilter extends JsonPathFilterBase { return index.toArray(new Integer[0]); } - private boolean indexIsInRange(List list, int index){ - if(index < 0){ + private boolean indexIsInRange(List list, int index) { + if (index < 0) { return false; - }else if(index > list.size() -1){ + } else if (index > list.size() - 1) { return false; - }else { + } else { return true; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java index a91a42ee..b6b38f0b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java @@ -22,25 +22,45 @@ public class PropertyFilter extends JsonPathFilterBase { } @Override - public List apply(List filter) { + public FilterOutput apply(FilterOutput filter) { List result = new JSONArray(); if (WILDCARD.equals(pathFragment)) { - for (Object current : filter) { - for (Object value : JsonUtil.toMap(current).values()) { + if (filter.isList()) { + for (Object current : filter.getResultAsList()) { + for (Object value : JsonUtil.toMap(current).values()) { + result.add(value); + } + } + } else { + for (Object value : JsonUtil.toMap(filter.getResult()).values()) { result.add(value); } } - } - else { - for (Object current : filter) { - if (JsonUtil.toMap(current).containsKey(pathFragment)) { - result.add(JsonUtil.toMap(current).get(pathFragment)); + return new FilterOutput(result); + } else { + if (filter.isList()) { + for (Object current : filter.getResultAsList()) { + if (JsonUtil.toMap(current).containsKey(pathFragment)) { + + Object o = JsonUtil.toMap(current).get(pathFragment); + if (JsonUtil.isList(o)) { + result.addAll(JsonUtil.toList(o)); + + } else { + result.add(JsonUtil.toMap(current).get(pathFragment)); + } + } + } + return new FilterOutput(result); + } else { + Object mapValue = null; + if (JsonUtil.toMap(filter.getResult()).containsKey(pathFragment)) { + mapValue = JsonUtil.toMap(filter.getResult()).get(pathFragment); } + return new FilterOutput(mapValue); } } - - return result; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java index a7e138b4..ff22e8a0 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/RootFilter.java @@ -11,7 +11,7 @@ import java.util.List; public class RootFilter extends JsonPathFilterBase{ @Override - public List apply(List filter) { - return filter; + public FilterOutput apply(FilterOutput root) { + return root; } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java b/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java index 07ca587d..3de9802a 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/filter/TraverseFilter.java @@ -14,12 +14,12 @@ import java.util.List; public class TraverseFilter extends JsonPathFilterBase { @Override - public List apply(List filter) { + public FilterOutput apply(FilterOutput filter) { List result = new JSONArray(); - traverse(filter, result); + traverse(filter.getResult(), result); - return result; + return new FilterOutput(result); } private void traverse(Object container, List result) { diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java index 86be95a3..e6623c77 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; import static org.junit.Assert.*; @@ -54,16 +55,16 @@ public class JsonPathTest { @Test public void read_path_with_colon() throws Exception { - assertEquals(JsonPath.readOne(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar"); - assertEquals(JsonPath.readOne(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']"), "fooBar"); + assertEquals(JsonPath.read(DOCUMENT, "$.store.bicycle.foo:bar"), "fooBar"); + assertEquals(JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['foo:bar']"), "fooBar"); } @Test public void read_document_from_root() throws Exception { - List list = JsonPath.read(DOCUMENT, "$.store"); + Map result = JsonPath.read(DOCUMENT, "$.store"); - assertEquals(2, ((Map) list.get(0)).values().size()); + assertEquals(2, result.values().size()); } @@ -73,7 +74,9 @@ public class JsonPathTest { JsonPath path = JsonPath.compile("$.store.book[1]"); - List list = path.read(DOCUMENT); + Map map = path.read(DOCUMENT); + + assertEquals("Evelyn Waugh", map.get("author")); } @Test @@ -86,14 +89,15 @@ public class JsonPathTest { @Test public void read_store_book_author() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); - assertThat(JsonPath.read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); } @Test public void all_authors() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$..author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); + assertThat(JsonPath.>read(DOCUMENT, "$..author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); } @@ -101,57 +105,57 @@ public class JsonPathTest { public void all_store_properties() throws Exception { List itemsInStore = JsonPath.read(DOCUMENT, "$.store.*"); - assertEquals(JsonPath.readOne(itemsInStore, "$.[0].[0].author"), "Nigel Rees"); - assertEquals(JsonPath.readOne(itemsInStore, "$.[0][0].author"), "Nigel Rees"); + assertEquals(JsonPath.read(itemsInStore, "$.[0].[0].author"), "Nigel Rees"); + assertEquals(JsonPath.read(itemsInStore, "$.[0][0].author"), "Nigel Rees"); } @Test public void all_prices_in_store() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store..price"), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); + assertThat(JsonPath.>read(DOCUMENT, "$.store..price"), hasItems(8.95D, 12.99D, 8.99D, 19.95D)); } @Test public void access_array_by_index_from_tail() throws Exception { - assertThat(JsonPath.readOne(DOCUMENT, "$..book[(@.length-1)].author"), equalTo("J. R. R. Tolkien")); - assertThat(JsonPath.readOne(DOCUMENT, "$..book[-1:].author"), equalTo("J. R. R. Tolkien")); + assertThat(JsonPath.read(DOCUMENT, "$..book[(@.length-1)].author"), equalTo("J. R. R. Tolkien")); + assertThat(JsonPath.read(DOCUMENT, "$..book[-1:].author"), equalTo("J. R. R. Tolkien")); } @Test public void read_store_book_index_0_and_1() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); - assertTrue(JsonPath.read(DOCUMENT, "$.store.book[0,1].author").size() == 2); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[0,1].author").size() == 2); } @Test public void read_store_book_pull_first_2() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store.book[:2].author"), hasItems("Nigel Rees", "Evelyn Waugh")); - assertTrue(JsonPath.read(DOCUMENT, "$.store.book[:2].author").size() == 2); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[:2].author"), hasItems("Nigel Rees", "Evelyn Waugh")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[:2].author").size() == 2); } @Test public void read_store_book_filter_by_isbn() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn"), hasItems("0-553-21311-3", "0-395-19395-8")); - assertTrue(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[?(@.isbn)].isbn"), hasItems("0-553-21311-3", "0-395-19395-8")); + assertTrue(JsonPath.read(DOCUMENT, "$.store.book[?(@.isbn)].isbn").size() == 2); } @Test public void all_books_cheaper_than_10() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$.store.book[?(@.price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick")); + assertThat(JsonPath.>read(DOCUMENT, "$.store.book[?(@.price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick")); } @Test public void all_books_with_category_reference() throws Exception { - assertThat(JsonPath.read(DOCUMENT, "$..book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century")); + assertThat(JsonPath.>read(DOCUMENT, "$..book[?(@.category = 'reference')].title"), hasItems("Sayings of the Century")); } @@ -163,13 +167,15 @@ public class JsonPathTest { @Test public void access_index_out_of_bounds_does_not_throw_exception() throws Exception { - List res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); + Object res = JsonPath.read(DOCUMENT, "$.store.book[100].author"); + + assertNull(res); - assertTrue(res.isEmpty()); + res = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); - res = JsonPath.read(DOCUMENT, "$.store.book[100, 200].author"); - assertTrue(res.isEmpty()); + assertThat((List)res, hasItems("Evelyn Waugh")); + //assertNull((); } /*