Browse Source

Merge branch 'atomcat1978-master'

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
6bc270a82a
  1. 31
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 21
      json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java
  3. 39
      json-path/src/main/java/com/jayway/jsonpath/WriteContext.java
  4. 30
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  5. 95
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/Function.java
  7. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/function/FunctionFactory.java
  8. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java
  9. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PassthruFunction.java
  10. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  11. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/token/FunctionPathToken.java
  12. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java
  13. 2
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  14. 7
      json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java
  15. 1
      json-path/src/test/java/com/jayway/jsonpath/GsonJsonProviderTest.java
  16. 5
      json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java
  17. 3
      json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java
  18. 1
      json-path/src/test/java/com/jayway/jsonpath/MultiPropTest.java
  19. 102
      json-path/src/test/java/com/jayway/jsonpath/WriteTest.java
  20. 11
      json-path/src/test/java/com/jayway/jsonpath/functions/NumericFunctionTest.java
  21. 11
      json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java
  22. 9
      json-path/src/test/java/com/jayway/jsonpath/internal/token/PathTokenTest.java
  23. 54
      json-path/src/test/resources/json_array_multiple_delete.json

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

@ -226,6 +226,26 @@ public class JsonPath {
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
/**
* Converts the value on the given path.
*
* @param jsonObject a json object
* @param valueConverter 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) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
notNull(valueConverter, "valueConverter can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.convert(valueConverter, configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
/**
* Deletes the object this path points to in the provided jsonObject
*
@ -284,6 +304,17 @@ public class JsonPath {
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
public <T> T renameKey(Object jsonObject, String oldKeyName, String newKeyName, Configuration configuration){
notNull(jsonObject, "json can not be null");
notEmpty(newKeyName, "newKeyName can not be null or empty");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.renameKey(oldKeyName, newKeyName, configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
/**
* Applies this JsonPath to the provided json string
*

21
json-path/src/main/java/com/jayway/jsonpath/ValueConverter.java

@ -0,0 +1,21 @@
/*
* 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;
public interface ValueConverter {
public Object convert(Object currentValue, Configuration configuration);
}

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

@ -56,6 +56,25 @@ public interface WriteContext {
*/
DocumentContext set(JsonPath path, Object newValue);
/**
* Converts the value on the given path.
*
* @param path path to be converted set
* @param valueConverter Converter object to be invoked
* @param filters filters
* @return a document context
*/
DocumentContext convert(String path, ValueConverter valueConverter, Predicate... filters);
/**
* Converts the value on the given path.
*
* @param path path to be converted set
* @param valueConverter Converter object to be invoked (or lambda:))
* @return a document context
*/
DocumentContext convert(JsonPath path, ValueConverter valueConverter);
/**
* Deletes the given path
*
@ -126,4 +145,24 @@ public interface WriteContext {
*/
DocumentContext put(JsonPath path, String key, Object value);
/**
* Renames the last key element of a given path.
* @param path The path to the old key. Should be resolved to a map
* or an array including map items.
* @param oldKeyName The old key name.
* @param newKeyName The new key name.
* @param filters filters.
* @return a document content.
*/
DocumentContext renameKey(String path, String oldKeyName, String newKeyName, Predicate... filters);
/**
* Renames the last key element of a given path.
* @param path The path to the old key. Should be resolved to a map
* or an array including map items.
* @param oldKeyName The old key name.
* @param newKeyName The new key name.
* @return a document content.
*/
DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName);
}

30
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -23,6 +23,7 @@ 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;
@ -212,6 +213,18 @@ public class JsonReader implements ParseContext, DocumentContext {
return this;
}
@Override
public DocumentContext convert(String path, ValueConverter valueConverter, Predicate... filters) {
convert(compile(path, filters), valueConverter);
return this;
}
@Override
public DocumentContext convert(JsonPath path, ValueConverter valueConverter) {
path.convert(json, valueConverter, configuration);
return this;
}
@Override
public DocumentContext delete(String path, Predicate... filters) {
return delete(compile(path, filters));
@ -249,6 +262,23 @@ public class JsonReader implements ParseContext, DocumentContext {
return put(compile(path, filters), key, value);
}
@Override
public DocumentContext renameKey(String path, String oldKeyName, String newKeyName, Predicate... filters) {
return renameKey(compile(path, filters), oldKeyName, newKeyName);
}
@Override
public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) {
List<String> modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){
for (String p : modified) {
logger.debug("Rename path {} new value {}", p, newKeyName);
}
}
return this;
}
@Override
public DocumentContext put(JsonPath path, String key, Object value){
List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST));

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

@ -2,6 +2,8 @@ package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidModificationException;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.ValueConverter;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.Collection;
@ -17,6 +19,9 @@ public abstract class PathRef implements Comparable<PathRef> {
@Override
public void set(Object newVal, Configuration configuration) {}
@Override
public void convert(ValueConverter valueConverter, Configuration configuration) {}
@Override
public void delete(Configuration configuration) {}
@ -25,6 +30,10 @@ public abstract class PathRef implements Comparable<PathRef> {
@Override
public void put(String key, Object newVal, Configuration configuration) {}
@Override
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {}
};
protected Object parent;
@ -38,12 +47,32 @@ 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 delete(Configuration configuration);
public abstract void add(Object newVal, Configuration configuration);
public abstract void put(String key, Object newVal, Configuration configuration);
public abstract void renameKey(String oldKey,String newKeyName, Configuration configuration);
protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){
if(configuration.jsonProvider().isMap(targetMap)){
if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){
throw new PathNotFoundException("No results for Key "+oldKeyName+" found in map!");
}
configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName));
configuration.jsonProvider().removeProperty(targetMap, oldKeyName);
} else {
throw new InvalidModificationException("Can only rename properties in a map");
}
}
protected boolean targetInvalid(Object target){
return target == JsonProvider.UNDEFINED || target == null;
}
@Override
public int compareTo(PathRef o) {
return this.getAccessor().toString().compareTo(o.getAccessor().toString()) * -1;
@ -81,6 +110,10 @@ 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");
}
@Override
public void delete(Configuration configuration) {
throw new InvalidModificationException("Invalid delete operation");
@ -103,7 +136,18 @@ public abstract class PathRef implements Comparable<PathRef> {
throw new InvalidModificationException("Invalid put operation. $ is not a map");
}
}
@Override
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {
Object target = parent;
if(targetInvalid(target)){
return;
}
renameInMap(target, oldKeyName, newKeyName, configuration);
}
}
private static class ArrayIndexPathRef extends PathRef {
private int index;
@ -117,13 +161,18 @@ public abstract class PathRef implements Comparable<PathRef> {
configuration.jsonProvider().setArrayIndex(parent, index, newVal);
}
public void convert(ValueConverter valueConverter, Configuration configuration){
Object currentValue = configuration.jsonProvider().getArrayIndex(parent, index);
configuration.jsonProvider().setArrayIndex(parent, index, valueConverter.convert(currentValue, configuration));
}
public void delete(Configuration configuration){
configuration.jsonProvider().removeProperty(parent, index);
}
public void add(Object value, Configuration configuration){
Object target = configuration.jsonProvider().getArrayIndex(parent, index);
if(target == JsonProvider.UNDEFINED || target == null){
if(targetInvalid(target)){
return;
}
if(configuration.jsonProvider().isArray(target)){
@ -135,7 +184,7 @@ public abstract class PathRef implements Comparable<PathRef> {
public void put(String key, Object value, Configuration configuration){
Object target = configuration.jsonProvider().getArrayIndex(parent, index);
if(target == JsonProvider.UNDEFINED || target == null){
if(targetInvalid(target)){
return;
}
if(configuration.jsonProvider().isMap(target)){
@ -145,6 +194,15 @@ public abstract class PathRef implements Comparable<PathRef> {
}
}
@Override
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {
Object target = configuration.jsonProvider().getArrayIndex(parent, index);
if(targetInvalid(target)){
return;
}
renameInMap(target, oldKeyName, newKeyName, configuration);
}
@Override
public Object getAccessor() {
return index;
@ -175,13 +233,20 @@ public abstract class PathRef implements Comparable<PathRef> {
configuration.jsonProvider().setProperty(parent, property, newVal);
}
@Override
public void convert(ValueConverter valueConverter, Configuration configuration) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
}
public void delete(Configuration configuration){
configuration.jsonProvider().removeProperty(parent, property);
}
public void add(Object value, Configuration configuration){
Object target = configuration.jsonProvider().getMapValue(parent, property);
if(target == JsonProvider.UNDEFINED || target == null){
if(targetInvalid(target)){
return;
}
if(configuration.jsonProvider().isArray(target)){
@ -193,7 +258,7 @@ public abstract class PathRef implements Comparable<PathRef> {
public void put(String key, Object value, Configuration configuration){
Object target = configuration.jsonProvider().getMapValue(parent, property);
if(target == JsonProvider.UNDEFINED || target == null){
if(targetInvalid(target)){
return;
}
if(configuration.jsonProvider().isMap(target)){
@ -203,6 +268,15 @@ public abstract class PathRef implements Comparable<PathRef> {
}
}
@Override
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {
Object target = configuration.jsonProvider().getMapValue(parent, property);
if(targetInvalid(target)){
return;
}
renameInMap(target, oldKeyName, newKeyName, configuration);
}
@Override
public Object getAccessor() {
return property;
@ -223,6 +297,12 @@ public abstract class PathRef implements Comparable<PathRef> {
configuration.jsonProvider().setProperty(parent, property, newVal);
}
}
public void convert(ValueConverter valueConverter, Configuration configuration) {
for (String property : properties) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
}
}
public void delete(Configuration configuration){
for (String property : properties) {
@ -237,7 +317,12 @@ public abstract class PathRef implements Comparable<PathRef> {
@Override
public void put(String key, Object newVal, Configuration configuration) {
throw new InvalidModificationException("Add can not be performed to multiple properties");
throw new InvalidModificationException("Put can not be performed to multiple properties");
}
@Override
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {
throw new InvalidModificationException("Rename can not be performed to multiple properties");
}
@Override

2
json-path/src/main/java/com/jayway/jsonpath/Function.java → json-path/src/main/java/com/jayway/jsonpath/internal/function/Function.java

@ -1,4 +1,4 @@
package com.jayway.jsonpath;
package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;

1
json-path/src/main/java/com/jayway/jsonpath/internal/function/FunctionFactory.java

@ -1,6 +1,5 @@
package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.internal.function.numeric.Average;
import com.jayway.jsonpath.internal.function.numeric.Max;

1
json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java

@ -1,6 +1,5 @@
package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;

1
json-path/src/main/java/com/jayway/jsonpath/internal/function/PassthruFunction.java

@ -1,6 +1,5 @@
package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java

@ -1,8 +1,8 @@
package com.jayway.jsonpath.internal.function.numeric;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Function;
/**
* Defines the pattern for processing numerical values via an abstract implementation that iterates over the collection

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

@ -1,7 +1,7 @@
package com.jayway.jsonpath.internal.token;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Function;
import com.jayway.jsonpath.internal.function.FunctionFactory;
import java.util.regex.Matcher;

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

@ -14,11 +14,11 @@
*/
package com.jayway.jsonpath.internal.token;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.function.Function;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.List;

2
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -1,13 +1,13 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.token.PredicateContextImpl;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.internal.token.PredicateContextImpl;
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
import java.util.HashMap;

7
json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java

@ -2,14 +2,13 @@ package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static com.jayway.jsonpath.JsonPath.using;
import static com.jayway.jsonpath.TestUtils.assertEvaluationThrows;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import java.util.Map;
/**
* Deep scan is indefinite, so certain "illegal" actions become a no-op instead of a path evaluation exception.
*/

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

@ -3,7 +3,6 @@ package com.jayway.jsonpath;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.jayway.jsonpath.spi.mapper.MappingException;
import org.junit.Test;

5
json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

@ -1,13 +1,12 @@
package com.jayway.jsonpath;
import static com.jayway.jsonpath.TestUtils.assertHasNoResults;
import static com.jayway.jsonpath.TestUtils.assertHasOneResult;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static com.jayway.jsonpath.TestUtils.assertHasNoResults;
import static com.jayway.jsonpath.TestUtils.assertHasOneResult;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

3
json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java

@ -4,9 +4,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.IOException;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;

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

@ -7,7 +7,6 @@ import java.util.Map;
import static com.jayway.jsonpath.JsonPath.using;
import static com.jayway.jsonpath.TestUtils.assertEvaluationThrows;
import static org.assertj.core.api.Assertions.assertThat;
public class MultiPropTest {

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

@ -2,6 +2,7 @@ package com.jayway.jsonpath;
import org.junit.Test;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@ -126,6 +127,16 @@ public class WriteTest extends BaseTest {
assertThat(res).containsExactly("reference");
}
@Test
public void an_array_criteria_with_multiple_results_can_be_deleted(){
InputStream stream = this.getClass().getResourceAsStream("/json_array_multiple_delete.json");
String deletePath = "$._embedded.mandates[?(@.count=~/0/)]";
DocumentContext documentContext = JsonPath.parse(stream);
documentContext.delete(deletePath);
List<Object> result = documentContext.read(deletePath);
assertThat(result.size()).isEqualTo(0);
}
@Test
public void multi_prop_delete() {
@ -214,6 +225,97 @@ public class WriteTest extends BaseTest {
parse(model).set("$[?(@.a == 'a-val')]", 1);
}
@Test
public void a_path_can_be_renamed(){
Object o = parse(JSON_DOCUMENT).renameKey("$.store", "book", "updated-book").json();
List<Object> result = parse(o).read("$.store.updated-book");
assertThat(result).isNotEmpty();
}
@Test
public void keys_in_root_containing_map_can_be_renamed(){
Object o = parse(JSON_DOCUMENT).renameKey("$", "store", "new-store").json();
List<Object> result = parse(o).read("$.new-store[*]");
assertThat(result).isNotEmpty();
}
@Test
public void map_array_items_can_be_renamed(){
Object o = parse(JSON_DOCUMENT).renameKey("$.store.book[*]", "category", "renamed-category").json();
List<Object> result = parse(o).read("$.store.book[*].renamed-category");
assertThat(result).isNotEmpty();
}
@Test(expected = InvalidModificationException.class)
public void non_map_array_items_cannot_be_renamed(){
List<Integer> model = new LinkedList<Integer>();
model.add(1);
model.add(2);
parse(model).renameKey("$[*]", "oldKey", "newKey");
}
@Test(expected = InvalidModificationException.class)
public void multiple_properties_cannot_be_renamed(){
parse(JSON_DOCUMENT).renameKey("$.store.book[*]['author', 'category']", "old-key", "new-key");
}
@Test(expected = PathNotFoundException.class)
public void non_existent_key_rename_not_allowed(){
Object o = parse(JSON_DOCUMENT).renameKey("$", "fake", "new-fake").json();
}
@Test(expected = InvalidModificationException.class)
public void rootCannotBeConverted(){
ValueConverter valueConverter = new ValueConverter() {
@Override
public Object convert(Object currentValue, Configuration configuration) {
return currentValue.toString()+"converted";
}
};
Object o = parse(JSON_DOCUMENT).convert("$", valueConverter).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");
assertThat(stringResult.endsWith("converted")).isTrue();
}
@Test
public void object_can_be_converted(){
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {};
ValueConverter valueConverter = new ToStringValueConverterImpl();
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);
assertThat(result).isInstanceOf(String.class);
assertThat(result).endsWith("converted");
}
@Test
public void multi_match_path_can_be_converted(){
ValueConverter valueConverter = new ToStringValueConverterImpl();
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");
for(String sRes : stringResult){
assertThat(sRes).isInstanceOf(String.class);
assertThat(sRes.endsWith("converted")).isTrue();
}
}
// Helper converter implementation for test cases.
private class ToStringValueConverterImpl implements ValueConverter{
@Override
public Object convert(Object currentValue, Configuration configuration) {
return currentValue.toString()+"converted";
}
}
}

11
json-path/src/test/java/com/jayway/jsonpath/functions/NumericFunctionTest.java

@ -2,23 +2,12 @@ package com.jayway.jsonpath.functions;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configurations;
import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONArray;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import static com.jayway.jsonpath.Configurations.*;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.runners.Parameterized.Parameters;
/**

11
json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java

@ -1,17 +1,14 @@
package com.jayway.jsonpath.internal;
import static org.junit.Assert.*;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.util.List;
public class JsonReaderTest extends BaseTest {

9
json-path/src/test/java/com/jayway/jsonpath/internal/token/PathTokenTest.java

@ -1,17 +1,12 @@
package com.jayway.jsonpath.internal.token;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.internal.token.PathToken;
import com.jayway.jsonpath.internal.token.PropertyPathToken;
import com.jayway.jsonpath.internal.token.ScanPathToken;
import com.jayway.jsonpath.internal.token.WildcardPathToken;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
public class PathTokenTest extends BaseTest {
@Test

54
json-path/src/test/resources/json_array_multiple_delete.json

@ -0,0 +1,54 @@
{
"_embedded": {
"mandates": [
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "10",
"difference": "+0"
},
{
"count": "34",
"difference": "+0"
},
{
"count": "2",
"difference": "+0"
},
{
"count": "4",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
},
{
"count": "0",
"difference": "+0"
}
]
}
}
Loading…
Cancel
Save