Browse Source

Improved conversions.

pull/8/merge
Kalle Stenflo 13 years ago
parent
commit
765f573306
  1. 29
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  2. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/ConvertUtils.java
  3. 103
      json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java

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

@ -122,6 +122,10 @@ public class JsonModel {
return jsonProvider.isMap(jsonObject);
}
public void print(){
System.out.println(toJson());
}
/**
* Check if this JsonModel has the given definite path
*
@ -705,6 +709,13 @@ public class JsonModel {
*/
Double getDouble(String key);
/**
* Tries to convert the value associated with the key to an {@link String}
* @param key the key
* @return converted value
*/
String getString(String key);
/**
* @see Map#putAll(java.util.Map)
*/
@ -778,6 +789,9 @@ public class JsonModel {
*/
ArrayOps transform(Transformer<List<Object>> transformer);
ArrayOps each(Transformer<Object> transformer);
/**
* @see ListMappingModelReader
*/
@ -847,6 +861,11 @@ public class JsonModel {
return ConvertUtils.toDouble(get(key));
}
@Override
public String getString(String key) {
return ConvertUtils.toString(get(key));
}
@Override
public ObjectOps putAll(Map<String, Object> map) {
getTargetObject(jsonPath, Map.class).putAll(map);
@ -929,6 +948,16 @@ public class JsonModel {
return this;
}
@Override
public ArrayOps each(Transformer<Object> transformer) {
List targetObject = getTargetObject(jsonPath, List.class);
for(int i = 0; i < targetObject.size(); i++){
targetObject.set(i, transformer.transform(targetObject.get(i)));
}
return this;
}
@Override
public <T> List<T> toListOf(Class<T> targetClass) {
return new DefaultMappingModelReader(getTargetObject(jsonPath, List.class)).toListOf(targetClass);

8
json-path/src/main/java/com/jayway/jsonpath/internal/ConvertUtils.java

@ -75,4 +75,12 @@ public class ConvertUtils {
throw new InvalidConversionException("Could not convert " + o.toString() + " to Double");
}
}
public static String toString(Object o) {
if(null == o){
return null;
}
return o.toString();
}
}

103
json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java

@ -0,0 +1,103 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
/**
* Created by IntelliJ IDEA.
* User: kallestenflo
* Date: 3/14/12
* Time: 7:30 AM
*/
public class JsonModelChainedCallsTest {
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" +
" \"number\": 12,\n" +
" \"dot.notation\": \"new\"\n" +
" }\n" +
" }\n" +
"}";
@Test
public void convert_and_map() throws Exception {
JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Map<String, Object>> transformer = new Transformer<Map<String, Object>>() {
@Override
public Object transform(Map<String, Object> map) {
map.remove("isbn");
map.put("author", "kalle");
return map;
}
};
Book book = model.opsForObject("store.book[0]").transform(transformer).to(Book.class);
assertEquals(book.author, "kalle");
}
@Test
public void convert_each_and_map() throws Exception {
JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Object> transformer = new Transformer<Object>() {
@Override
public Object transform(Object obj) {
Map<String, Object> map = (Map<String, Object>) obj;
map.remove("isbn");
map.put("author", "kalle");
return map;
}
};
List<Book> books = model.opsForArray("store.book").each(transformer).toList().of(Book.class);
System.out.println();
}
public static class Book {
public String category;
public String author;
public String title;
public Double price;
}
}
Loading…
Cancel
Save