Browse Source

Renamed update operation 'convert' to 'map'.

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
c97c70a33d
  1. 44
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 8
      json-path/src/main/java/com/jayway/jsonpath/MapFunction.java
  3. 12
      json-path/src/main/java/com/jayway/jsonpath/WriteContext.java
  4. 24
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
  5. 22
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java
  7. 30
      json-path/src/test/java/com/jayway/jsonpath/WriteTest.java
  8. 2
      json-path/src/test/java/com/jayway/jsonpath/internal/JsonContextTest.java

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

@ -16,7 +16,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.JsonReader;
import com.jayway.jsonpath.internal.JsonContext;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathCompiler;
import com.jayway.jsonpath.internal.PathRef;
@ -229,20 +229,20 @@ public class JsonPath {
/**
* Converts the value on the given path.
* Replaces the value on the given path with the result of the {@link MapFunction}.
*
* @param jsonObject a json object
* @param valueConverter Converter object to be invoked
* @param mapFunction Converter object to be invoked
* @param configuration configuration to use
* @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
*/
public <T> T convert(Object jsonObject, ValueConverter valueConverter, Configuration configuration) {
public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
notNull(valueConverter, "valueConverter can not be null");
notNull(mapFunction, "mapFunction can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.convert(valueConverter, configuration);
updateOperation.convert(mapFunction, configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
@ -498,7 +498,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(String json, String jsonPath, Predicate... filters) {
return new JsonReader().parse(json).read(jsonPath, filters);
return new JsonContext().parse(json).read(jsonPath, filters);
}
@ -513,7 +513,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonURL).read(jsonPath, filters);
return new JsonContext().parse(jsonURL).read(jsonPath, filters);
}
/**
@ -527,7 +527,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonFile).read(jsonPath, filters);
return new JsonContext().parse(jsonFile).read(jsonPath, filters);
}
/**
@ -541,7 +541,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(InputStream jsonInputStream, String jsonPath, Predicate... filters) throws IOException {
return new JsonReader().parse(jsonInputStream).read(jsonPath, filters);
return new JsonContext().parse(jsonInputStream).read(jsonPath, filters);
}
@ -559,7 +559,7 @@ public class JsonPath {
* @return a parsing context based on given configuration
*/
public static ParseContext using(Configuration configuration) {
return new JsonReader(configuration);
return new JsonContext(configuration);
}
/**
@ -570,7 +570,7 @@ public class JsonPath {
*/
@Deprecated
public static ParseContext using(JsonProvider provider) {
return new JsonReader(Configuration.builder().jsonProvider(provider).build());
return new JsonContext(Configuration.builder().jsonProvider(provider).build());
}
/**
@ -581,7 +581,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(Object json) {
return new JsonReader().parse(json);
return new JsonContext().parse(json);
}
/**
@ -592,7 +592,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(String json) {
return new JsonReader().parse(json);
return new JsonContext().parse(json);
}
/**
@ -603,7 +603,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(InputStream json) {
return new JsonReader().parse(json);
return new JsonContext().parse(json);
}
/**
@ -614,7 +614,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(File json) throws IOException {
return new JsonReader().parse(json);
return new JsonContext().parse(json);
}
/**
@ -625,7 +625,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(URL json) throws IOException {
return new JsonReader().parse(json);
return new JsonContext().parse(json);
}
/**
@ -636,7 +636,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(Object json, Configuration configuration) {
return new JsonReader(configuration).parse(json);
return new JsonContext(configuration).parse(json);
}
/**
@ -647,7 +647,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(String json, Configuration configuration) {
return new JsonReader(configuration).parse(json);
return new JsonContext(configuration).parse(json);
}
/**
@ -658,7 +658,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(InputStream json, Configuration configuration) {
return new JsonReader(configuration).parse(json);
return new JsonContext(configuration).parse(json);
}
/**
@ -669,7 +669,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(File json, Configuration configuration) throws IOException {
return new JsonReader(configuration).parse(json);
return new JsonContext(configuration).parse(json);
}
/**
@ -680,7 +680,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(URL json, Configuration configuration) throws IOException {
return new JsonReader(configuration).parse(json);
return new JsonContext(configuration).parse(json);
}
private <T> T resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) {

8
json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java → json-path/src/main/java/com/jayway/jsonpath/MapFunction.java

@ -14,6 +14,10 @@
*/
package com.jayway.jsonpath;
public interface ValueConverter {
Object convert(Object currentValue, Configuration configuration);
/**
* Returns a new representation for the input value.
*/
public interface MapFunction {
Object map(Object currentValue, Configuration configuration);
}

12
json-path/src/main/java/com/jayway/jsonpath/WriteContext.java

@ -57,23 +57,23 @@ public interface WriteContext {
DocumentContext set(JsonPath path, Object newValue);
/**
* Converts the value on the given path.
* Replaces the value on the given path with the result of the {@link MapFunction}.
*
* @param path path to be converted set
* @param valueConverter Converter object to be invoked
* @param mapFunction Converter object to be invoked
* @param filters filters
* @return a document context
*/
DocumentContext convert(String path, ValueConverter valueConverter, Predicate... filters);
DocumentContext map(String path, MapFunction mapFunction, Predicate... filters);
/**
* Converts the value on the given path.
* Replaces the value on the given path with the result of the {@link MapFunction}.
*
* @param path path to be converted set
* @param valueConverter Converter object to be invoked (or lambda:))
* @param mapFunction Converter object to be invoked (or lambda:))
* @return a document context
*/
DocumentContext convert(JsonPath path, ValueConverter valueConverter);
DocumentContext map(JsonPath path, MapFunction mapFunction);
/**
* Deletes the given path

24
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java → json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java

@ -18,12 +18,12 @@ import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.MapFunction;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.ValueConverter;
import com.jayway.jsonpath.spi.cache.Cache;
import com.jayway.jsonpath.spi.cache.CacheProvider;
import org.slf4j.Logger;
@ -41,23 +41,23 @@ 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 {
public class JsonContext implements ParseContext, DocumentContext {
private static final Logger logger = LoggerFactory.getLogger(JsonReader.class);
private static final Logger logger = LoggerFactory.getLogger(JsonContext.class);
private final Configuration configuration;
private Object json;
public JsonReader() {
public JsonContext() {
this(Configuration.defaultConfiguration());
}
public JsonReader(Configuration configuration) {
public JsonContext(Configuration configuration) {
notNull(configuration, "configuration can not be null");
this.configuration = configuration;
}
private JsonReader(Object json, Configuration configuration) {
private JsonContext(Object json, Configuration configuration) {
notNull(json, "json can not be null");
notNull(configuration, "configuration can not be null");
this.configuration = configuration;
@ -184,7 +184,7 @@ public class JsonReader implements ParseContext, DocumentContext {
}
public ReadContext withListeners(EvaluationListener... listener){
return new JsonReader(json, configuration.setEvaluationListeners(listener));
return new JsonContext(json, configuration.setEvaluationListeners(listener));
}
@ -213,14 +213,14 @@ public class JsonReader implements ParseContext, DocumentContext {
}
@Override
public DocumentContext convert(String path, ValueConverter valueConverter, Predicate... filters) {
convert(compile(path, filters), valueConverter);
public DocumentContext map(String path, MapFunction mapFunction, Predicate... filters) {
map(compile(path, filters), mapFunction);
return this;
}
@Override
public DocumentContext convert(JsonPath path, ValueConverter valueConverter) {
path.convert(json, valueConverter, configuration);
public DocumentContext map(JsonPath path, MapFunction mapFunction) {
path.map(json, mapFunction, configuration);
return this;
}
@ -290,14 +290,12 @@ public class JsonReader implements ParseContext, DocumentContext {
}
private final class LimitingEvaluationListener implements EvaluationListener {
final int limit;
private LimitingEvaluationListener(int limit) {
this.limit = limit;
}
@Override
public EvaluationContinuation resultFound(FoundResult found) {
if(found.index() == limit - 1){

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

@ -2,8 +2,8 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidModificationException;
import com.jayway.jsonpath.MapFunction;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.ValueConverter;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.Collection;
@ -20,7 +20,7 @@ public abstract class PathRef implements Comparable<PathRef> {
public void set(Object newVal, Configuration configuration) {}
@Override
public void convert(ValueConverter valueConverter, Configuration configuration) {}
public void convert(MapFunction mapFunction, Configuration configuration) {}
@Override
public void delete(Configuration configuration) {}
@ -47,7 +47,7 @@ public abstract class PathRef implements Comparable<PathRef> {
public abstract void set(Object newVal, Configuration configuration);
public abstract void convert(ValueConverter valueConverter, Configuration configuration);
public abstract void convert(MapFunction mapFunction, Configuration configuration);
public abstract void delete(Configuration configuration);
@ -110,8 +110,8 @@ public abstract class PathRef implements Comparable<PathRef> {
throw new InvalidModificationException("Invalid delete operation");
}
public void convert(ValueConverter valueConverter, Configuration configuration){
throw new InvalidModificationException("Invalid convert operation");
public void convert(MapFunction mapFunction, Configuration configuration){
throw new InvalidModificationException("Invalid map operation");
}
@Override
@ -161,9 +161,9 @@ public abstract class PathRef implements Comparable<PathRef> {
configuration.jsonProvider().setArrayIndex(parent, index, newVal);
}
public void convert(ValueConverter valueConverter, Configuration configuration){
public void convert(MapFunction mapFunction, Configuration configuration){
Object currentValue = configuration.jsonProvider().getArrayIndex(parent, index);
configuration.jsonProvider().setArrayIndex(parent, index, valueConverter.convert(currentValue, configuration));
configuration.jsonProvider().setArrayIndex(parent, index, mapFunction.map(currentValue, configuration));
}
public void delete(Configuration configuration){
@ -234,9 +234,9 @@ public abstract class PathRef implements Comparable<PathRef> {
}
@Override
public void convert(ValueConverter valueConverter, Configuration configuration) {
public void convert(MapFunction mapFunction, Configuration configuration) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
}
@ -297,10 +297,10 @@ public abstract class PathRef implements Comparable<PathRef> {
configuration.jsonProvider().setProperty(parent, property, newVal);
}
}
public void convert(ValueConverter valueConverter, Configuration configuration) {
public void convert(MapFunction mapFunction, Configuration configuration) {
for (String property : properties) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
}
}

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

@ -248,7 +248,7 @@ public final class Utils {
* Returns an upper case hexadecimal <code>String</code> for the given
* character.
*
* @param ch The character to convert.
* @param ch The character to map.
* @return An upper case hexadecimal <code>String</code>
*/
public static String hex(char ch) {

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

@ -266,43 +266,43 @@ public class WriteTest extends BaseTest {
}
@Test(expected = InvalidModificationException.class)
public void rootCannotBeConverted(){
ValueConverter valueConverter = new ValueConverter() {
public void rootCannotBeMapped(){
MapFunction mapFunction = new MapFunction() {
@Override
public Object convert(Object currentValue, Configuration configuration) {
public Object map(Object currentValue, Configuration configuration) {
return currentValue.toString()+"converted";
}
};
Object o = parse(JSON_DOCUMENT).convert("$", valueConverter).json();
Object o = parse(JSON_DOCUMENT).map("$", mapFunction).json();
}
@Test
public void single_match_value_can_be_converted(){
ValueConverter valueConverter = new ToStringValueConverterImpl();
String stringResult = parse(JSON_DOCUMENT).convert("$.string-property", valueConverter).read("$.string-property");
public void single_match_value_can_be_mapped(){
MapFunction mapFunction = new ToStringMapFunction();
String stringResult = parse(JSON_DOCUMENT).map("$.string-property", mapFunction).read("$.string-property");
assertThat(stringResult.endsWith("converted")).isTrue();
}
@Test
public void object_can_be_converted(){
public void object_can_be_mapped(){
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {};
ValueConverter valueConverter = new ToStringValueConverterImpl();
MapFunction mapFunction = new ToStringMapFunction();
DocumentContext documentContext = JsonPath.using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT);
Object list = documentContext.read("$..book");
assertThat(list).isInstanceOf(List.class);
String result = documentContext.convert("$..book", valueConverter).read("$..book", typeRef).get(0);
String result = documentContext.map("$..book", mapFunction).read("$..book", typeRef).get(0);
assertThat(result).isInstanceOf(String.class);
assertThat(result).endsWith("converted");
}
@Test
public void multi_match_path_can_be_converted(){
ValueConverter valueConverter = new ToStringValueConverterImpl();
public void multi_match_path_can_be_mapped(){
MapFunction mapFunction = new ToStringMapFunction();
List<Double> doubleResult = parse(JSON_DOCUMENT).read("$..display-price");
for(Double dRes : doubleResult){
assertThat(dRes).isInstanceOf(Double.class);
}
List<String> stringResult = parse(JSON_DOCUMENT).convert("$..display-price", valueConverter).read("$..display-price");
List<String> stringResult = parse(JSON_DOCUMENT).map("$..display-price", mapFunction).read("$..display-price");
for(String sRes : stringResult){
assertThat(sRes).isInstanceOf(String.class);
assertThat(sRes.endsWith("converted")).isTrue();
@ -310,10 +310,10 @@ public class WriteTest extends BaseTest {
}
// Helper converter implementation for test cases.
private class ToStringValueConverterImpl implements ValueConverter{
private class ToStringMapFunction implements MapFunction {
@Override
public Object convert(Object currentValue, Configuration configuration) {
public Object map(Object currentValue, Configuration configuration) {
return currentValue.toString()+"converted";
}
}

2
json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java → json-path/src/test/java/com/jayway/jsonpath/internal/JsonContextTest.java

@ -10,7 +10,7 @@ import org.junit.Test;
import java.util.List;
public class JsonReaderTest extends BaseTest {
public class JsonContextTest extends BaseTest {
@Test
public void cached_path_with_predicates() {
Loading…
Cancel
Save