Browse Source

HttpProvider SPI.

Removed commons-io dependency.
pull/8/merge
Kalle Stenflo 13 years ago
parent
commit
1216133e18
  1. 6
      json-path/pom.xml
  2. 3
      json-path/src/main/java/com/jayway/jsonpath/Filter.java
  3. 10
      json-path/src/main/java/com/jayway/jsonpath/InvalidModelException.java
  4. 111
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  5. 13
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  6. 13
      json-path/src/main/java/com/jayway/jsonpath/Transformer.java
  7. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/Util.java
  8. 10
      json-path/src/main/java/com/jayway/jsonpath/spi/HttpProvider.java
  9. 10
      json-path/src/main/java/com/jayway/jsonpath/spi/HttpProviderFactory.java
  10. 10
      json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java
  11. 34
      json-path/src/test/java/com/jayway/jsonpath/JsonModelMappingTest.java
  12. 71
      json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java
  13. 10
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  14. 5
      pom.xml

6
json-path/pom.xml

@ -32,16 +32,12 @@
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.codehaus.jackson</groupId> <groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>

3
json-path/src/main/java/com/jayway/jsonpath/Filter.java

@ -120,8 +120,5 @@ public abstract class Filter<T> {
} }
return true; return true;
} }
} }
} }

10
json-path/src/main/java/com/jayway/jsonpath/InvalidModelPathException.java → json-path/src/main/java/com/jayway/jsonpath/InvalidModelException.java

@ -18,21 +18,21 @@ package com.jayway.jsonpath;
* @author Kalle Stenflo * @author Kalle Stenflo
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class InvalidModelPathException extends RuntimeException { public class InvalidModelException extends RuntimeException {
public InvalidModelPathException() { public InvalidModelException() {
super(); super();
} }
public InvalidModelPathException(String message) { public InvalidModelException(String message) {
super(message); super(message);
} }
public InvalidModelPathException(String message, Throwable cause) { public InvalidModelException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public InvalidModelPathException(Throwable cause) { public InvalidModelException(Throwable cause) {
super(cause); super(cause);
} }
} }

111
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -15,10 +15,10 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken; import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.Util;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory; import com.jayway.jsonpath.spi.JsonProviderFactory;
import com.jayway.jsonpath.spi.MappingProviderFactory; import com.jayway.jsonpath.spi.MappingProviderFactory;
import org.apache.commons.io.IOUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -57,13 +57,13 @@ public class JsonModel {
* Creates a new JsonModel based on a json document. * Creates a new JsonModel based on a json document.
* Note that the jsonObject must either a {@link List} or a {@link Map} * Note that the jsonObject must either a {@link List} or a {@link Map}
* *
* @param jsonObject the json object * @param jsonObject the json object
* @param jsonProvider * @param jsonProvider
*/ */
private JsonModel(Object jsonObject, JsonProvider jsonProvider) { private JsonModel(Object jsonObject, JsonProvider jsonProvider) {
notNull(jsonObject, "json can not be null"); notNull(jsonObject, "json can not be null");
if (!(jsonObject instanceof Map) && !(jsonObject instanceof List)) { if (!jsonProvider.isContainer(jsonObject)) {
throw new IllegalArgumentException("Invalid container object"); throw new IllegalArgumentException("Invalid container object");
} }
this.jsonProvider = jsonProvider; this.jsonProvider = jsonProvider;
@ -85,7 +85,7 @@ public class JsonModel {
/** /**
* Creates a new JsonModel by fetching the content from the provided URL * Creates a new JsonModel by fetching the content from the provided URL
* *
* @param jsonURL the URL to read * @param jsonURL the URL to read
* @param jsonProvider * @param jsonProvider
* @throws IOException failed to load URL * @throws IOException failed to load URL
*/ */
@ -98,10 +98,18 @@ public class JsonModel {
this.jsonObject = jsonProvider.parse(jsonInputStream); this.jsonObject = jsonProvider.parse(jsonInputStream);
this.jsonProvider = jsonProvider; this.jsonProvider = jsonProvider;
} finally { } finally {
IOUtils.closeQuietly(jsonInputStream); Util.closeQuietly(jsonInputStream);
} }
} }
public boolean isList(){
return jsonProvider.isList(jsonObject);
}
public boolean isMap(){
return jsonProvider.isMap(jsonObject);
}
// -------------------------------------------------------- // --------------------------------------------------------
// //
// Getters // Getters
@ -138,14 +146,22 @@ public class JsonModel {
return opsForArray(JsonPath.compile(jsonPath)); return opsForArray(JsonPath.compile(jsonPath));
} }
public ArrayOps opsForArray() {
return new DefaultArrayOps(this.jsonObject);
}
public ArrayOps opsForArray(JsonPath jsonPath) { public ArrayOps opsForArray(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null"); notNull(jsonPath, "jsonPath can not be null");
List<Object> opsTarget = getTargetObject(jsonPath, List.class); Object opsTarget = getTargetObject(jsonPath, List.class);
return new DefaultArrayOps(opsTarget); return new DefaultArrayOps(opsTarget);
} }
public ObjectOps opsForObject() {
return new DefaultObjectOps(this.jsonObject);
}
public ObjectOps opsForObject(String jsonPath) { public ObjectOps opsForObject(String jsonPath) {
return opsForObject(JsonPath.compile(jsonPath)); return opsForObject(JsonPath.compile(jsonPath));
} }
@ -153,7 +169,7 @@ public class JsonModel {
public ObjectOps opsForObject(JsonPath jsonPath) { public ObjectOps opsForObject(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null"); notNull(jsonPath, "jsonPath can not be null");
Map<String, Object> opsTarget = getTargetObject(jsonPath, Map.class); Object opsTarget = getTargetObject(jsonPath, Map.class);
return new DefaultObjectOps(opsTarget); return new DefaultObjectOps(opsTarget);
} }
@ -192,8 +208,8 @@ public class JsonModel {
Object subModel = jsonPath.read(jsonObject); Object subModel = jsonPath.read(jsonObject);
if (!(subModel instanceof Map) && !(subModel instanceof List)) { if(!jsonProvider.isContainer(subModel)){
throw new InvalidModelPathException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null")); throw new InvalidModelException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null"));
} }
return new JsonModel(subModel, this.jsonProvider); return new JsonModel(subModel, this.jsonProvider);
@ -204,8 +220,12 @@ public class JsonModel {
// Mapping model readers // Mapping model readers
// //
// -------------------------------------------------------- // --------------------------------------------------------
public MappingModelReader map(String jsonPath) { public MappingModelReader map() {
return map(JsonPath.compile(jsonPath)); return new DefaultMappingModelReader(this.jsonObject);
}
public MappingModelReader map(String jsonPath, Filter... filters) {
return map(JsonPath.compile(jsonPath, filters));
} }
public MappingModelReader map(JsonPath jsonPath) { public MappingModelReader map(JsonPath jsonPath) {
@ -219,25 +239,25 @@ public class JsonModel {
// Static factory methods // Static factory methods
// //
// -------------------------------------------------------- // --------------------------------------------------------
public static JsonModel create(String json) { public static JsonModel model(String json) {
notEmpty(json, "json can not be null or empty"); notEmpty(json, "json can not be null or empty");
return new JsonModel(json, JsonProviderFactory.createProvider()); return new JsonModel(json, JsonProviderFactory.createProvider());
} }
public static JsonModel create(Object jsonObject) { public static JsonModel model(Object jsonObject) {
notNull(jsonObject, "jsonObject can not be null"); notNull(jsonObject, "jsonObject can not be null");
return new JsonModel(jsonObject, JsonProviderFactory.createProvider()); return new JsonModel(jsonObject, JsonProviderFactory.createProvider());
} }
public static JsonModel create(URL url) throws IOException { public static JsonModel model(URL url) throws IOException {
notNull(url, "url can not be null"); notNull(url, "url can not be null");
return new JsonModel(url, JsonProviderFactory.createProvider()); return new JsonModel(url, JsonProviderFactory.createProvider());
} }
public static JsonModel create(InputStream jsonInputStream) throws IOException { public static JsonModel model(InputStream jsonInputStream) throws IOException {
notNull(jsonInputStream, "jsonInputStream can not be null"); notNull(jsonInputStream, "jsonInputStream can not be null");
return new JsonModel(jsonInputStream, JsonProviderFactory.createProvider()); return new JsonModel(jsonInputStream, JsonProviderFactory.createProvider());
@ -269,7 +289,7 @@ public class JsonModel {
} while (!tokens.isEmpty()); } while (!tokens.isEmpty());
if (modelRef.getClass().isAssignableFrom(clazz)) { if (modelRef.getClass().isAssignableFrom(clazz)) {
throw new InvalidModelPathException(jsonPath + " does nor refer to a Map but " + currentToken.getClass().getName()); throw new InvalidModelException(jsonPath + " does nor refer to a Map but " + currentToken.getClass().getName());
} }
return clazz.cast(modelRef); return clazz.cast(modelRef);
} }
@ -307,10 +327,16 @@ public class JsonModel {
ObjectOps put(String key, Object value); ObjectOps put(String key, Object value);
ObjectOps putIfAbsent(String key, Object value);
Object get(String key);
ObjectOps putAll(Map<String, Object> map); ObjectOps putAll(Map<String, Object> map);
ObjectOps remove(String key); ObjectOps remove(String key);
ObjectOps transform(Transformer<JsonModel> transformer);
<T> T to(Class<T> targetClass); <T> T to(Class<T> targetClass);
} }
@ -325,6 +351,8 @@ public class JsonModel {
ListMappingModelReader toList(); ListMappingModelReader toList();
ArrayOps transform(Transformer<Object> transformer);
<T> List<T> toListOf(Class<T> targetClass); <T> List<T> toListOf(Class<T> targetClass);
<T> Set<T> toSetOf(Class<T> targetClass); <T> Set<T> toSetOf(Class<T> targetClass);
@ -334,9 +362,8 @@ public class JsonModel {
private Map<String, Object> opsTarget; private Map<String, Object> opsTarget;
private DefaultObjectOps(Map<String, Object> opsTarget) { private DefaultObjectOps(Object opsTarget) {
this.opsTarget = (Map<String, Object>) opsTarget;
this.opsTarget = opsTarget;
} }
@Override @Override
@ -355,6 +382,19 @@ public class JsonModel {
return this; return this;
} }
@Override
public ObjectOps putIfAbsent(String key, Object value) {
if (!opsTarget.containsKey(key)) {
opsTarget.put(key, value);
}
return this;
}
@Override
public Object get(String key) {
return opsTarget.get(key);
}
@Override @Override
public ObjectOps putAll(Map<String, Object> map) { public ObjectOps putAll(Map<String, Object> map) {
opsTarget.putAll(map); opsTarget.putAll(map);
@ -367,6 +407,12 @@ public class JsonModel {
return this; return this;
} }
@Override
public ObjectOps transform(Transformer<JsonModel> transformer) {
transformer.transform(-1, JsonModel.model(opsTarget));
return this;
}
@Override @Override
public <T> T to(Class<T> targetClass) { public <T> T to(Class<T> targetClass) {
return new DefaultMappingModelReader(opsTarget).to(targetClass); return new DefaultMappingModelReader(opsTarget).to(targetClass);
@ -377,8 +423,8 @@ public class JsonModel {
private List<Object> opsTarget; private List<Object> opsTarget;
private DefaultArrayOps(List<Object> opsTarget) { private DefaultArrayOps(Object opsTarget) {
this.opsTarget = opsTarget; this.opsTarget = (List<Object>) opsTarget;
} }
@Override @Override
@ -409,6 +455,15 @@ public class JsonModel {
return new DefaultMappingModelReader(opsTarget); return new DefaultMappingModelReader(opsTarget);
} }
@Override
public ArrayOps transform(Transformer<Object> transformer) {
for (int i = 0; i < opsTarget.size(); i++) {
Object current = opsTarget.get(i);
opsTarget.set(i, transformer.transform(i, current));
}
return this;
}
@Override @Override
public <T> List<T> toListOf(Class<T> targetClass) { public <T> List<T> toListOf(Class<T> targetClass) {
return new DefaultMappingModelReader(opsTarget).toListOf(targetClass); return new DefaultMappingModelReader(opsTarget).toListOf(targetClass);
@ -439,20 +494,22 @@ public class JsonModel {
@Override @Override
public <T> List<T> toListOf(Class<T> targetClass) { public <T> List<T> toListOf(Class<T> targetClass) {
if (!(model instanceof List)) { Object modelRef = model;
model = asList(model); if (!(modelRef instanceof List)) {
modelRef = asList(modelRef);
} }
return MappingProviderFactory.createProvider().convertValue(model, List.class, targetClass); return MappingProviderFactory.createProvider().convertValue(modelRef, List.class, targetClass);
} }
@Override @Override
public <T> Set<T> toSetOf(Class<T> targetClass) { public <T> Set<T> toSetOf(Class<T> targetClass) {
if (!(model instanceof List)) { Object modelRef = model;
if (!(modelRef instanceof List)) {
Set setModel = new HashSet(); Set setModel = new HashSet();
setModel.add(model); setModel.add(model);
model = setModel; modelRef = setModel;
} }
return MappingProviderFactory.createProvider().convertValue(model, Set.class, targetClass); return MappingProviderFactory.createProvider().convertValue(modelRef, Set.class, targetClass);
} }
@Override @Override

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

@ -17,10 +17,11 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken; import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.PathTokenizer; import com.jayway.jsonpath.internal.PathTokenizer;
import com.jayway.jsonpath.internal.Util;
import com.jayway.jsonpath.internal.filter.PathTokenFilter; import com.jayway.jsonpath.internal.filter.PathTokenFilter;
import com.jayway.jsonpath.spi.HttpProviderFactory;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory; import com.jayway.jsonpath.spi.JsonProviderFactory;
import org.apache.commons.io.IOUtils;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
@ -214,12 +215,12 @@ public class JsonPath {
public <T> T read(URL jsonURL) throws IOException { public <T> T read(URL jsonURL) throws IOException {
notNull(jsonURL, "json URL can not be null"); notNull(jsonURL, "json URL can not be null");
BufferedReader in = null; InputStream in = null;
try { try {
in = new BufferedReader(new InputStreamReader(jsonURL.openStream())); in = HttpProviderFactory.getProvider().get(jsonURL);
return (T) read(JsonProviderFactory.createProvider().parse(in)); return (T) read(JsonProviderFactory.createProvider().parse(in));
} finally { } finally {
IOUtils.closeQuietly(in); Util.closeQuietly(in);
} }
} }
@ -241,7 +242,7 @@ public class JsonPath {
fis = new FileInputStream(jsonFile); fis = new FileInputStream(jsonFile);
return (T) read(JsonProviderFactory.createProvider().parse(fis)); return (T) read(JsonProviderFactory.createProvider().parse(fis));
} finally { } finally {
IOUtils.closeQuietly(fis); Util.closeQuietly(fis);
} }
} }
@ -260,7 +261,7 @@ public class JsonPath {
try { try {
return (T) read(JsonProviderFactory.createProvider().parse(jsonInputStream)); return (T) read(JsonProviderFactory.createProvider().parse(jsonInputStream));
} finally { } finally {
IOUtils.closeQuietly(jsonInputStream); Util.closeQuietly(jsonInputStream);
} }
} }

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

@ -0,0 +1,13 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/9/12
* Time: 3:20 PM
*/
public interface Transformer {
public JsonModel transform(JsonModel model);
}

10
json-path/src/main/java/com/jayway/jsonpath/internal/Util.java

@ -0,0 +1,10 @@
package com.jayway.jsonpath.internal;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/10/12
* Time: 7:25 AM
*/
public class Util {
}

10
json-path/src/main/java/com/jayway/jsonpath/spi/HttpProvider.java

@ -0,0 +1,10 @@
package com.jayway.jsonpath.spi;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/10/12
* Time: 7:38 AM
*/
public interface HttpProvider {
}

10
json-path/src/main/java/com/jayway/jsonpath/spi/HttpProviderFactory.java

@ -0,0 +1,10 @@
package com.jayway.jsonpath.spi;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/10/12
* Time: 7:39 AM
*/
public class HttpProviderFactory {
}

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

@ -0,0 +1,10 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/10/12
* Time: 8:12 AM
*/
public class HttpProviderTest {
}

34
json-path/src/test/java/com/jayway/jsonpath/JsonModelMappingTest.java

@ -5,6 +5,9 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
import static com.jayway.jsonpath.JsonModel.model;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
/** /**
@ -49,11 +52,36 @@ public class JsonModelMappingTest {
" }\n" + " }\n" +
" }\n" + " }\n" +
"}"; "}";
@Test
public void map_and_filter_can_be_combined() throws Exception {
JsonModel model = JsonModel.model(DOCUMENT);
Filter filter = Filter.filter(Criteria.where("category").is("fiction").and("price").gt(10D));
List<Book> books = model.map("$.store.book[?]", filter).toList().of(Book.class);
JsonPath jsonPath = JsonPath.compile("$.store.book[?]", filter(where("category").is("fiction").and("price").gt(10D)));
Object read = jsonPath.read(DOCUMENT);
List<Book> books1 = JsonModel.model(DOCUMENT).map("$.store.book[?]", filter).toListOf(Book.class);
model(DOCUMENT);
System.out.println("");
}
@Test @Test
public void map_a_json_model_to_an_Class() throws Exception { public void map_a_json_model_to_an_Class() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Book book = model.map("$.store.book[1]").to(Book.class); Book book = model.map("$.store.book[1]").to(Book.class);
@ -65,7 +93,7 @@ public class JsonModelMappingTest {
@Test @Test
public void map_a_json_model_to_a_List() throws Exception { public void map_a_json_model_to_a_List() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class); List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
@ -92,7 +120,7 @@ public class JsonModelMappingTest {
@Test @Test
public void map_a_json_model_to_a_Set() throws Exception { public void map_a_json_model_to_a_Set() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Set<Book> bookSet = model.map("$.store.book[1]").toSetOf(Book.class); Set<Book> bookSet = model.map("$.store.book[1]").toSetOf(Book.class);

71
json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java

@ -55,7 +55,7 @@ public class JsonModelOpsTest {
@Test @Test
public void object_ops_can_update() throws Exception { public void object_ops_can_update() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
model.opsForObject("store.book[0]") model.opsForObject("store.book[0]")
.put("author", "Kalle") .put("author", "Kalle")
@ -68,7 +68,7 @@ public class JsonModelOpsTest {
@Test @Test
public void array_ops_can_add_element() throws Exception { public void array_ops_can_add_element() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Map<String, Object> newBook = new HashMap<String, Object>(); Map<String, Object> newBook = new HashMap<String, Object>();
newBook.put("category", "reference"); newBook.put("category", "reference");
@ -90,7 +90,7 @@ public class JsonModelOpsTest {
@Test @Test
public void arrays_can_be_mapped() throws Exception { public void arrays_can_be_mapped() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
List<Book> books1 = model.opsForArray("store.book").toList().of(Book.class); List<Book> books1 = model.opsForArray("store.book").toList().of(Book.class);
List<Book> books2 = model.opsForArray("store.book").toListOf(Book.class); List<Book> books2 = model.opsForArray("store.book").toListOf(Book.class);
@ -103,7 +103,7 @@ public class JsonModelOpsTest {
@Test @Test
public void objects_can_be_mapped() throws Exception { public void objects_can_be_mapped() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Book book = model.opsForObject("store.book[1]").to(Book.class); Book book = model.opsForObject("store.book[1]").to(Book.class);
@ -114,6 +114,69 @@ public class JsonModelOpsTest {
} }
@Test
public void object_can_be_transformed() throws Exception {
Transformer transformer = new Transformer<JsonModel>() {
@Override
public JsonModel transform(int index, JsonModel model) {
model.opsForObject().put("newProp", "newProp");
return model;
}
};
JsonModel model = JsonModel.model(DOCUMENT);
model.opsForObject("store.book[1]").transform(transformer);
assertEquals("newProp", model.get("store.book[1].newProp"));
}
@Test
public void arrays_can_be_transformed() throws Exception {
Transformer transformer = new Transformer<Object>() {
@Override
public Object transform(int index, Object model) {
Map<String, Object> map = (Map<String, Object>) model;
map.put("newProp", "newProp");
return model;
}
};
JsonModel model = JsonModel.model(DOCUMENT);
model.opsForArray("store.book").transform(transformer);
assertEquals("newProp", model.get("store.book[1].newProp"));
}
@Test
public void array_can_be_transformed_to_primitives() throws Exception {
Transformer positionTransformer = new Transformer<Object>() {
private int i = 0;
@Override
public Object transform(int index, Object model) {
return i++;
}
};
Transformer multiplyingTransformer = new Transformer<Object>() {
@Override
public Object transform(int index, Object model) {
int in = (Integer)model;
return in * 2;
}
};
JsonModel model = JsonModel.model(DOCUMENT);
model.opsForArray("store.book").transform(positionTransformer).transform(multiplyingTransformer);
assertEquals(2, model.get("store.book[1]"));
}
public static class Book { public static class Book {
public String category; public String category;
public String author; public String author;

10
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -57,18 +57,18 @@ public class JsonModelTest {
@Test @Test
public void a_json_document_can_be_fetched_with_a_URL() throws Exception { public void a_json_document_can_be_fetched_with_a_URL() throws Exception {
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json"); URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json");
assertEquals("REQUEST_DENIED", JsonModel.create(url).get("status")); assertEquals("REQUEST_DENIED", JsonModel.model(url).get("status"));
} }
@Test @Test
public void a_json_document_can_be_fetched_with_a_InputStream() throws Exception { public void a_json_document_can_be_fetched_with_a_InputStream() throws Exception {
ByteArrayInputStream bis = new ByteArrayInputStream(DOCUMENT.getBytes()); ByteArrayInputStream bis = new ByteArrayInputStream(DOCUMENT.getBytes());
assertEquals("Nigel Rees", JsonModel.create(bis).get("store.book[0].author")); assertEquals("Nigel Rees", JsonModel.model(bis).get("store.book[0].author"));
} }
@Test @Test
public void test_a_sub_model_can_be_fetched_and_read() throws Exception { public void test_a_sub_model_can_be_fetched_and_read() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
assertEquals("Nigel Rees", model.getModel("$store.book[0]").get("author")); assertEquals("Nigel Rees", model.getModel("$store.book[0]").get("author"));
assertEquals("Nigel Rees", model.getModel(JsonPath.compile("$store.book[0]")).get("author")); assertEquals("Nigel Rees", model.getModel(JsonPath.compile("$store.book[0]")).get("author"));
} }
@ -79,7 +79,7 @@ public class JsonModelTest {
doc.put("items", asList(0, 1, 2)); doc.put("items", asList(0, 1, 2));
doc.put("child", Collections.singletonMap("key", "value")); doc.put("child", Collections.singletonMap("key", "value"));
JsonModel model = JsonModel.create(doc); JsonModel model = JsonModel.model(doc);
assertEquals("value", model.get("$child.key")); assertEquals("value", model.get("$child.key"));
assertEquals(1, model.get("$items[1]")); assertEquals(1, model.get("$items[1]"));
@ -89,7 +89,7 @@ public class JsonModelTest {
@Test(expected = InvalidPathException.class) @Test(expected = InvalidPathException.class)
public void invalid_path_throws() throws Exception { public void invalid_path_throws() throws Exception {
JsonModel.create(DOCUMENT).get("store.invalid"); JsonModel.model(DOCUMENT).get("store.invalid");
} }

5
pom.xml

@ -239,11 +239,6 @@
<version>2.6</version> <version>2.6</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>

Loading…
Cancel
Save