Browse Source

Improved escape handling and fixed JsonValueNode issue.

pull/169/head
Kalle Stenflo 9 years ago
parent
commit
ac83feea14
  1. 70
      json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  3. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  4. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  5. 9
      json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java
  6. 4
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/TapestryMappingProvider.java
  7. 2
      json-path/src/test/java/com/jayway/jsonpath/FilterCompilerTest.java
  8. 21
      json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

70
json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java

@ -77,51 +77,22 @@ public class CharacterIndex {
int readPosition = startPosition + 1;
while (inBounds(readPosition)) {
if (skipStrings) {
if (charAt(readPosition) == SINGLE_QUOTE) {
boolean escaped = false;
while (inBounds(readPosition)) {
readPosition++;
if (escaped) {
escaped = false;
continue;
}
if (charAt(readPosition) == ESCAPE) {
escaped = true;
continue;
}
if (charAt(readPosition) == SINGLE_QUOTE) {
readPosition++;
break;
}
}
} else if (charAt(readPosition) == DOUBLE_QUOTE) {
boolean escaped = false;
while (inBounds(readPosition)) {
readPosition++;
if (escaped) {
escaped = false;
continue;
}
if (charAt(readPosition) == ESCAPE) {
escaped = true;
continue;
}
if (charAt(readPosition) == DOUBLE_QUOTE) {
readPosition++;
break;
}
char quoteChar = charAt(readPosition);
if (quoteChar == SINGLE_QUOTE || quoteChar == DOUBLE_QUOTE){
readPosition = nextIndexOfUnescaped(readPosition, quoteChar);
if(readPosition == -1){
throw new InvalidPathException("Could not find matching close quote for " + quoteChar + " when parsing : " + charSequence);
}
readPosition++;
}
}
if (skipRegex) {
if (charAt(readPosition) == REGEX) {
while (inBounds(readPosition)) {
readPosition++;
if (charAt(readPosition) == REGEX) {
readPosition++;
break;
}
readPosition = nextIndexOfUnescaped(readPosition, REGEX);
if(readPosition == -1){
throw new InvalidPathException("Could not find matching close for " + REGEX + " when parsing regex in : " + charSequence);
}
readPosition++;
}
}
if (charAt(readPosition) == openChar) {
@ -174,23 +145,22 @@ public class CharacterIndex {
}
public int nextIndexOfUnescaped(char c) {
return nextIndexOfUnescaped(position + 1, c);
return nextIndexOfUnescaped(position, c);
}
public int nextIndexOfUnescaped(int startPosition, char c) {
int readPosition = startPosition;
char prev1;
char prev2;
while (!isOutOfBounds(readPosition)) {
prev1 = charAtOr(readPosition - 1, ' ');
prev2 = charAtOr(readPosition - 2, ' ');
boolean ignore = (prev1 == '\\' && prev2 == '\\');
boolean escaped = (prev1 == '\\' && !ignore);
if (charAt(readPosition) == c && !escaped) {
int readPosition = startPosition + 1;
boolean inEscape = false;
while (!isOutOfBounds(readPosition)) {
if(inEscape){
inEscape = false;
} else if('\\' == charAt(readPosition)){
inEscape = true;
} else if (c == charAt(readPosition) && !inEscape){
return readPosition;
}
readPosition++;
readPosition ++;
}
return -1;
}

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

@ -218,8 +218,6 @@ public abstract class PathRef implements Comparable<PathRef> {
}
}
private static class ObjectPropertyPathRef extends PathRef {
private String property;

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

@ -174,8 +174,8 @@ public abstract class ValueNode {
if(o instanceof ValueNode) return (ValueNode)o;
if(o instanceof Class) return createClassNode((Class)o);
else if(isPath(o)) return new PathNode(o.toString(), false, false);
else if(isJson(o)) return createStringNode(o.toString(), false);
else if(o instanceof String) return createStringNode(o.toString(), false);
else if(isJson(o)) return createJsonNode(o.toString());
else if(o instanceof String) return createStringNode(o.toString(), true);
else if(o instanceof Character) return createStringNode(o.toString(), false);
else if(o instanceof Number) return createNumberNode(o.toString());
else if(o instanceof Boolean) return createBooleanNode(o.toString());

1
json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

@ -256,7 +256,6 @@ public class PathCompiler {
Predicate predicate = FilterCompiler.compile(criteria);
//Predicate predicate = Filter.parse(criteria);
appender.appendPathToken(PathTokenFactory.createPredicatePathToken(predicate));
path.setPosition(closeStatementBracketIndex + 1);

9
json-path/src/main/java/com/jayway/jsonpath/spi/json/TapestryJsonProvider.java

@ -1,14 +1,13 @@
package com.jayway.jsonpath.spi.json;
import java.io.InputStream;
import java.util.Collection;
import java.util.Scanner;
import com.jayway.jsonpath.InvalidJsonException;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONCollection;
import org.apache.tapestry5.json.JSONObject;
import com.jayway.jsonpath.InvalidJsonException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Scanner;
public class TapestryJsonProvider extends AbstractJsonProvider {

4
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/TapestryMappingProvider.java

@ -14,11 +14,11 @@
*/
package com.jayway.jsonpath.spi.mapper;
import java.util.ArrayList;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.TypeRef;
import java.util.ArrayList;
public class TapestryMappingProvider implements MappingProvider {
@Override

2
json-path/src/test/java/com/jayway/jsonpath/FilterCompilerTest.java

@ -81,7 +81,7 @@ public class FilterCompilerTest {
compile(filter);
throw new AssertionError("Expected " + filter + " to throw InvalidPathException");
} catch (InvalidPathException e){
e.printStackTrace();
//e.printStackTrace();
}
}
}

21
json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

@ -186,6 +186,27 @@ public class InlineFilterTest extends BaseTest {
assertHasOneResult("[{\"value\":5.1}]", "$[?(@.value<7)]", conf);
assertHasNoResults("[{\"value\":7.1}]", "$[?(@.value<5)]", conf);
}
@Test
public void escaped_literals() {
if(conf.jsonProvider().getClass().getSimpleName().startsWith("Jackson")){
return;
}
assertHasOneResult("[\"\\'foo\"]", "$[?(@ == '\\'foo')]", conf);
}
@Test
public void escaped_literals2() {
if(conf.jsonProvider().getClass().getSimpleName().startsWith("Jackson")){
return;
}
assertHasOneResult("[\"\\\\'foo\"]", "$[?(@ == \"\\\\'foo\")]", conf);
}
@Test
public void escape_pattern() {
assertHasOneResult("[\"x\"]", "$[?(@ =~ /\\/|x/)]", conf);
}
}

Loading…
Cancel
Save