Browse Source

Fixed issue with attribs named '$'.

pull/29/head
Kalle Stenflo 11 years ago
parent
commit
58c0474f29
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 3
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  3. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
  4. 20
      json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
  5. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java
  6. 18
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

2
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -297,7 +297,7 @@ public class Criteria {
return objectOrAnyCollectionItemMatches(actualVal, new Predicate<Object>() { return objectOrAnyCollectionItemMatches(actualVal, new Predicate<Object>() {
@Override @Override
public boolean accept(Object value) { public boolean accept(Object value) {
if (isValue == null) { if (isValue == null) {
return value == null; return value == null;
} else { } else {
return isValue.equals(value); return isValue.equals(value);

3
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -115,6 +115,9 @@ public class JsonPath {
isTrue(filterCountInPath == filters.length, "Filters in path ([?]) does not match provided filters."); isTrue(filterCountInPath == filters.length, "Filters in path ([?]) does not match provided filters.");
this.tokenizer = new PathTokenizer(jsonPath); this.tokenizer = new PathTokenizer(jsonPath);
//System.out.println(tokenizer.toString());
this.filters = new LinkedList<Filter>(); this.filters = new LinkedList<Filter>();
this.filters.addAll(asList(filters)); this.filters.addAll(asList(filters));

13
json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java

@ -31,20 +31,23 @@ public class PathToken {
private String fragment; private String fragment;
public PathToken(String fragment) { private int tokenIndex;
public PathToken(String fragment, int tokenIndex) {
this.fragment = fragment; this.fragment = fragment;
this.tokenIndex = tokenIndex;
} }
public PathTokenFilter getFilter(){ public PathTokenFilter getFilter(){
return FilterFactory.createFilter(fragment); return FilterFactory.createFilter(this);
} }
public Object filter(Object model, JsonProvider jsonProvider){ public Object filter(Object model, JsonProvider jsonProvider){
return FilterFactory.createFilter(fragment).filter(model, jsonProvider); return FilterFactory.createFilter(this).filter(model, jsonProvider);
} }
public Object apply(Object model, JsonProvider jsonProvider){ public Object apply(Object model, JsonProvider jsonProvider){
return FilterFactory.createFilter(fragment).getRef(model, jsonProvider); return FilterFactory.createFilter(this).getRef(model, jsonProvider);
} }
public String getFragment() { public String getFragment() {
@ -52,7 +55,7 @@ public class PathToken {
} }
public boolean isRootToken(){ public boolean isRootToken(){
return "$".equals(fragment); return this.tokenIndex == 0;
} }
public boolean isArrayIndexToken(){ public boolean isArrayIndexToken(){
return ARRAY_INDEX_PATTERN.matcher(fragment).matches(); return ARRAY_INDEX_PATTERN.matcher(fragment).matches();

20
json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java

@ -16,6 +16,7 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
import java.util.Formatter;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -37,8 +38,11 @@ public class PathTokenizer implements Iterable<PathToken> {
} }
this.pathChars = jsonPath.toCharArray(); this.pathChars = jsonPath.toCharArray();
int i = 0;
for (String pathFragment : splitPath()) { for (String pathFragment : splitPath()) {
pathTokens.add(new PathToken(pathFragment)); pathTokens.add(new PathToken(pathFragment, i));
i++;
} }
} }
@ -255,4 +259,18 @@ public class PathTokenizer implements Iterable<PathToken> {
} }
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("-----------------------------------------------------------------").append("\n");
sb.append("PATH: ").append(getPath()).append("\n");
sb.append(String.format("%-50s%-10s%-10s", "Fragment", "Root", "Array")).append("\n");
sb.append("-----------------------------------------------------------------").append("\n");
for (PathToken pathToken : pathTokens) {
sb.append(String.format("%-50s%-10b%-10b", pathToken.getFragment(), pathToken.isRootToken(), pathToken.isArrayIndexToken())).append("\n");;
}
return sb.toString();
}
} }

8
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterFactory.java

@ -14,6 +14,8 @@
*/ */
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.internal.PathToken;
/** /**
* @author Kalle Stenflo * @author Kalle Stenflo
*/ */
@ -25,9 +27,11 @@ public class FilterFactory {
private final static PathTokenFilter SCAN_FILTER = new ScanFilter(".."); private final static PathTokenFilter SCAN_FILTER = new ScanFilter("..");
private final static PathTokenFilter ARRAY_QUERY_FILTER = new ArrayQueryFilter("[?]"); private final static PathTokenFilter ARRAY_QUERY_FILTER = new ArrayQueryFilter("[?]");
public static PathTokenFilter createFilter(String pathFragment) { public static PathTokenFilter createFilter(PathToken token) {
String pathFragment = token.getFragment();
if (DOCUMENT_FILTER.getCondition().equals(pathFragment)) { //"$" if (DOCUMENT_FILTER.getCondition().equals(pathFragment) && token.isRootToken()) { //"$"
return DOCUMENT_FILTER; return DOCUMENT_FILTER;

18
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -1,6 +1,8 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.IOUtils; import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.spi.impl.JacksonProvider;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -149,6 +151,22 @@ public class IssuesTest {
assertTrue(result.size() == 1); assertTrue(result.size() == 1);
} }
@Test
public void issue_30() throws Exception {
String json = "{\"foo\" : {\"@id\" : \"123\", \"$\" : \"hello\"}}";
assertEquals("123", JsonPath.read(json, "foo.@id"));
assertEquals("hello", JsonPath.read(json, "foo.$"));
}
@Test
public void issue_32(){
String json = "{\"text\" : \"skill: \\\"Heuristic Evaluation\\\"\", \"country\" : \"\"}";
assertEquals("skill: \"Heuristic Evaluation\"", JsonPath.read(json, "$.text"));
}
} }

Loading…
Cancel
Save