Browse Source

Improved JSON bracket notation.

pull/1/merge
kalle 14 years ago
parent
commit
85d1585d13
  1. 29
      json-path/src/main/java/com/jayway/jsonpath/PathUtil.java
  2. 19
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  3. 13
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java

29
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
*
* <p/>
* a path is considered <strong>not</strong> definite if it contains a scan fragment ".."
* or an array position fragment that is not based on a single index
*
*
* <p/>
* <p/>
* absolute path examples:
*
* <p/>
* $store.book
* $store.book[1].value
*
* <p/>
* not absolute path examples
*
* <p/>
* $..book
* $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')]
@ -38,8 +39,8 @@ public class PathUtil {
/**
* Splits a path into fragments
*
* the path <code>$.store.book[1].category</code> returns ["$", "store", "book", "[1]", "value"]
* <p/>
* the path <code>$.store.book[1].category</code> 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;
}
}

19
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.<List<String>>read(DOCUMENT, "$.store.book[0,1].author"), hasItems("Nigel Rees", "Evelyn Waugh"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.store.book[*].author"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$.['store'].['book'][*].['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$['store']['book'][*]['author']"), hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
assertThat(JsonPath.<List<String>>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<String>)res, hasItems("Evelyn Waugh"));
assertThat((List<String>) 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 ");
}
}

13
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"));
}

Loading…
Cancel
Save