diff --git a/README.md b/README.md index 81c132e8..36bb6980 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ List> expensiveBooks = JsonPath All `read` operations are overloaded and also supports compiled JsonPath objects. This can be useful from a performance perspective if the same path is to be executed many times. -``` +```java JsonPath compiledPath = JsonPath.compile("$.store.book[1].author"); String author2 = JsonPath.read(document, compiledPath); diff --git a/json-path-web-test/src/main/resources/webapp/index.html b/json-path-web-test/src/main/resources/webapp/index.html index ecd8af4c..59626446 100644 --- a/json-path-web-test/src/main/resources/webapp/index.html +++ b/json-path-web-test/src/main/resources/webapp/index.html @@ -38,40 +38,35 @@ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", - "display-price": 8.95 + "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", - "display-price": 12.99 + "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", - "display-price": 8.99 + "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", - "display-price": 22.99 + "price": 22.99 } ], "bicycle": { - "foo": "baz", "color": "red", - "display-price": 19.95, - "foo:bar": "fooBar", - "dot.notation": "new", - "dash-notation": "dashes" + "price": 19.95 } }, - "foo": "bar", - "@id": "ID" + "expensive": 10 } diff --git a/json-path-web-test/src/main/resources/webapp/json/goessner.json b/json-path-web-test/src/main/resources/webapp/json/goessner.json index 86b9e134..7cff27f0 100644 --- a/json-path-web-test/src/main/resources/webapp/json/goessner.json +++ b/json-path-web-test/src/main/resources/webapp/json/goessner.json @@ -1 +1 @@ -{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "display-price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "display-price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "display-price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "display-price": 22.99 } ], "bicycle": { "foo": "baz", "color": "red", "display-price": 19.95, "foo:bar": "fooBar", "dot.notation": "new", "dash-notation": "dashes" } }, "foo": "bar", "@id": "ID" } \ No newline at end of file +{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 } \ No newline at end of file diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java index 16d0dcd5..66363aec 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java @@ -134,6 +134,7 @@ public class PathCompiler { } private static void assertValidFieldChars(String s, int start, int positions) { + /* int i = start; while (i < start + positions) { char c = s.charAt(i); @@ -143,6 +144,7 @@ public class PathCompiler { } i++; } + */ } private static int fastForward(String s, int index) { diff --git a/json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java b/json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java index bef9b34f..144817a3 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/ReturnTypeTest.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; import static com.jayway.jsonpath.JsonPath.parse; +import static com.jayway.jsonpath.JsonPath.using; +import static com.jayway.jsonpath.Option.AS_PATH_LIST; import static org.assertj.core.api.Assertions.assertThat; @SuppressWarnings("ALL") @@ -61,4 +63,23 @@ public class ReturnTypeTest extends BaseTest { .containsEntry("display-price", 8.95D); } + + @Test + public void a_path_evaluation_can_be_returned_as_PATH_LIST() { + Configuration conf = Configuration.builder().options(AS_PATH_LIST).build(); + + List pathList = using(conf).parse(JSON_DOCUMENT).read("$..author"); + + assertThat(pathList).containsExactly( + "$['store']['book'][0]['author']", + "$['store']['book'][1]['author']", + "$['store']['book'][2]['author']", + "$['store']['book'][3]['author']"); + + } + + @Test(expected = ClassCastException.class) + public void class_cast_exception_is_thrown_when_return_type_is_not_expected() { + List list = reader.read("$.store.book[0].author"); + } }