Browse Source

Fix issue json-path#562 and add a testcase

pull/701/head
SUSTech-11810721 4 years ago
parent
commit
e629933008
  1. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java
  2. 43
      json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java

4
json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java

@ -60,14 +60,14 @@ public class CompiledPath implements Path {
* A function with the scanner as input, or if this situation doesn't exist just the input path * A function with the scanner as input, or if this situation doesn't exist just the input path
*/ */
private RootPathToken invertScannerFunctionRelationship(final RootPathToken path) { private RootPathToken invertScannerFunctionRelationship(final RootPathToken path) {
if (path.isFunctionPath() && path.next() instanceof ScanPathToken) { if (path.isFunctionPath()) {
PathToken token = path; PathToken token = path;
PathToken prior = null; PathToken prior = null;
while (null != (token = token.next()) && !(token instanceof FunctionPathToken)) { while (null != (token = token.next()) && !(token instanceof FunctionPathToken)) {
prior = token; prior = token;
} }
// Invert the relationship $..path.function() to $.function($..path) // Invert the relationship $..path.function() to $.function($..path)
if (token instanceof FunctionPathToken) { if (path.next() instanceof ScanPathToken || (prior instanceof PredicatePathToken && prior.toString().equals("[?].length()") && token != null)) {
prior.setNext(null); prior.setNext(null);
path.setTail(prior); path.setTail(prior);

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

@ -151,4 +151,47 @@ public class JsonOrgJsonProviderTest extends BaseTest {
Object result = JsonPath.read(json,"$..[?(@.price == 22.99)].length()"); Object result = JsonPath.read(json,"$..[?(@.price == 22.99)].length()");
assertThat(result).isEqualTo(1); assertThat(result).isEqualTo(1);
} }
@Test
public void test_issue_562() {
String json = "{\n" +
" \"store\": {\n" +
" \"book\": [\n" +
" {\n" +
" \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" },\n" +
" \"expensive\": 10\n" +
"}\n";
Object result = JsonPath.read(json,"$.store.book[?(@.price>10)].length()");
assertThat(result).isEqualTo(2);
}
} }

Loading…
Cancel
Save