Skip to content

Commit

Permalink
fix issue273 (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengmmm00 authored Jan 30, 2022
1 parent ae253ab commit 72fed27
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public class EvaluationContextImpl implements EvaluationContext {
private int resultIndex = 0;


public RootPathToken getRoot(){
return ((CompiledPath) path).getRoot();
}

public EvaluationContextImpl(Path path, Object rootDocument, Configuration configuration, boolean forUpdate) {
notNull(path, "path can not be null");
notNull(rootDocument, "root can not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public abstract class PathToken {
private PathToken next;
private Boolean definite = null;
private Boolean upstreamDefinite = null;
private int upstreamArrayIndex = -1;


public void setUpstreamArrayIndex(int idx){
upstreamArrayIndex = idx;
}

PathToken appendTailToken(PathToken next) {
this.next = next;
Expand Down Expand Up @@ -75,7 +81,10 @@ void handleObjectProperty(String currentPath, Object model, EvaluationContextImp
}
PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, property) : PathRef.NO_OP;
if (isLeaf()) {
ctx.addResult(evalPath, pathRef, propertyVal);
String idx = "[" + String.valueOf(upstreamArrayIndex) + "]";
if(idx.equals("[-1]") || ctx.getRoot().getTail().prev().getPathFragment().equals(idx)){
ctx.addResult(evalPath, pathRef, propertyVal);
}
}
else {
next().evaluate(evalPath, pathRef, propertyVal, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class RootPathToken extends PathToken {
this.tokenCount = 1;
}

public PathToken getTail(){
return this.tail;
}

@Override
public int getTokenCount() {
return tokenCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void walkArray(PathToken pt, String currentPath, PathRef parent, O
int idx = 0;
for (Object evalModel : models) {
String evalPath = currentPath + "[" + idx + "]";
next.setUpstreamArrayIndex(idx);
next.evaluate(evalPath, parent, evalModel, ctx);
idx++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.jayway.jsonpath.internal.function;

import net.minidev.json.JSONArray;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;


import static org.junit.Assert.assertEquals;

public class Issue273 {
@Test
public void testGetPropertyFromArray(){
String json = "[\n" +
" [\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" +
" {\n" +
" \"color\" : \"red\",\n" +
" \"price\" : 19.95\n" +
" }\n" +
"]\n";

JSONArray arr = JsonPath.read(json,"$..[2].author");
assertEquals(arr.get(0), "Herman Melville");
}

@Test
public void testGetPropertyFromObject(){
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" +
" ";
JSONArray arr = JsonPath.read(json,"$..[2].author");
assertEquals(arr.get(0), "Herman Melville");
}
}

0 comments on commit 72fed27

Please sign in to comment.