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.