Browse Source

Added filters to the cache key #94

pull/137/head
gauravgupta 9 years ago
parent
commit
9bc0f8b213
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  2. 30
      json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java

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

@ -32,11 +32,13 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull;
import static java.util.Arrays.asList;
public class JsonReader implements ParseContext, DocumentContext {
@ -135,7 +137,12 @@ public class JsonReader implements ParseContext, DocumentContext {
public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty");
CacheProvider cache = configuration.CacheProvider();
JsonPath jsonPath = cache.get(path);
path = path.trim();
LinkedList filterStack = new LinkedList<Predicate>(asList(filters));
String cacheKey = Utils.concat(path, filterStack.toString());
JsonPath jsonPath = cache.get(cacheKey);
if(jsonPath != null){
return read(jsonPath);
}else {

30
json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java

@ -0,0 +1,30 @@
package com.jayway.jsonpath.internal;
import static org.junit.Assert.*;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
public class JsonReaderTest extends BaseTest {
@Test
public void cached_path_with_predicates() {
Filter feq = Filter.filter(Criteria.where("category").eq("reference"));
Filter fne = Filter.filter(Criteria.where("category").ne("reference"));
List<String> eq = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?].category", feq);
List<String> ne = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[?].category", fne);
Assertions.assertThat(eq).contains("reference");
Assertions.assertThat(ne).doesNotContain("reference");
}
}
Loading…
Cancel
Save