Browse Source

Merge pull request #157 from jochenberger/support-double-quotes

Support double quotes
pull/158/head
kallestenflo 9 years ago
parent
commit
eafc9ae14f
  1. 2
      README.md
  2. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  3. 1
      json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java

2
README.md

@ -82,7 +82,7 @@ The function output is dictated by the function itself.
Filter Operators
-----------------
Filters are logical expressions used to filter arrays. A typical filter would be `[?(@.age > 18)]` where `@` represents the current item being processed. More complex filters can be created with logical operators `&&` and `||`. String literals must be enclosed by single quotes `[?(@.color == 'blue')]`.
Filters are logical expressions used to filter arrays. A typical filter would be `[?(@.age > 18)]` where `@` represents the current item being processed. More complex filters can be created with logical operators `&&` and `||`. String literals must be enclosed by single or double quotes (`[?(@.color == 'blue')]` or `[?(@.color == "blue")]`).
| Operator | Description |
| :----------------------- | :---------------------------------------------------------------- |

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

@ -29,6 +29,7 @@ public class PathCompiler {
private static final char SPLIT = ':';
private static final char MINUS = '-';
private static final char TICK = '\'';
private static final char DOUBLE_QUOTE = '"';
private final LinkedList<Predicate> filterStack;
private final CharacterIndex path;
@ -345,9 +346,13 @@ public class PathCompiler {
// ['foo']
//
private boolean readBracketPropertyToken(PathTokenAppender appender) {
if (!path.currentCharIs(OPEN_SQUARE_BRACKET) || !path.nextSignificantCharIs(TICK)) {
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) {
return false;
}
char potentialStringDelimiter = path.nextSignificantChar();
if (potentialStringDelimiter != TICK && potentialStringDelimiter != DOUBLE_QUOTE) {
return false;
}
List<String> properties = new ArrayList<String>();
@ -361,7 +366,7 @@ public class PathCompiler {
if (c == CLOSE_SQUARE_BRACKET && !inProperty) {
break;
} else if (c == TICK) {
} else if (c == potentialStringDelimiter) {
if (inProperty) {
endPosition = readPosition;
properties.add(path.subSequence(startPosition, endPosition).toString());

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

@ -59,6 +59,7 @@ public class PathCompilerTest {
assertThat(compile("$['1prop']").toString()).isEqualTo("$['1prop']");
assertThat(compile("$['@prop']").toString()).isEqualTo("$['@prop']");
assertThat(compile("$[ '@prop' ]").toString()).isEqualTo("$['@prop']");
assertThat(compile("$[\"prop\"]").toString()).isEqualTo("$['prop']");
}
@Test

Loading…
Cancel
Save