Browse Source

Fix issue #667 and add testcases

pull/701/head
SUSTech-11810721 4 years ago
parent
commit
a593b4206e
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java
  2. 75
      json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java

@ -57,7 +57,7 @@ public class Length implements PathFunction {
while (null != tail && null != tail.getNext()) { while (null != tail && null != tail.getNext()) {
tail = tail.getNext(); tail = tail.getNext();
} }
if (null != tail) { if (ctx.configuration().jsonProvider().isMap(model) && null != tail) {
tail.setNext(new WildcardPathToken()); tail.setNext(new WildcardPathToken());
} }
} }

75
json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java

@ -68,6 +68,14 @@ public class JsonOrgJsonProviderTest extends BaseTest {
assertThat(result).isEqualTo(4); assertThat(result).isEqualTo(4);
} }
@Test
public void read_book_length_using_translated_query_with_filter() {
Object result = using(Configuration.defaultConfiguration())
.parse(JSON_BOOK_STORE_DOCUMENT)
.read("$..[?(@.category == \"fiction\")].length()");
assertThat(result).isEqualTo(3);
}
@Test @Test
public void read_book_length() { public void read_book_length() {
Object result = using(Configuration.defaultConfiguration()) Object result = using(Configuration.defaultConfiguration())
@ -76,4 +84,71 @@ public class JsonOrgJsonProviderTest extends BaseTest {
assertThat(result).isEqualTo(4); assertThat(result).isEqualTo(4);
} }
@Test
public void test(){
String json = "[\n" +
" {\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"category\": \"reference\",\n" +
" \"price\": 8.95,\n" +
" \"title\": \"Sayings of the Century\"\n" +
" },\n" +
" {\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"category\": \"fiction\",\n" +
" \"price\": 12.99,\n" +
" \"title\": \"Sword of Honour\"\n" +
" },\n" +
" {\n" +
" \"author\": \"Herman Melville\",\n" +
" \"category\": \"fiction\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99,\n" +
" \"title\": \"Moby Dick\"\n" +
" },\n" +
" {\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"category\": \"fiction\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99,\n" +
" \"title\": \"The Lord of the Rings\"\n" +
" }\n" +
"]";
Object result = JsonPath.read(json,"$..[?(@.price < 10)].length()");
assertThat(result).isEqualTo(2);
}
@Test
public void test2(){
String json = "[\n" +
" {\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"category\": \"reference\",\n" +
" \"price\": 8.95,\n" +
" \"title\": \"Sayings of the Century\"\n" +
" },\n" +
" {\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"category\": \"fiction\",\n" +
" \"price\": 12.99,\n" +
" \"title\": \"Sword of Honour\"\n" +
" },\n" +
" {\n" +
" \"author\": \"Herman Melville\",\n" +
" \"category\": \"fiction\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99,\n" +
" \"title\": \"Moby Dick\"\n" +
" },\n" +
" {\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"category\": \"fiction\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99,\n" +
" \"title\": \"The Lord of the Rings\"\n" +
" }\n" +
"]";
Object result = JsonPath.read(json,"$..[?(@.price == 22.99)].length()");
assertThat(result).isEqualTo(1);
}
} }

Loading…
Cancel
Save