Browse Source

Added write ops to JsonModel.

pull/7/head
Kalle Stenflo 13 years ago
parent
commit
974a84553c
  1. 15
      json-path/src/main/java/com/jayway/jsonpath/IndefinitePathException.java
  2. 262
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  3. 14
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
  5. 16
      json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java
  6. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayEvalFilter.java
  7. 16
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  8. 22
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  9. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java
  10. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/HasFieldFilter.java
  11. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/PassthroughFilter.java
  12. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ScanFilter.java
  13. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/WildcardFilter.java
  14. 115
      json-path/src/test/java/com/jayway/jsonpath/JsonModelMappingTest.java
  15. 124
      json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java
  16. 61
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

15
json-path/src/main/java/com/jayway/jsonpath/IndefinitePathException.java

@ -0,0 +1,15 @@
package com.jayway.jsonpath;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/4/12
* Time: 7:15 PM
*/
public class IndefinitePathException extends RuntimeException {
public IndefinitePathException(String path) {
super("The path " + path + " is not definite");
}
}

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

@ -14,6 +14,8 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.PathToken;
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 org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
@ -22,10 +24,7 @@ import org.codehaus.jackson.map.type.CollectionType;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.HashSet; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.notEmpty; import static org.apache.commons.lang.Validate.notEmpty;
@ -103,6 +102,15 @@ public class JsonModel {
} }
} }
// --------------------------------------------------------
//
// Getters
//
// --------------------------------------------------------
public Object getJsonObject() {
return this.jsonObject;
}
// -------------------------------------------------------- // --------------------------------------------------------
// //
// Model readers // Model readers
@ -111,10 +119,7 @@ public class JsonModel {
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public <T> T get(String jsonPath) { public <T> T get(String jsonPath) {
notEmpty(jsonPath, "jsonPath can not be null or empty"); return (T) get(JsonPath.compile(jsonPath));
JsonPath path = JsonPath.compile(jsonPath);
return (T) get(path);
} }
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
@ -124,6 +129,34 @@ public class JsonModel {
return (T) jsonPath.read(jsonObject); return (T) jsonPath.read(jsonObject);
} }
// --------------------------------------------------------
//
// Model writers
//
// --------------------------------------------------------
public ArrayOps opsForArray(String jsonPath) {
return opsForArray(JsonPath.compile(jsonPath));
}
public ArrayOps opsForArray(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null");
List<Object> opsTarget = getTargetObject(jsonPath, List.class);
return new DefaultArrayOps(opsTarget);
}
public ObjectOps opsForObject(String jsonPath) {
return opsForObject(JsonPath.compile(jsonPath));
}
public ObjectOps opsForObject(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null");
Map<String, Object> opsTarget = getTargetObject(jsonPath, Map.class);
return new DefaultObjectOps(opsTarget);
}
// -------------------------------------------------------- // --------------------------------------------------------
// //
@ -135,9 +168,7 @@ public class JsonModel {
} }
public String toJson(String jsonPath) { public String toJson(String jsonPath) {
notEmpty(jsonPath, "jsonPath can not be null or empty"); return toJson(JsonPath.compile(jsonPath));
return JsonProviderFactory.getInstance().toJson(get(jsonPath));
} }
public String toJson(JsonPath jsonPath) { public String toJson(JsonPath jsonPath) {
@ -153,21 +184,18 @@ public class JsonModel {
// -------------------------------------------------------- // --------------------------------------------------------
public JsonModel getSubModel(String jsonPath) { public JsonModel getSubModel(String jsonPath) {
notEmpty(jsonPath, "jsonPath can not be null or empty"); return getSubModel(JsonPath.compile(jsonPath));
JsonPath path = JsonPath.compile(jsonPath);
return getSubModel(path);
} }
public JsonModel getSubModel(JsonPath jsonPath) { public JsonModel getSubModel(JsonPath jsonPath) {
notNull(jsonPath, "jsonPath can not be null"); notNull(jsonPath, "jsonPath can not be null");
Object subModel = jsonPath.read(jsonObject); Object subModel = jsonPath.read(jsonObject);
if(!(subModel instanceof Map) && !(subModel instanceof List)){ if (!(subModel instanceof Map) && !(subModel instanceof List)) {
throw new InvalidModelPathException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel!=null?subModel.getClass():"null")); throw new InvalidModelPathException("The path " + jsonPath.getPath() + " returned an invalid model " + (subModel != null ? subModel.getClass() : "null"));
} }
return new JsonModel(subModel); return new JsonModel(subModel);
} }
@ -176,10 +204,8 @@ public class JsonModel {
// Mapping model readers // Mapping model readers
// //
// -------------------------------------------------------- // --------------------------------------------------------
public MappingModelReader map(final String jsonPath) { public MappingModelReader map(String jsonPath) {
notEmpty(jsonPath, "jsonPath can not be null or empty"); return map(JsonPath.compile(jsonPath));
return new DefaultMappingModelReader(JsonModel.this.get(jsonPath));
} }
public MappingModelReader map(final JsonPath jsonPath) { public MappingModelReader map(final JsonPath jsonPath) {
@ -217,27 +243,195 @@ public class JsonModel {
return new JsonModel(jsonInputStream); return new JsonModel(jsonInputStream);
} }
// --------------------------------------------------------
//
// Private helpers
//
// --------------------------------------------------------
private static ObjectMapper getObjectMapper() {
if (JsonModel.objectMapper == null) {
synchronized (JsonModel.class) {
try {
Class.forName("org.codehaus.jackson.map.ObjectMapper");
} catch (ClassNotFoundException e) {
throw new RuntimeException("org.codehaus.jackson.map.ObjectMapper not found on classpath. This is an optional dependency needed for POJO conversions.");
}
JsonModel.objectMapper = new ObjectMapper();
}
}
return JsonModel.objectMapper;
}
private <T> T getTargetObject(JsonPath jsonPath, Class<T> clazz) {
notNull(jsonPath, "jsonPath can not be null");
if (!jsonPath.isPathDefinite()) {
throw new IndefinitePathException(jsonPath.getPath());
}
JsonProvider jsonProvider = JsonProviderFactory.getInstance();
Object modelRef = jsonObject;
LinkedList<PathToken> tokens = jsonPath.getTokenizer().getPathTokens();
PathToken currentToken;
do {
currentToken = tokens.poll();
modelRef = currentToken.get(modelRef, jsonProvider);
} while (!tokens.isEmpty());
if (modelRef.getClass().isAssignableFrom(clazz)) {
throw new InvalidModelPathException(jsonPath + " does nor refer to a Map but " + (currentToken != null ? currentToken.getClass().getName() : "null"));
}
return clazz.cast(modelRef);
}
// -------------------------------------------------------- // --------------------------------------------------------
// //
// Interfaces // Interfaces
// //
// -------------------------------------------------------- // --------------------------------------------------------
public interface MappingModelReader { public interface MappingModelReader extends ListMappingModelReader, ObjectMappingModelReader {
}
public interface ObjectMappingModelReader {
<T> T to(Class<T> targetClass);
}
public interface ListMappingModelReader {
<T> List<T> of(Class<T> targetClass);
ListMappingModelReader toList(); ListMappingModelReader toList();
<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);
}
public interface ObjectOps {
Map<String, Object> getTarget();
boolean containsKey(String key);
ObjectOps put(String key, Object value);
ObjectOps putAll(Map<String, Object> map);
ObjectOps remove(String key);
<T> T to(Class<T> targetClass); <T> T to(Class<T> targetClass);
} }
public interface ListMappingModelReader { public interface ArrayOps {
<T> List<T> of(Class<T> targetClass); List<Object> getTarget();
ArrayOps add(Object o);
ArrayOps addAll(Collection<Object> collection);
ArrayOps remove(Object o);
ListMappingModelReader toList();
<T> List<T> toListOf(Class<T> targetClass);
<T> Set<T> toSetOf(Class<T> targetClass);
}
private static class DefaultObjectOps implements ObjectOps {
private Map<String, Object> opsTarget;
private DefaultObjectOps(Map<String, Object> opsTarget) {
this.opsTarget = opsTarget;
}
@Override
public Map<String, Object> getTarget() {
return opsTarget;
}
@Override
public boolean containsKey(String key) {
return opsTarget.containsKey(key);
}
@Override
public ObjectOps put(String key, Object value) {
opsTarget.put(key, value);
return this;
}
@Override
public ObjectOps putAll(Map<String, Object> map) {
opsTarget.putAll(map);
return this;
}
@Override
public ObjectOps remove(String key) {
opsTarget.remove(key);
return this;
}
@Override
public <T> T to(Class<T> targetClass) {
return new DefaultMappingModelReader(opsTarget).to(targetClass);
}
}
private static class DefaultArrayOps implements ArrayOps {
private List<Object> opsTarget;
private DefaultArrayOps(List<Object> opsTarget) {
this.opsTarget = opsTarget;
}
@Override
public List<Object> getTarget() {
return opsTarget;
}
@Override
public ArrayOps add(Object o) {
opsTarget.add(o);
return this;
}
@Override
public ArrayOps addAll(Collection<Object> collection) {
opsTarget.addAll(collection);
return this;
}
@Override
public ArrayOps remove(Object o) {
opsTarget.remove(o);
return this;
}
@Override
public ListMappingModelReader toList() {
return new DefaultMappingModelReader(opsTarget);
}
@Override
public <T> List<T> toListOf(Class<T> targetClass) {
return new DefaultMappingModelReader(opsTarget).toListOf(targetClass);
}
@Override
public <T> Set<T> toSetOf(Class<T> targetClass) {
return new DefaultMappingModelReader(opsTarget).toSetOf(targetClass);
}
} }
private static class DefaultMappingModelReader implements MappingModelReader, ListMappingModelReader { private static class DefaultMappingModelReader implements MappingModelReader {
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private Object model; private Object model;
@ -284,19 +478,5 @@ public class JsonModel {
} }
// --------------------------------------------------------
//
// Private helpers
//
// --------------------------------------------------------
private static ObjectMapper getObjectMapper() {
if (JsonModel.objectMapper == null) {
synchronized (JsonModel.class) {
JsonModel.objectMapper = new ObjectMapper();
}
}
return JsonModel.objectMapper;
}
} }

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

@ -18,6 +18,7 @@ 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.filter.Filter; import com.jayway.jsonpath.internal.filter.Filter;
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 org.apache.commons.io.IOUtils;
@ -108,6 +109,15 @@ public class JsonPath {
this.tokenizer = new PathTokenizer(jsonPath); this.tokenizer = new PathTokenizer(jsonPath);
} }
PathTokenizer getTokenizer(){
return this.tokenizer;
}
/**
* Returns the string representation of this JsonPath
*
* @return path as String
*/
public String getPath() { public String getPath() {
return this.tokenizer.getPath(); return this.tokenizer.getPath();
} }
@ -154,13 +164,15 @@ public class JsonPath {
throw new IllegalArgumentException("Invalid container object"); throw new IllegalArgumentException("Invalid container object");
} }
JsonProvider jsonProvider = JsonProviderFactory.getInstance();
Object result = jsonObject; Object result = jsonObject;
boolean inArrayContext = false; boolean inArrayContext = false;
for (PathToken pathToken : tokenizer) { for (PathToken pathToken : tokenizer) {
Filter filter = pathToken.getFilter(); Filter filter = pathToken.getFilter();
result = filter.filter(result, JsonProviderFactory.getInstance(), inArrayContext); result = filter.filter(result, jsonProvider, inArrayContext);
if (!inArrayContext) { if (!inArrayContext) {
inArrayContext = filter.isArrayFilter(); inArrayContext = filter.isArrayFilter();

4
json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java

@ -37,6 +37,10 @@ public class PathToken {
return FilterFactory.createFilter(fragment).filter(model, jsonProvider); return FilterFactory.createFilter(fragment).filter(model, jsonProvider);
} }
public Object get(Object model, JsonProvider jsonProvider){
return FilterFactory.createFilter(fragment).getRef(model, jsonProvider);
}
public String getFragment() { public String getFragment() {
return fragment; return fragment;
} }

16
json-path/src/main/java/com/jayway/jsonpath/internal/PathTokenizer.java

@ -16,6 +16,7 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.InvalidPathException;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -25,18 +26,17 @@ import java.util.List;
*/ */
public class PathTokenizer implements Iterable<PathToken> { public class PathTokenizer implements Iterable<PathToken> {
private String path;
private char[] pathChars;
private int index = 0;
private List<PathToken> pathTokens = new LinkedList<PathToken>(); private List<PathToken> pathTokens = new LinkedList<PathToken>();
private char[] pathChars;
private transient int index = 0;
public PathTokenizer(String jsonPath) { public PathTokenizer(String jsonPath) {
if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) { if (!jsonPath.startsWith("$") && !jsonPath.startsWith("$[")) {
jsonPath = "$." + jsonPath; jsonPath = "$." + jsonPath;
} }
this.path = jsonPath; this.pathChars = jsonPath.toCharArray();
this.pathChars = path.toCharArray();
for (String pathFragment : splitPath()) { for (String pathFragment : splitPath()) {
pathTokens.add(new PathToken(pathFragment)); pathTokens.add(new PathToken(pathFragment));
@ -52,7 +52,11 @@ public class PathTokenizer implements Iterable<PathToken> {
} }
public String getPath() { public String getPath() {
return path; return new String(pathChars);
}
public LinkedList<PathToken> getPathTokens(){
return new LinkedList<PathToken>(pathTokens);
} }
public Iterator<PathToken> iterator() { public Iterator<PathToken> iterator() {

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

@ -59,6 +59,11 @@ public class ArrayEvalFilter extends Filter {
return result; return result;
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException("");
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return true; return true;

16
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -17,11 +17,15 @@ package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* @author Kalle Stenflo * @author Kalle Stenflo
*/ */
public class ArrayIndexFilter extends Filter { public class ArrayIndexFilter extends Filter {
private static final Pattern SINGLE_ARRAY_INDEX_PATTERN = Pattern.compile("\\[\\d+\\]");
public ArrayIndexFilter(String condition) { public ArrayIndexFilter(String condition) {
super(condition); super(condition);
} }
@ -70,6 +74,18 @@ public class ArrayIndexFilter extends Filter {
} }
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
if(SINGLE_ARRAY_INDEX_PATTERN.matcher(condition).matches()){
String trimmedCondition = trim(condition, 1, 1);
List<Object> src = jsonProvider.toList(obj);
return src.get(Integer.parseInt(trimmedCondition));
} else {
throw new UnsupportedOperationException();
}
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return true; return true;

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

@ -59,28 +59,16 @@ public class FieldFilter extends Filter {
public Object filter(Object obj, JsonProvider jsonProvider) { public Object filter(Object obj, JsonProvider jsonProvider) {
if (jsonProvider.isList(obj)) { if (jsonProvider.isList(obj)) {
return obj; return obj;
/*
List<Object> result = jsonProvider.createList();
for (Object current : jsonProvider.toList(obj)) {
if (jsonProvider.isMap(current)) {
Map<String, Object> map = jsonProvider.toMap(current);
if (map.containsKey(condition)) {
Object o = map.get(condition);
if (jsonProvider.isList(o)) {
result.addAll(jsonProvider.toList(o));
} else {
result.add(map.get(condition));
}
}
}
}
return result;
*/
} else { } else {
return jsonProvider.getMapValue(obj, condition); return jsonProvider.getMapValue(obj, condition);
} }
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
return filter(obj, jsonProvider);
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return false; return false;

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/Filter.java

@ -46,7 +46,7 @@ public abstract class Filter {
public abstract Object filter(Object obj, JsonProvider jsonProvider); public abstract Object filter(Object obj, JsonProvider jsonProvider);
public abstract Object getRef(Object obj, JsonProvider jsonProvider);
public abstract boolean isArrayFilter(); public abstract boolean isArrayFilter();

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

@ -55,6 +55,11 @@ public class HasFieldFilter extends Filter {
return result; return result;
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException();
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return true; return true;

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

@ -33,6 +33,11 @@ public class PassthroughFilter extends Filter {
return obj; return obj;
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
return obj;
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return isArrayFilter; return isArrayFilter;

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

@ -41,6 +41,11 @@ public class ScanFilter extends Filter {
return true; return true;
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException();
}
private void scan(Object container, List<Object> result, JsonProvider jsonProvider) { private void scan(Object container, List<Object> result, JsonProvider jsonProvider) {

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

@ -45,6 +45,11 @@ public class WildcardFilter extends Filter {
return result; return result;
} }
@Override
public Object getRef(Object obj, JsonProvider jsonProvider) {
throw new UnsupportedOperationException();
}
@Override @Override
public boolean isArrayFilter() { public boolean isArrayFilter() {
return true; return true;

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

@ -0,0 +1,115 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import java.util.Set;
import static junit.framework.Assert.assertEquals;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/4/12
* Time: 8:36 PM
*/
public class JsonModelMappingTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void map_a_json_model_to_an_Class() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Book book = model.map("$.store.book[1]").to(Book.class);
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
@Test
public void map_a_json_model_to_a_List() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
booksList = model.map("$.store.book[*]").toListOf(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
booksList = model.map("$.store.book[*]").toList().of(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
}
@Test
public void map_a_json_model_to_a_Set() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Set<Book> bookSet = model.map("$.store.book[1]").toSetOf(Book.class);
Book book = bookSet.iterator().next();
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
public static class Book {
public String category;
public String author;
public String title;
public String isbn;
public Double price;
}
}

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

@ -0,0 +1,124 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static junit.framework.Assert.assertEquals;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/4/12
* Time: 4:55 PM
*/
public class JsonModelOpsTest {
public final static String DOCUMENT =
"{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"foo:bar\": \"fooBar\",\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void object_ops_can_update() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
model.opsForObject("store.book[0]")
.put("author", "Kalle")
.put("price", 12.30D);
assertEquals("Kalle", model.get("store.book[0].author"));
assertEquals(12.30D, model.get("store.book[0].price"));
}
@Test
public void array_ops_can_add_element() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Map<String, Object> newBook = new HashMap<String, Object>();
newBook.put("category", "reference");
newBook.put("author", "Kalle");
newBook.put("title", "JSONPath book");
newBook.put("isbn", "0-553-21311-34");
newBook.put("price", 12.10D);
model.opsForArray("store.book").add(newBook);
JsonModel subModel = model.getSubModel("store.book[4]");
assertEquals("reference", subModel.get("category"));
assertEquals("Kalle", subModel.get("author"));
assertEquals("JSONPath book", subModel.get("title"));
assertEquals("0-553-21311-34", subModel.get("isbn"));
assertEquals(12.10D, subModel.get("price"));
}
@Test
public void arrays_can_be_mapped() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
List<Book> books1 = model.opsForArray("store.book").toList().of(Book.class);
List<Book> books2 = model.opsForArray("store.book").toListOf(Book.class);
Set<Book> books3 = model.opsForArray("store.book").toSetOf(Book.class);
assertEquals(4, books1.size());
assertEquals(4, books2.size());
assertEquals(4, books3.size());
}
@Test
public void objects_can_be_mapped() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Book book = model.opsForObject("store.book[1]").to(Book.class);
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
public static class Book {
public String category;
public String author;
public String title;
public String isbn;
public Double price;
}
}

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

@ -84,68 +84,7 @@ public class JsonModelTest {
assertEquals("{\"child\":{\"key\":\"value\"},\"items\":[0,1,2]}", model.toJson()); assertEquals("{\"child\":{\"key\":\"value\"},\"items\":[0,1,2]}", model.toJson());
} }
@Test
public void map_a_json_model_to_an_Class() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Book book = model.map("$.store.book[1]").to(Book.class);
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
@Test
public void map_a_json_model_to_a_List() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
List<Book> booksList = model.map("$.store.book[0,1]").toListOf(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
booksList = model.map("$.store.book[*]").toListOf(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
booksList = model.map("$.store.book[*]").toList().of(Book.class);
assertEquals("fiction", booksList.get(1).category);
assertEquals("Evelyn Waugh", booksList.get(1).author);
assertEquals("Sword of Honour", booksList.get(1).title);
assertEquals(12.99D, booksList.get(1).price);
}
@Test
public void map_a_json_model_to_a_Set() throws Exception {
JsonModel model = JsonModel.create(DOCUMENT);
Set<Book> bookSet = model.map("$.store.book[1]").toSetOf(Book.class);
Book book = bookSet.iterator().next();
assertEquals("fiction", book.category);
assertEquals("Evelyn Waugh", book.author);
assertEquals("Sword of Honour", book.title);
assertEquals(12.99D, book.price);
}
public static class Book {
public String category;
public String author;
public String title;
public String isbn;
public Double price;
}
} }

Loading…
Cancel
Save