From b4a29682453c7637775153893c27068531237b14 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Tue, 24 Nov 2015 16:38:06 +0100 Subject: [PATCH] accept double quotes as string delimiters in filters --- .../jsonpath/internal/filter/FilterCompiler.java | 10 ++++++---- .../java/com/jayway/jsonpath/PathCompilerTest.java | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java index 753e4a99..3ef2694b 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java @@ -20,6 +20,7 @@ public class FilterCompiler { private static final char SPACE = ' '; private static final char MINUS = '-'; private static final char TICK = '\''; + private static final char DOUBLE_QUOTE = '"'; private static final char PERIOD = '.'; private static final char LT = '<'; private static final char GT = '>'; @@ -133,7 +134,8 @@ public class FilterCompiler { private ValueNode readLiteral(){ switch (filter.skipBlanks().currentChar()){ - case TICK: return readStringLiteral(); + case TICK: return readStringLiteral(TICK); + case DOUBLE_QUOTE: return readStringLiteral(DOUBLE_QUOTE); case TRUE: return readBooleanLiteral(); case FALSE: return readBooleanLiteral(); case MINUS: return readNumberLiteral(); @@ -245,12 +247,12 @@ public class FilterCompiler { return ValueNode.createPatternNode(pattern); } - private ValueNode.StringNode readStringLiteral() { + private ValueNode.StringNode readStringLiteral(char endChar) { int begin = filter.position(); - int closingTickIndex = filter.nextIndexOfUnescaped(TICK); + int closingTickIndex = filter.nextIndexOfUnescaped(endChar); if (closingTickIndex == -1) { - throw new InvalidPathException("String not closed. Expected " + TICK + " in " + filter); + throw new InvalidPathException("String not closed. Expected " + endChar + " in " + filter); } else { filter.setPosition(closingTickIndex + 1); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java index 6c11e970..2c232cca 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java @@ -109,6 +109,7 @@ public class PathCompilerTest { @Test public void an_inline_criteria_can_be_parsed() { assertThat(compile("$[?(@.foo == 'bar')]").toString()).isEqualTo("$[?]"); + assertThat(compile("$[?(@.foo == \"bar\")]").toString()).isEqualTo("$[?]"); } @Test