Browse Source

Made write features JsonProvider neutral.

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

1
build.gradle

@ -41,7 +41,6 @@ subprojects {
apply plugin: 'java'
apply plugin: 'signing'
apply plugin: 'osgi'
apply plugin: 'maven'
sourceCompatibility = 1.6
targetCompatibility = 1.6

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

@ -15,7 +15,12 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.*;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.JsonReader;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathCompiler;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.io.File;
@ -221,9 +226,19 @@ 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);
@ -231,7 +246,6 @@ public class JsonPath {
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
/**
* Deletes the object this path points to in the provided jsonObject
*

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);
}

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

@ -14,8 +14,6 @@
*/
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.ValueConverter;
public interface WriteContext {
/**
@ -62,7 +60,7 @@ public interface WriteContext {
* Converts the value on the given path.
*
* @param path path to be converted set
* @param valueConverter Converter object to be invoked (or lambda:))
* @param valueConverter Converter object to be invoked
* @param filters filters
* @return a document context
*/
@ -137,7 +135,6 @@ public interface WriteContext {
*/
DocumentContext put(String path, String key, Object value, Predicate... filters);
/**
* Add or update the key with a the given value at the given path
*
@ -168,5 +165,4 @@ public interface WriteContext {
* @return a document content.
*/
DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName);
}

2
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;
@ -36,7 +37,6 @@ import java.util.LinkedList;
import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.JsonPath.parse;
import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull;
import static java.util.Arrays.asList;

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

@ -1,10 +1,12 @@
package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.*;
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;
import java.util.List;
public abstract class PathRef implements Comparable<PathRef> {
@ -149,12 +151,10 @@ public abstract class PathRef implements Comparable<PathRef> {
private static class ArrayIndexPathRef extends PathRef {
private int index;
private Object value;
private ArrayIndexPathRef(Object parent, int index) {
super(parent);
this.index = index;
this.value = ((List<Object>) parent).get(index);
}
public void set(Object newVal, Configuration configuration){
@ -163,11 +163,11 @@ public abstract class PathRef implements Comparable<PathRef> {
public void convert(ValueConverter valueConverter, Configuration configuration){
Object currentValue = configuration.jsonProvider().getArrayIndex(parent, index);
configuration.jsonProvider().setArrayIndex(parent, index, valueConverter.convert(currentValue));
configuration.jsonProvider().setArrayIndex(parent, index, valueConverter.convert(currentValue, configuration));
}
public void delete(Configuration configuration){
configuration.jsonProvider().removeProperty(parent, value);
configuration.jsonProvider().removeProperty(parent, index);
}
public void add(Object value, Configuration configuration){
@ -236,7 +236,7 @@ public abstract class PathRef implements Comparable<PathRef> {
@Override
public void convert(ValueConverter valueConverter, Configuration configuration) {
Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue));
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
}
@ -300,7 +300,7 @@ public abstract class PathRef implements Comparable<PathRef> {
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.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue, configuration));
}
}

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

@ -1,10 +0,0 @@
package com.jayway.jsonpath.internal;
/**
* Created by tom on 29/04/15.
*/
public interface ValueConverter {
public Object convert(Object currentValue);
}

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;

8
json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java

@ -99,16 +99,16 @@ public abstract class AbstractJsonProvider implements JsonProvider {
* Removes a value in an object or array
*
* @param obj an array or an object
* @param key a String key or an object in a Collection to be removed.
* @param key a String key or a numerical index to remove
*/
@SuppressWarnings("unchecked")
public void removeProperty(Object obj, Object key) {
if (isMap(obj))
((Map) obj).remove(key.toString());
else {
Collection collection = (Collection) obj;
//int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
collection.remove(key);
List list = (List) obj;
int index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
list.remove(index);
}
}

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 {

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

@ -1,9 +1,12 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.ValueConverter;
import org.junit.Test;
import java.util.*;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static com.jayway.jsonpath.JsonPath.parse;
import static java.util.Collections.emptyMap;
@ -126,8 +129,9 @@ public class WriteTest extends BaseTest {
@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(getClass().getResourceAsStream("/json_array_multiple_delete.json"));
DocumentContext documentContext = JsonPath.parse(stream);
documentContext.delete(deletePath);
List<Object> result = documentContext.read(deletePath);
assertThat(result.size()).isEqualTo(0);
@ -265,7 +269,7 @@ public class WriteTest extends BaseTest {
public void rootCannotBeConverted(){
ValueConverter valueConverter = new ValueConverter() {
@Override
public Object convert(Object currentValue) {
public Object convert(Object currentValue, Configuration configuration) {
return currentValue.toString()+"converted";
}
};
@ -281,13 +285,14 @@ public class WriteTest extends BaseTest {
@Test
public void object_can_be_converted(){
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {};
ValueConverter valueConverter = new ToStringValueConverterImpl();
DocumentContext documentContext = parse(JSON_DOCUMENT);
DocumentContext documentContext = JsonPath.using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT);
Object list = documentContext.read("$..book");
assertThat(list).isInstanceOf(List.class);
String result = ((List<String>)documentContext.convert("$..book", valueConverter).read("$..book")).get(0);
String result = documentContext.convert("$..book", valueConverter).read("$..book", typeRef).get(0);
assertThat(result).isInstanceOf(String.class);
assertThat(((String)result).endsWith("converted")).isTrue();
assertThat(result).endsWith("converted");
}
@Test
@ -308,7 +313,7 @@ public class WriteTest extends BaseTest {
private class ToStringValueConverterImpl implements ValueConverter{
@Override
public Object convert(Object currentValue) {
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

Loading…
Cancel
Save