Browse Source

Fixed bracket bug in array filters.

pull/6/head
Kalle Stenflo 13 years ago
parent
commit
2933d3be25
  1. 21
      json-path-assert/pom.xml
  2. 13
      json-path/pom.xml
  3. 13
      json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java
  4. 9
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java
  5. 9
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java
  6. 3
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  7. 21
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  8. 20
      pom.xml

21
json-path-assert/pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
@ -33,18 +34,6 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
-->
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
@ -54,5 +43,11 @@
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

13
json-path/pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
@ -48,15 +49,17 @@
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies --> <!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.hamcrest</groupId>
<artifactId>junit</artifactId> <artifactId>hamcrest-library</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>junit</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

13
json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java

@ -33,7 +33,7 @@ public class PathTokenizer implements Iterable<PathToken> {
} }
} }
public List<String> getFragments(){ public List<String> getFragments() {
List<String> fragments = new LinkedList<String>(); List<String> fragments = new LinkedList<String>();
for (PathToken pathToken : pathTokens) { for (PathToken pathToken : pathTokens) {
fragments.add(pathToken.getFragment()); fragments.add(pathToken.getFragment());
@ -106,9 +106,19 @@ public class PathTokenizer implements Iterable<PathToken> {
private String extract(boolean includeSopChar, char... stopChars) { private String extract(boolean includeSopChar, char... stopChars) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
while (!isEmpty() && (!isStopChar(peek(), stopChars))) { while (!isEmpty() && (!isStopChar(peek(), stopChars))) {
if (peek() == '(') {
do {
sb.append(poll());
} while (peek() != ')');
sb.append(poll());
} else {
char c = poll(); char c = poll();
if (isStopChar(c, stopChars)) { if (isStopChar(c, stopChars)) {
@ -119,6 +129,7 @@ public class PathTokenizer implements Iterable<PathToken> {
sb.append(c); sb.append(c);
} }
} }
}
if (includeSopChar) { if (includeSopChar) {
assertValidPeek(false, stopChars); assertValidPeek(false, stopChars);
sb.append(poll()); sb.append(poll());

9
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java

@ -29,7 +29,14 @@ public class ArrayEvalFilter extends Filter {
List<Object> src = jsonProvider.toList(obj); List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList(); List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2); String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
trimmedCondition = trim(trimmedCondition, 5, 2);
ConditionStatement conditionStatement = createConditionStatement(trimmedCondition); ConditionStatement conditionStatement = createConditionStatement(trimmedCondition);

9
json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java

@ -24,7 +24,14 @@ public class HasFieldFilter extends Filter {
List<Object> src = jsonProvider.toList(obj); List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList(); List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2); String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
trimmedCondition = trim(trimmedCondition, 5, 2);
for (Object item : src) { for (Object item : src) {
if(jsonProvider.isMap(item)){ if(jsonProvider.isMap(item)){

3
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -1,5 +1,6 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.util.ScriptEngineJsonPath; import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test; import org.junit.Test;
@ -212,7 +213,9 @@ public class JsonPathTest {
@Test @Test
public void all_books_cheaper_than_10() throws Exception { public void all_books_cheaper_than_10() throws Exception {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@['display-price'] < 10)].title"), hasItems("Sayings of the Century", "Moby Dick")); assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@['display-price'] < 10)].title"), hasItems("Sayings of the Century", "Moby Dick"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@.display-price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick"));
} }

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

@ -19,6 +19,27 @@ public class SplitPathFragmentsTest {
private JsonProvider jsonProvider = JsonProvider.getInstance(); private JsonProvider jsonProvider = JsonProvider.getInstance();
@Test
public void array_filter_bracket_test() throws Exception {
PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@['isbn'])].isbn", jsonProvider);
//PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@.isbn)].isbn", jsonProvider);
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment);
}
//assertThat(tokenizer.getFragments(), hasItems("$", "store", "[*]"));
}
@Test @Test
public void valid_path_is_split_correctly() throws Exception { public void valid_path_is_split_correctly() throws Exception {

20
pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and ~ See the License for the specific language governing permissions and
~ limitations under the License. ~ limitations under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.sonatype.oss</groupId> <groupId>org.sonatype.oss</groupId>
@ -31,7 +32,7 @@
<inceptionYear>2011</inceptionYear> <inceptionYear>2011</inceptionYear>
<issueManagement> <issueManagement>
<system>GitHub Issue Tracking</system> <system>GitHub Issue Tracking</system>
<url /> <url/>
</issueManagement> </issueManagement>
<licenses> <licenses>
<license> <license>
@ -165,12 +166,23 @@
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version> <version>1.7.1</version>
</dependency> </dependency>
<!--
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId> <artifactId>hamcrest-library</artifactId>
<version>1.1</version> <version>1.1</version>
</dependency> </dependency>
-->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
@ -181,7 +193,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.8.2</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

Loading…
Cancel
Save