diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index f1f88f91..a9b9dbf4 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/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 convert(Object jsonObject, ValueConverter valueConverter, Configuration configuration) { + public 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 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 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 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 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 resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) { diff --git a/json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java b/json-path/src/main/java/com/jayway/jsonpath/MapFunction.java similarity index 80% rename from json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java rename to json-path/src/main/java/com/jayway/jsonpath/MapFunction.java index a731c610..59e8d7f3 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java +++ b/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); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java b/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java index 32fcc81c..751cbc5d 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/WriteContext.java @@ -40,9 +40,9 @@ public interface WriteContext { /** * Set the value a the given path * - * @param path path to set - * @param newValue new value - * @param filters filters + * @param path path to set + * @param newValue new value + * @param filters filters * @return a document context */ DocumentContext set(String path, Object newValue, Predicate... filters); @@ -50,30 +50,30 @@ public interface WriteContext { /** * Set the value a the given path * - * @param path path to set - * @param newValue new value + * @param path path to set + * @param newValue new value * @return a document context */ 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 filters filters + * @param path path to be converted set + * @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 path path to be converted set + * @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 diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java similarity index 93% rename from json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java rename to json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java index 4ff8009a..17ce9bce 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java +++ b/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){ diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java index c93742b8..0ca3eaaf 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java +++ b/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 { 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 { 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 { 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 { 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 { } @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 { 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)); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java b/json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java index 37649552..976c0209 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/Utils.java @@ -248,7 +248,7 @@ public final class Utils { * Returns an upper case hexadecimal String for the given * character. * - * @param ch The character to convert. + * @param ch The character to map. * @return An upper case hexadecimal String */ public static String hex(char ch) { diff --git a/json-path/src/test/java/com/jayway/jsonpath/WriteTest.java b/json-path/src/test/java/com/jayway/jsonpath/WriteTest.java index e06f062d..5b33a37a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/WriteTest.java +++ b/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> typeRef = new TypeRef>() {}; - 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 doubleResult = parse(JSON_DOCUMENT).read("$..display-price"); for(Double dRes : doubleResult){ assertThat(dRes).isInstanceOf(Double.class); } - List stringResult = parse(JSON_DOCUMENT).convert("$..display-price", valueConverter).read("$..display-price"); + List 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"; } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java b/json-path/src/test/java/com/jayway/jsonpath/internal/JsonContextTest.java similarity index 94% rename from json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java rename to json-path/src/test/java/com/jayway/jsonpath/internal/JsonContextTest.java index 33586dcc..e430c37b 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java +++ b/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() {