From 47cb1c6f06d6032ee949b666e27353196ecb767e Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Fri, 6 Sep 2013 15:41:40 +0200 Subject: [PATCH] Improved API and added some doc. --- .../com/jayway/jsonpath/Configuration.java | 14 ++ .../java/com/jayway/jsonpath/JsonModel.java | 10 +- .../java/com/jayway/jsonpath/JsonPath.java | 128 ++++++++++++++---- .../main/java/com/jayway/jsonpath/Option.java | 14 ++ .../com/jayway/jsonpath/ParseContext.java | 14 ++ .../java/com/jayway/jsonpath/ReadContext.java | 34 +++++ .../java/com/jayway/jsonpath/Transformer.java | 3 +- .../java/com/jayway/jsonpath/IssuesTest.java | 4 +- .../jsonpath/JsonModelChainedCallsTest.java | 4 +- .../com/jayway/jsonpath/JsonModelOpsTest.java | 14 +- .../jsonpath/JsonModelSubModelTest.java | 4 +- .../reader/ReadConfigurationTest.java | 1 - 12 files changed, 201 insertions(+), 43 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/Configuration.java b/json-path/src/main/java/com/jayway/jsonpath/Configuration.java index 650cf7d0..ddd940c8 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Configuration.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Configuration.java @@ -1,3 +1,17 @@ +/* + * Copyright 2011 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jayway.jsonpath; import com.jayway.jsonpath.spi.JsonProvider; diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java index 53d1957e..d5bf2cbc 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java @@ -767,7 +767,7 @@ public class JsonModel { /** * Allows transformations of the target object. the target for this {@link ObjectOps} will be be replaced - * with the {@link Object} returned by the {@link Transformer#transform(Object)} + * with the {@link Object} returned by the {@link Transformer#transform(Object, Configuration)} * * @param transformer the transformer to use * @return this {@link ObjectOps} @@ -823,7 +823,7 @@ public class JsonModel { /** * Allows transformations of the target list. The target for this {@link ArrayOps} will be be replaced - * with the {@link Object} returned by the {@link Transformer#transform(Object)} + * with the {@link Object} returned by the {@link Transformer#transform(Object, Configuration)} * * @param transformer the transformer to use * @return this {@link ArrayOps} @@ -922,7 +922,7 @@ public class JsonModel { @Override public ObjectOps transform(Transformer> transformer) { Map targetObject = getTargetObject(jsonPath, Map.class); - Object transformed = transformer.transform(targetObject); + Object transformed = transformer.transform(targetObject, configuration); setTargetObject(jsonPath, transformed); return this; } @@ -984,7 +984,7 @@ public class JsonModel { @Override public ArrayOps transform(Transformer> transformer) { - Object transformed = transformer.transform(getTargetObject(jsonPath, List.class)); + Object transformed = transformer.transform(getTargetObject(jsonPath, List.class), configuration); setTargetObject(jsonPath, transformed); return this; } @@ -994,7 +994,7 @@ public class JsonModel { List targetObject = getTargetObject(jsonPath, List.class); for (int i = 0; i < targetObject.size(); i++) { - targetObject.set(i, transformer.transform(targetObject.get(i))); + targetObject.set(i, transformer.transform(targetObject.get(i), configuration)); } return this; } 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 d9f81f1b..746d02f1 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -255,12 +255,10 @@ public class JsonPath { result = filter.filter(result, configuration, contextFilters, inArrayContext); - if(result == null && !pathToken.isEndToken()){ throw new PathNotFoundException("Path token: '" + pathToken.getFragment() + "' not found."); } - if (!inArrayContext) { inArrayContext = filter.isArrayFilter(); } @@ -508,56 +506,140 @@ public class JsonPath { } - //------- + // -------------------------------------------------------- + // + // Static Fluent API + // + // -------------------------------------------------------- + + + /** + * Creates a {@link ParseContext} that can be used to parse a given JSON input. + * + * @param configuration configuration to use when parsing JSON + * @return a parsing context based on given configuration + */ public static ParseContext using(Configuration configuration){ return new JsonReader(configuration); } + /** + * Creates a {@link ParseContext} that will parse a given JSON input. + * + * @param provider provider to use when parsing JSON + * @return a parsing context based on given provider + */ public static ParseContext using(JsonProvider provider){ return new JsonReader(Configuration.builder().jsonProvider(provider).build()); } - public static ReadContext parse(Object json, Configuration configuration) { - return new JsonReader(configuration).parse(json); - } - + /** + * Parses the given JSON input using the default {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ public static ReadContext parse(Object json) { return new JsonReader().parse(json); } - public static ReadContext parse(String json, Configuration configuration){ - return new JsonReader(configuration).parse(json); + /** + * Parses the given JSON input using the default {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json string + * @return a read context + */ + public static ReadContext parse(String json) { + return new JsonReader().parse(json); } - public static ReadContext parse(String json) { + /** + * Parses the given JSON input using the default {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json stream + * @return a read context + */ + public static ReadContext parse(InputStream json) { return new JsonReader().parse(json); } - public static ReadContext parse(InputStream json, Configuration configuration) { - return new JsonReader(configuration).parse(json); + /** + * Parses the given JSON input using the default {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json file + * @return a read context + */ + public static ReadContext parse(File json) throws IOException { + return new JsonReader().parse(json); } - public static ReadContext parse(InputStream json) { + /** + * Parses the given JSON input using the default {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json url + * @return a read context + */ + public static ReadContext parse(URL json) throws IOException { return new JsonReader().parse(json); } - public static ReadContext parse(File json, Configuration configuration) throws IOException { + /** + * Parses the given JSON input using the provided {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ + public static ReadContext parse(Object json, Configuration configuration) { return new JsonReader(configuration).parse(json); } - public static ReadContext parse(File json) throws IOException { - return new JsonReader().parse(json); + /** + * Parses the given JSON input using the provided {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ + public static ReadContext parse(String json, Configuration configuration){ + return new JsonReader(configuration).parse(json); } - - @SuppressWarnings({"unchecked"}) - public static T read(Configuration configuration, Object json, String path, Filter... filters){ - return compile(path, filters).read(json, configuration); + /** + * Parses the given JSON input using the provided {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ + public static ReadContext parse(InputStream json, Configuration configuration) { + return new JsonReader(configuration).parse(json); } - @SuppressWarnings({"unchecked"}) - public static T read(Configuration configuration, String json, String path, Filter... filters){ - return read(configuration, configuration.getProvider().parse(json), path, filters); + /** + * Parses the given JSON input using the provided {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ + public static ReadContext parse(File json, Configuration configuration) throws IOException { + return new JsonReader(configuration).parse(json); } + /** + * Parses the given JSON input using the provided {@link Configuration} and + * returns a {@link ReadContext} for path evaluation + * + * @param json input + * @return a read context + */ + public static ReadContext parse(URL json, Configuration configuration) throws IOException { + return new JsonReader(configuration).parse(json); + } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/Option.java b/json-path/src/main/java/com/jayway/jsonpath/Option.java index d88f5d0d..a45749fa 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Option.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Option.java @@ -1,3 +1,17 @@ +/* + * Copyright 2011 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jayway.jsonpath; /** diff --git a/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java b/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java index f1b64746..9b64e932 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java @@ -1,3 +1,17 @@ +/* + * Copyright 2011 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jayway.jsonpath; import java.io.File; diff --git a/json-path/src/main/java/com/jayway/jsonpath/ReadContext.java b/json-path/src/main/java/com/jayway/jsonpath/ReadContext.java index a6dd4c99..3dbc52ce 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/ReadContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/ReadContext.java @@ -1,3 +1,17 @@ +/* + * Copyright 2011 the original author or authors. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jayway.jsonpath; /** @@ -7,9 +21,29 @@ package com.jayway.jsonpath; */ public interface ReadContext { + /** + * Returns the JSON model that this context is reading + * + * @return json model + */ Object json(); + /** + * Reads the given path from this context + * + * @param path path to read + * @param filters filters + * @param + * @return result + */ T read(String path, Filter... filters); + /** + * Reads the given path from this context + * + * @param path path to apply + * @param + * @return result + */ T read(JsonPath path); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/Transformer.java b/json-path/src/main/java/com/jayway/jsonpath/Transformer.java index 2f2a7457..82f01fed 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Transformer.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Transformer.java @@ -22,9 +22,10 @@ public interface Transformer { /** * * @param obj object to transform + * @param configuration configuration to use * @return the transformed object */ - public Object transform(T obj); + public Object transform(T obj, Configuration configuration); diff --git a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java b/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java index f53f3d09..7d4d73d1 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java @@ -134,14 +134,14 @@ public class IssuesTest { //Configuration configuration = Configuration.defaultConfiguration(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; - System.out.println(JsonPath.read(configuration, json, "a.d")); + System.out.println(JsonPath.parse(json, configuration).read("a.d")); } @Test public void issue_22c() throws Exception { Configuration configuration = Configuration.builder().build(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; - assertNull(JsonPath.read(configuration, json, "a.d")); + assertNull(JsonPath.parse(json, configuration).read("a.d")); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java index 93362f09..703d5450 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java @@ -58,7 +58,7 @@ public class JsonModelChainedCallsTest { JsonModel model = JsonModel.model(DOCUMENT); Transformer> transformer = new Transformer>() { - public Object transform(Map map) { + public Object transform(Map map, Configuration configuration) { map.remove("isbn"); map.put("author", "kalle"); return map; @@ -77,7 +77,7 @@ public class JsonModelChainedCallsTest { JsonModel model = JsonModel.model(DOCUMENT); Transformer transformer = new Transformer() { - public Object transform(Object obj) { + public Object transform(Object obj, Configuration configuration) { Map map = (Map) obj; map.remove("isbn"); map.put("author", "kalle"); diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java index 1c12c165..86380b51 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java @@ -117,7 +117,7 @@ public class JsonModelOpsTest { model.opsForObject().transform(new Transformer>() { @Override - public Object transform(Map obj) { + public Object transform(Map obj, Configuration configuration) { obj.put("name", "kalle"); return obj; } @@ -138,7 +138,7 @@ public class JsonModelOpsTest { model.opsForArray().transform(new Transformer>() { @Override - public Object transform(List obj) { + public Object transform(List obj, Configuration configuration) { return Collections.singletonMap("root", "new"); } }); @@ -163,7 +163,7 @@ public class JsonModelOpsTest { model.opsForObject("child").transform(new Transformer>() { @Override - public Object transform(Map obj) { + public Object transform(Map obj, Configuration configuration) { obj.put("name", "kalle"); return obj; } @@ -204,7 +204,7 @@ public class JsonModelOpsTest { Transformer transformer = new Transformer>() { @Override - public Map transform(Map model) { + public Map transform(Map model, Configuration configuration) { model.put("newProp", "newProp"); return model; } @@ -221,7 +221,7 @@ public class JsonModelOpsTest { public void arrays_can_be_transformed() throws Exception { Transformer transformer = new Transformer>() { @Override - public Object transform(List model) { + public Object transform(List model, Configuration configuration) { for (Object o : model) { Map map = (Map) o; @@ -245,7 +245,7 @@ public class JsonModelOpsTest { private int i = 0; @Override - public Object transform(List model) { + public Object transform(List model, Configuration configuration) { List newList = new ArrayList(); for (Object o : model) { @@ -258,7 +258,7 @@ public class JsonModelOpsTest { Transformer multiplyingTransformer = new Transformer>() { @Override - public Object transform(List model) { + public Object transform(List model, Configuration configuration) { for (int i = 0; i < model.size(); i++) { int curr = (Integer) model.get(i); diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java index 5c075852..b575ce4c 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java @@ -91,7 +91,7 @@ public class JsonModelSubModelTest { JsonModel subModel = model.getSubModel("store.book[0]"); subModel.opsForObject().transform(new Transformer>() { @Override - public Object transform(Map obj) { + public Object transform(Map obj, Configuration configuration) { return Collections.singletonMap("prop", "new"); } }); @@ -106,7 +106,7 @@ public class JsonModelSubModelTest { JsonModel subModel = model.getSubModel("store.bicycle.book"); subModel.opsForObject().transform(new Transformer>() { @Override - public Object transform(Map obj) { + public Object transform(Map obj, Configuration configuration) { return Collections.singletonMap("prop", "new"); } }); diff --git a/json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java b/json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java index 3f0b1eeb..c5fd113b 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java @@ -35,7 +35,6 @@ public class ReadConfigurationTest { JsonPath.using(configuration).parse("{}").read("$"); - JsonPath.read(configuration, "{}", "$");