Browse Source

Merge pull request #194 from jochenberger/fix-array-subscriptions

PR for #173
pull/195/head
kallestenflo 9 years ago
parent
commit
91f21d01f2
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java
  2. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  3. 15
      json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java

9
json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java

@ -6,11 +6,14 @@ import com.jayway.jsonpath.internal.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import static java.lang.Character.isDigit; import static java.lang.Character.isDigit;
public class ArrayIndexOperation { public class ArrayIndexOperation {
private final static Pattern COMMA = Pattern.compile("\\s*,\\s*");
private final List<Integer> indexes; private final List<Integer> indexes;
private ArrayIndexOperation(List<Integer> indexes) { private ArrayIndexOperation(List<Integer> indexes) {
@ -39,13 +42,13 @@ public class ArrayIndexOperation {
//check valid chars //check valid chars
for (int i = 0; i < operation.length(); i++) { for (int i = 0; i < operation.length(); i++) {
char c = operation.charAt(i); char c = operation.charAt(i);
if (!isDigit(c) && c != ',') { if (!isDigit(c) && c != ',' && c != ' ') {
throw new InvalidPathException("Failed to parse ArrayIndexOperation: " + operation); throw new InvalidPathException("Failed to parse ArrayIndexOperation: " + operation);
} }
} }
String[] tokens = operation.split(","); String[] tokens = COMMA.split(operation, -1);
List<Integer> tempIndexes = new ArrayList<Integer>(); List<Integer> tempIndexes = new ArrayList<Integer>(tokens.length);
for (String token : tokens) { for (String token : tokens) {
tempIndexes.add(parseInteger(token)); tempIndexes.add(parseInteger(token));
} }

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

@ -512,7 +512,7 @@ public class PathCompiler {
return false; return false;
} }
String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().replace(" ", ""); String expression = path.subSequence(expressionBeginIndex, expressionEndIndex).toString().trim();
if ("*".equals(expression)) { if ("*".equals(expression)) {
return false; return false;
@ -521,7 +521,7 @@ public class PathCompiler {
//check valid chars //check valid chars
for (int i = 0; i < expression.length(); i++) { for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i); char c = expression.charAt(i);
if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT) { if (!isDigit(c) && c != COMMA && c != MINUS && c != SPLIT && c != SPACE) {
return false; return false;
} }
} }
@ -560,6 +560,7 @@ public class PathCompiler {
int endPosition = 0; int endPosition = 0;
boolean inProperty = false; boolean inProperty = false;
boolean inEscape = false; boolean inEscape = false;
boolean lastSignificantWasComma = false;
while (path.inBounds(readPosition)) { while (path.inBounds(readPosition)) {
char c = path.charAt(readPosition); char c = path.charAt(readPosition);
@ -569,6 +570,9 @@ public class PathCompiler {
} else if('\\' == c){ } else if('\\' == c){
inEscape = true; inEscape = true;
} else if (c == CLOSE_SQUARE_BRACKET && !inProperty) { } else if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
if (lastSignificantWasComma){
fail("Found empty property at index "+readPosition);
}
break; break;
} else if (c == potentialStringDelimiter) { } else if (c == potentialStringDelimiter) {
if (inProperty && !inEscape) { if (inProperty && !inEscape) {
@ -579,7 +583,13 @@ public class PathCompiler {
} else { } else {
startPosition = readPosition + 1; startPosition = readPosition + 1;
inProperty = true; inProperty = true;
lastSignificantWasComma = false;
}
} else if (c == COMMA){
if (lastSignificantWasComma){
fail("Found empty property at index "+readPosition);
} }
lastSignificantWasComma = true;
} }
readPosition++; readPosition++;
} }

15
json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java

@ -251,4 +251,19 @@ public class PathCompilerTest {
assertThat(result).containsExactly("] it"); assertThat(result).containsExactly("] it");
} }
@Test(expected = InvalidPathException.class)
public void array_indexes_must_be_separated_by_commas() {
compile("$[0, 1, 2 4]");
}
@Test(expected = InvalidPathException.class)
public void trailing_comma_after_list_is_not_accepted() {
compile("$['1','2',]");
}
@Test(expected = InvalidPathException.class)
public void accept_only_a_single_comma_between_indexes() {
compile("$['1', ,'3']");
}
} }

Loading…
Cancel
Save