diff --git a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java index fb868c4b..9593b430 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java +++ b/json-path/src/main/java/com/jayway/jsonpath/PathUtil.java @@ -2,6 +2,7 @@ package com.jayway.jsonpath; import java.util.LinkedList; import java.util.List; +import java.util.Scanner; /** * User: kalle stenflo @@ -12,18 +13,18 @@ public class PathUtil { /** * Checks if a path points to a single item or if it potentially returns multiple items - * + *

* a path is considered not definite if it contains a scan fragment ".." * or an array position fragment that is not based on a single index - * - * + *

+ *

* absolute path examples: - * + *

* $store.book * $store.book[1].value - * + *

* not absolute path examples - * + *

* $..book * $.store.book[1,2] * $.store.book[?(@.category = 'fiction')] @@ -38,8 +39,8 @@ public class PathUtil { /** * Splits a path into fragments - * - * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "value"] + *

+ * the path $.store.book[1].category returns ["$", "store", "book", "[1]", "category"] * * @param jsonPath path to split * @return fragments @@ -52,7 +53,10 @@ public class PathUtil { jsonPath = "$." + jsonPath; } - jsonPath = jsonPath.replace("..", ".~.") + jsonPath = jsonPath.replace("$['","$.['") + .replaceAll("\\['(\\w+)\\.(\\w+)']", "['$1~$2']") + .replace("']['","'].['" ) + .replace("..", ".~~.") .replace("[", ".[") .replace("@.", "@") .replace("['", "") @@ -64,8 +68,13 @@ public class PathUtil { if (split[i].trim().isEmpty()) { continue; } - fragments.add(split[i].replace("@", "@.").replace("~", "..")); + fragments.add(split[i].replace("@", "@.").replace("~", ".")); } + + //for (String fragment : fragments) { + // System.out.println(fragment); + //} + return fragments; } } 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 ec49585e..f81e2640 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import java.util.List; import java.util.Map; +import java.util.Scanner; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; @@ -48,11 +49,20 @@ public class JsonPathTest { " \"bicycle\": {\n" + " \"color\": \"red\",\n" + " \"price\": 19.95,\n" + - " \"foo:bar\": \"fooBar\"\n" + + " \"foo:bar\": \"fooBar\",\n" + + " \"dot.notation\": \"new\"\n" + " }\n" + " }\n" + "}"; + @Test + public void bracket_notation_can_be_used_in_path() throws Exception { + + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].bicycle.['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$['store']['bicycle']['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store']['bicycle']['dot.notation']")); + assertEquals("new", JsonPath.read(DOCUMENT, "$.['store'].['bicycle'].['dot.notation']")); + } @Test public void filter_an_array() throws Exception { @@ -66,7 +76,7 @@ public class JsonPathTest { public void read_path_with_colon() throws Exception { assertEquals(JsonPath.read(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 @@ -103,6 +113,8 @@ public class JsonPathTest { 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")); + 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")); } @@ -185,7 +197,7 @@ public class JsonPathTest { res = JsonPath.read(DOCUMENT, "$.store.book[1, 200].author"); - assertThat((List)res, hasItems("Evelyn Waugh")); + assertThat((List) res, hasItems("Evelyn Waugh")); //assertNull((); } @@ -200,4 +212,5 @@ public class JsonPathTest { public void invalid_new_path_throws_exception() throws Exception { JsonPath.read(DOCUMENT, "new "); } + } diff --git a/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java index a5650bee..a781788c 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java @@ -30,6 +30,18 @@ public class SplitPathFragmentsTest { */ + @Test + public void bracket_notation_can_be_split() throws Exception { + assertThat(PathUtil.splitPath("$.['store'].['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("$['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store']['price']"), hasItems("$", "store", "price")); + assertThat(PathUtil.splitPath("['store'].price"), hasItems("$", "store", "price")); + + assertThat(PathUtil.splitPath("$.['store book'].['price list']"), hasItems("$", "store book", "price list")); + + assertThat(PathUtil.splitPath("$.['store.book'].['price.list']"), hasItems("$", "store.book", "price.list")); + } + @Test public void fragments_are_split_correctly() throws Exception { @@ -62,6 +74,7 @@ public class SplitPathFragmentsTest { assertThat(PathUtil.splitPath("$.[0].[1].author"), hasItems("$", "[0]", "[1]", "author")); assertThat(PathUtil.splitPath("$.foo:bar.author"), hasItems("$", "foo:bar", "author")); + }