Browse Source

Improved API and added some doc.

pull/10/merge
Kalle Stenflo 11 years ago
parent
commit
47cb1c6f06
  1. 14
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  3. 128
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  4. 14
      json-path/src/main/java/com/jayway/jsonpath/Option.java
  5. 14
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  6. 34
      json-path/src/main/java/com/jayway/jsonpath/ReadContext.java
  7. 3
      json-path/src/main/java/com/jayway/jsonpath/Transformer.java
  8. 4
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  9. 4
      json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java
  10. 14
      json-path/src/test/java/com/jayway/jsonpath/JsonModelOpsTest.java
  11. 4
      json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java
  12. 1
      json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java

14
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; package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;

10
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 * 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 * @param transformer the transformer to use
* @return this {@link ObjectOps} * @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 * 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 * @param transformer the transformer to use
* @return this {@link ArrayOps} * @return this {@link ArrayOps}
@ -922,7 +922,7 @@ public class JsonModel {
@Override @Override
public ObjectOps transform(Transformer<Map<String, Object>> transformer) { public ObjectOps transform(Transformer<Map<String, Object>> transformer) {
Map targetObject = getTargetObject(jsonPath, Map.class); Map targetObject = getTargetObject(jsonPath, Map.class);
Object transformed = transformer.transform(targetObject); Object transformed = transformer.transform(targetObject, configuration);
setTargetObject(jsonPath, transformed); setTargetObject(jsonPath, transformed);
return this; return this;
} }
@ -984,7 +984,7 @@ public class JsonModel {
@Override @Override
public ArrayOps transform(Transformer<List<Object>> transformer) { public ArrayOps transform(Transformer<List<Object>> transformer) {
Object transformed = transformer.transform(getTargetObject(jsonPath, List.class)); Object transformed = transformer.transform(getTargetObject(jsonPath, List.class), configuration);
setTargetObject(jsonPath, transformed); setTargetObject(jsonPath, transformed);
return this; return this;
} }
@ -994,7 +994,7 @@ public class JsonModel {
List targetObject = getTargetObject(jsonPath, List.class); List targetObject = getTargetObject(jsonPath, List.class);
for (int i = 0; i < targetObject.size(); i++) { 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; return this;
} }

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

@ -255,12 +255,10 @@ public class JsonPath {
result = filter.filter(result, configuration, contextFilters, inArrayContext); result = filter.filter(result, configuration, contextFilters, inArrayContext);
if(result == null && !pathToken.isEndToken()){ if(result == null && !pathToken.isEndToken()){
throw new PathNotFoundException("Path token: '" + pathToken.getFragment() + "' not found."); throw new PathNotFoundException("Path token: '" + pathToken.getFragment() + "' not found.");
} }
if (!inArrayContext) { if (!inArrayContext) {
inArrayContext = filter.isArrayFilter(); 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){ public static ParseContext using(Configuration configuration){
return new JsonReader(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){ public static ParseContext using(JsonProvider provider){
return new JsonReader(Configuration.builder().jsonProvider(provider).build()); 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) { public static ReadContext parse(Object json) {
return new JsonReader().parse(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); 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); 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); 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"}) * Parses the given JSON input using the provided {@link Configuration} and
public static <T> T read(Configuration configuration, Object json, String path, Filter... filters){ * returns a {@link ReadContext} for path evaluation
return compile(path, filters).read(json, configuration); *
* @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> T read(Configuration configuration, String json, String path, Filter... filters){ * Parses the given JSON input using the provided {@link Configuration} and
return read(configuration, configuration.getProvider().parse(json), path, filters); * 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);
}
} }

14
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; package com.jayway.jsonpath;
/** /**

14
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; package com.jayway.jsonpath;
import java.io.File; import java.io.File;

34
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; package com.jayway.jsonpath;
/** /**
@ -7,9 +21,29 @@ package com.jayway.jsonpath;
*/ */
public interface ReadContext { public interface ReadContext {
/**
* Returns the JSON model that this context is reading
*
* @return json model
*/
Object json(); Object json();
/**
* Reads the given path from this context
*
* @param path path to read
* @param filters filters
* @param <T>
* @return result
*/
<T> T read(String path, Filter... filters); <T> T read(String path, Filter... filters);
/**
* Reads the given path from this context
*
* @param path path to apply
* @param <T>
* @return result
*/
<T> T read(JsonPath path); <T> T read(JsonPath path);
} }

3
json-path/src/main/java/com/jayway/jsonpath/Transformer.java

@ -22,9 +22,10 @@ public interface Transformer<T> {
/** /**
* *
* @param obj object to transform * @param obj object to transform
* @param configuration configuration to use
* @return the transformed object * @return the transformed object
*/ */
public Object transform(T obj); public Object transform(T obj, Configuration configuration);

4
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -134,14 +134,14 @@ public class IssuesTest {
//Configuration configuration = Configuration.defaultConfiguration(); //Configuration configuration = Configuration.defaultConfiguration();
String json = "{\"a\":{\"b\":1,\"c\":2}}"; 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 @Test
public void issue_22c() throws Exception { public void issue_22c() throws Exception {
Configuration configuration = Configuration.builder().build(); Configuration configuration = Configuration.builder().build();
String json = "{\"a\":{\"b\":1,\"c\":2}}"; String json = "{\"a\":{\"b\":1,\"c\":2}}";
assertNull(JsonPath.read(configuration, json, "a.d")); assertNull(JsonPath.parse(json, configuration).read("a.d"));
} }

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

@ -58,7 +58,7 @@ public class JsonModelChainedCallsTest {
JsonModel model = JsonModel.model(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Map<String, Object>> transformer = new Transformer<Map<String, Object>>() { Transformer<Map<String, Object>> transformer = new Transformer<Map<String, Object>>() {
public Object transform(Map<String, Object> map) { public Object transform(Map<String, Object> map, Configuration configuration) {
map.remove("isbn"); map.remove("isbn");
map.put("author", "kalle"); map.put("author", "kalle");
return map; return map;
@ -77,7 +77,7 @@ public class JsonModelChainedCallsTest {
JsonModel model = JsonModel.model(DOCUMENT); JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Object> transformer = new Transformer<Object>() { Transformer<Object> transformer = new Transformer<Object>() {
public Object transform(Object obj) { public Object transform(Object obj, Configuration configuration) {
Map<String, Object> map = (Map<String, Object>) obj; Map<String, Object> map = (Map<String, Object>) obj;
map.remove("isbn"); map.remove("isbn");
map.put("author", "kalle"); map.put("author", "kalle");

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

@ -117,7 +117,7 @@ public class JsonModelOpsTest {
model.opsForObject().transform(new Transformer<Map<String, Object>>() { model.opsForObject().transform(new Transformer<Map<String, Object>>() {
@Override @Override
public Object transform(Map<String, Object> obj) { public Object transform(Map<String, Object> obj, Configuration configuration) {
obj.put("name", "kalle"); obj.put("name", "kalle");
return obj; return obj;
} }
@ -138,7 +138,7 @@ public class JsonModelOpsTest {
model.opsForArray().transform(new Transformer<List<Object>>() { model.opsForArray().transform(new Transformer<List<Object>>() {
@Override @Override
public Object transform(List<Object> obj) { public Object transform(List<Object> obj, Configuration configuration) {
return Collections.singletonMap("root", "new"); return Collections.singletonMap("root", "new");
} }
}); });
@ -163,7 +163,7 @@ public class JsonModelOpsTest {
model.opsForObject("child").transform(new Transformer<Map<String, Object>>() { model.opsForObject("child").transform(new Transformer<Map<String, Object>>() {
@Override @Override
public Object transform(Map<String, Object> obj) { public Object transform(Map<String, Object> obj, Configuration configuration) {
obj.put("name", "kalle"); obj.put("name", "kalle");
return obj; return obj;
} }
@ -204,7 +204,7 @@ public class JsonModelOpsTest {
Transformer transformer = new Transformer<Map<String, Object>>() { Transformer transformer = new Transformer<Map<String, Object>>() {
@Override @Override
public Map<String, Object> transform(Map<String, Object> model) { public Map<String, Object> transform(Map<String, Object> model, Configuration configuration) {
model.put("newProp", "newProp"); model.put("newProp", "newProp");
return model; return model;
} }
@ -221,7 +221,7 @@ public class JsonModelOpsTest {
public void arrays_can_be_transformed() throws Exception { public void arrays_can_be_transformed() throws Exception {
Transformer transformer = new Transformer<List<Object>>() { Transformer transformer = new Transformer<List<Object>>() {
@Override @Override
public Object transform(List<Object> model) { public Object transform(List<Object> model, Configuration configuration) {
for (Object o : model) { for (Object o : model) {
Map<String, Object> map = (Map<String, Object>) o; Map<String, Object> map = (Map<String, Object>) o;
@ -245,7 +245,7 @@ public class JsonModelOpsTest {
private int i = 0; private int i = 0;
@Override @Override
public Object transform(List<Object> model) { public Object transform(List<Object> model, Configuration configuration) {
List<Object> newList = new ArrayList<Object>(); List<Object> newList = new ArrayList<Object>();
for (Object o : model) { for (Object o : model) {
@ -258,7 +258,7 @@ public class JsonModelOpsTest {
Transformer multiplyingTransformer = new Transformer<List<Object>>() { Transformer multiplyingTransformer = new Transformer<List<Object>>() {
@Override @Override
public Object transform(List<Object> model) { public Object transform(List<Object> model, Configuration configuration) {
for (int i = 0; i < model.size(); i++) { for (int i = 0; i < model.size(); i++) {
int curr = (Integer) model.get(i); int curr = (Integer) model.get(i);

4
json-path/src/test/java/com/jayway/jsonpath/JsonModelSubModelTest.java

@ -91,7 +91,7 @@ public class JsonModelSubModelTest {
JsonModel subModel = model.getSubModel("store.book[0]"); JsonModel subModel = model.getSubModel("store.book[0]");
subModel.opsForObject().transform(new Transformer<Map<String, Object>>() { subModel.opsForObject().transform(new Transformer<Map<String, Object>>() {
@Override @Override
public Object transform(Map<String, Object> obj) { public Object transform(Map<String, Object> obj, Configuration configuration) {
return Collections.singletonMap("prop", "new"); return Collections.singletonMap("prop", "new");
} }
}); });
@ -106,7 +106,7 @@ public class JsonModelSubModelTest {
JsonModel subModel = model.getSubModel("store.bicycle.book"); JsonModel subModel = model.getSubModel("store.bicycle.book");
subModel.opsForObject().transform(new Transformer<Map<String, Object>>() { subModel.opsForObject().transform(new Transformer<Map<String, Object>>() {
@Override @Override
public Object transform(Map<String, Object> obj) { public Object transform(Map<String, Object> obj, Configuration configuration) {
return Collections.singletonMap("prop", "new"); return Collections.singletonMap("prop", "new");
} }
}); });

1
json-path/src/test/java/com/jayway/jsonpath/reader/ReadConfigurationTest.java

@ -35,7 +35,6 @@ public class ReadConfigurationTest {
JsonPath.using(configuration).parse("{}").read("$"); JsonPath.using(configuration).parse("{}").read("$");
JsonPath.read(configuration, "{}", "$");

Loading…
Cancel
Save