Browse Source

fail when there are spaces inside array indexes

pull/194/head
Jochen Berger 9 years ago
parent
commit
4bfa932cdf
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.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));
} }

4
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;
} }
} }

Loading…
Cancel
Save