Browse Source

Fixed issue 36 - deep scan for arrays.

pull/10/merge
Kalle Stenflo 11 years ago
parent
commit
5f8beff812
  1. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  2. 2
      json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java
  3. 21
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  4. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

5
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -54,7 +54,10 @@ public class FieldFilter extends PathTokenFilter {
if(split.length == 1){
if (keys.contains(condition)) {
Object o = jsonProvider.getProperty(current, condition);
if (jsonProvider.isArray(o)) {
boolean isArr = jsonProvider.isArray(o);
boolean isEnd = pathToken.isEndToken();
if (isArr && !isEnd) {
for(Object item : jsonProvider.toIterable(o)){
jsonProvider.setProperty(result, jsonProvider.length(result), item);
}

2
json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java

@ -2,6 +2,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.spi.HttpProviderFactory;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
@ -16,6 +17,7 @@ import static junit.framework.Assert.assertEquals;
* Date: 3/10/12
* Time: 8:12 AM
*/
@Ignore
public class HttpProviderTest {

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

@ -25,6 +25,27 @@ import static org.junit.Assert.assertThat;
*/
public class IssuesTest {
@Test
public void issue_36() {
String json = "{\n" +
"\n" +
" \"arrayOfObjectsAndArrays\" : [ { \"k\" : [\"json\"] }, { \"k\":[\"path\"] }, { \"k\" : [\"is\"] }, { \"k\" : [\"cool\"] } ],\n" +
"\n" +
" \"arrayOfObjects\" : [{\"k\" : \"json\"}, {\"k\":\"path\"}, {\"k\" : \"is\"}, {\"k\" : \"cool\"}]\n" +
"\n" +
" }";
Object o1 = JsonPath.read(json, "$.arrayOfObjectsAndArrays..k ");
Object o2 = JsonPath.read(json, "$.arrayOfObjects..k ");
assertEquals("[[\"json\"],[\"path\"],[\"is\"],[\"cool\"]]", o1.toString());
assertEquals("[\"json\",\"path\",\"is\",\"cool\"]", o2.toString());
}
@Test(expected = PathNotFoundException.class)
public void issue_11() throws Exception {
String json = "{ \"foo\" : [] }";

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

@ -276,7 +276,7 @@ public class JsonPathTest {
//List<String> books = JsonPath.<List<String>>read(DOCUMENT, "$..book");
Object books = JsonPath.<List<String>>read(DOCUMENT, "$..book");
System.out.println("test");
System.out.println(books);
}

Loading…
Cancel
Save