Browse Source

Merge b009482f76 into 45333e0a31

pull/968/merge
vishesh27111 3 months ago committed by GitHub
parent
commit
effd639acc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5817
      Test Results - Tests_in_'json-path-parent_json-path'.html
  2. 1144
      Test Results - Tests_in_'json-path-parent_json-path-assert'.html
  3. 82
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  4. 35
      json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java
  5. 35
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java
  6. 34
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  7. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PassthruPathFunction.java
  8. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunction.java
  9. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/json/Append.java
  10. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/json/KeySetFunction.java
  11. 30
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  12. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Average.java
  13. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Max.java
  14. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Min.java
  15. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/StandardDeviation.java
  16. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Sum.java
  17. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java
  18. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java
  19. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Length.java
  20. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java
  21. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathToken.java
  22. 32
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java
  23. 5
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java
  24. 34
      json-path/src/test/java/com/jayway/jsonpath/internal/JettisonProviderTest.java
  25. 46
      json-path/src/test/java/com/jayway/jsonpath/internal/JsonOrgJsonProviderTest.java
  26. 42
      json-path/src/test/java/com/jayway/jsonpath/internal/TapestryMappingProviderTest.java

5817
Test Results - Tests_in_'json-path-parent_json-path'.html

File diff suppressed because one or more lines are too long

1144
Test Results - Tests_in_'json-path-parent_json-path-assert'.html

File diff suppressed because one or more lines are too long

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

@ -16,6 +16,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.*;
import com.jayway.jsonpath.internal.PathEvaluator;
import com.jayway.jsonpath.internal.path.PathCompiler;
import com.jayway.jsonpath.spi.json.JsonProvider;
@ -139,6 +140,23 @@ public class JsonPath {
return path.isDefinite();
}
// public boolean isFunctionPath() {
// return path.isFunctionPath();
// }
//
// public boolean isRootPath() {
// return path.isRootPath();
// }
public EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration) {
return evaluate(document, rootDocument, configuration, false);
}
public EvaluationContext evaluate(Object document, Object rootDocument, Configuration configuration, boolean forUpdate) {
EvaluationContext evaluationContext = null;
return resultByConfiguration(document, configuration, evaluationContext);
}
/**
* Applies this JsonPath to the provided json document.
* Note that the document must be identified as either a List or Map by
@ -165,46 +183,8 @@ public class JsonPath {
*/
@SuppressWarnings("unchecked")
public <T> T read(Object jsonObject, Configuration configuration) {
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (path.isFunctionPath()) {
if (optAsPathList || optAlwaysReturnList) {
if (optSuppressExceptions) {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!");
}
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
return evaluationContext.getValue(true);
} else if (optAsPathList) {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
return (T) configuration.jsonProvider().createArray();
}
return (T) evaluationContext.getPath();
} else {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
if (optSuppressExceptions && evaluationContext.getPathList().isEmpty()) {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
Object res = evaluationContext.getValue(false);
if (optAlwaysReturnList && path.isDefinite()) {
Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T) array;
} else {
return (T) res;
}
}
PathEvaluator evaluator = new PathEvaluator(path, configuration);
return evaluator.evaluate(jsonObject);
}
/**
@ -218,7 +198,7 @@ public class JsonPath {
public <T> T set(Object jsonObject, Object newVal, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
if (evaluationContext.getPathList().isEmpty()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (optSuppressExceptions) {
@ -246,7 +226,7 @@ public class JsonPath {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
notNull(mapFunction, "mapFunction can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
if (evaluationContext.getPathList().isEmpty()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (optSuppressExceptions) {
@ -273,7 +253,7 @@ public class JsonPath {
public <T> T delete(Object jsonObject, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
if (evaluationContext.getPathList().isEmpty()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (optSuppressExceptions) {
@ -300,7 +280,7 @@ public class JsonPath {
public <T> T add(Object jsonObject, Object value, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
if (evaluationContext.getPathList().isEmpty()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (optSuppressExceptions) {
@ -329,7 +309,7 @@ public class JsonPath {
notNull(jsonObject, "json can not be null");
notEmpty(key, "key can not be null or empty");
notNull(configuration, "configuration can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
if (evaluationContext.getPathList().isEmpty()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
if (optSuppressExceptions) {
@ -348,7 +328,7 @@ public class JsonPath {
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);
EvaluationContext evaluationContext = evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
@ -554,17 +534,17 @@ public class JsonPath {
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param jsonURL url pointing to json doc
// * @param jsonURL url pointing to json doc
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
// @SuppressWarnings({"unchecked"})
@Deprecated
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters);
}
// public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
// return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters);
// }
/**
* Creates a new JsonPath and applies it to the provided Json object

35
json-path/src/main/java/com/jayway/jsonpath/PathEvaluator.java

@ -0,0 +1,35 @@
package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import static com.jayway.jsonpath.internal.Utils.*;
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
public class PathEvaluator {
private final Path path;
private final Configuration configuration;
public PathEvaluator(Path path, Configuration configuration) {
notNull(path, "path can not be null");
notNull(configuration, "configuration can not be null");
this.path = path;
this.configuration = configuration;
}
public <T> T evaluate(Object jsonObject) {
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration);
return resultByConfiguration(jsonObject, evaluationContext);
}
private <T> T resultByConfiguration(Object jsonObject, EvaluationContext evaluationContext) {
if(configuration.containsOption(AS_PATH_LIST)){
return (T)evaluationContext.getPathList();
} else {
return (T) jsonObject;
}
}
}

35
json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java

@ -375,27 +375,28 @@ public class EvaluatorFactory {
private static class NoneOfEvaluator implements Evaluator {
@Override
public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) {
ValueListNode rightValueListNode;
if (right.isJsonNode()) {
ValueNode vn = right.asJsonNode().asValueListNode(ctx);
ValueListNode rightValueListNode = extractValueListNode(right, ctx);
ValueListNode leftValueListNode = extractValueListNode(left, ctx);
return areAllElementsDifferent(leftValueListNode, rightValueListNode);
}
private ValueListNode extractValueListNode(ValueNode node, Predicate.PredicateContext ctx) {
if (node.isJsonNode()) {
ValueNode vn = node.asJsonNode().asValueListNode(ctx);
if (vn.isUndefinedNode()) {
return false;
return null; // or handle undefined case accordingly
} else {
rightValueListNode = vn.asValueListNode();
return vn.asValueListNode();
}
} else {
rightValueListNode = right.asValueListNode();
return node.asValueListNode();
}
ValueListNode leftValueListNode;
if (left.isJsonNode()) {
ValueNode vn = left.asJsonNode().asValueListNode(ctx);
if (vn.isUndefinedNode()) {
return false;
} else {
leftValueListNode = vn.asValueListNode();
}
} else {
leftValueListNode = left.asValueListNode();
}
private boolean areAllElementsDifferent(ValueListNode leftValueListNode, ValueListNode rightValueListNode) {
if (leftValueListNode == null || rightValueListNode == null) {
return false; // or handle the case where one of the lists is null
}
for (ValueNode leftValueNode : leftValueListNode) {
@ -405,7 +406,9 @@ public class EvaluatorFactory {
}
}
}
return true;
}
}
}

34
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -135,26 +135,44 @@ public abstract class ValueNode {
}
private static boolean isJson(Object o) {
if(o == null || !(o instanceof String)){
if (o == null || !(o instanceof String)) {
return false;
}
String str = o.toString().trim();
if (str.length() <= 1) {
return false;
}
char c0 = str.charAt(0);
char c1 = str.charAt(str.length() - 1);
if ((c0 == '[' && c1 == ']') || (c0 == '{' && c1 == '}')){
try {
new JSONParser(JSONParser.MODE_PERMISSIVE).parse(str);
return true;
} catch(Exception e){
return false;
}
if (isJsonArray(c0, c1) || isJsonObject(c0, c1)) {
return isValidJson(str);
}
return false;
}
private static boolean isJsonArray(char c0, char c1) {
return c0 == '[' && c1 == ']';
}
private static boolean isJsonObject(char c0, char c1) {
return c0 == '{' && c1 == '}';
}
private static boolean isValidJson(String str) {
try {
new JSONParser(JSONParser.MODE_PERMISSIVE).parse(str);
return true;
} catch (Exception e) {
return false;
}
}
//----------------------------------------------------

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

@ -13,7 +13,7 @@ import java.util.List;
public class PassthruPathFunction implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
return model;
}
}

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

@ -32,5 +32,5 @@ public interface PathFunction {
* @param parameters
* @return result
*/
Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters);
Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters);
}

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

@ -16,7 +16,7 @@ import java.util.List;
*/
public class Append implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
JsonProvider jsonProvider = ctx.configuration().jsonProvider();
if (parameters != null && parameters.size() > 0) {
for (Parameter param : parameters) {

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

@ -14,7 +14,7 @@ import java.util.List;
public class KeySetFunction implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if (ctx.configuration().jsonProvider().isMap(model)) {
return ctx.configuration().jsonProvider().getPropertyKeys(model);
}

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

@ -23,7 +23,7 @@ public abstract class AbstractAggregation implements PathFunction {
* @param value
* The numerical value to process next
*/
protected abstract void next(Number value);
protected abstract void processNumericValue(Number value);
/**
* Obtains the value generated via the series of next value calls
@ -31,31 +31,37 @@ public abstract class AbstractAggregation implements PathFunction {
* @return
* A numerical answer based on the input value provided
*/
protected abstract Number getValue();
protected abstract Number getAggregatedValue();
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
int count = 0;
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
for (Object obj : objects) {
if (obj instanceof Number) {
Number value = (Number) obj;
if (ctx.configuration().jsonProvider().isArray(model)) {
Iterable<?> arrayElements = ctx.configuration().jsonProvider().toIterable(model);
for (Object arrayElement : arrayElements) {
if (arrayElement instanceof Number) {
Number numericValue = (Number) arrayElement;
count++;
next(value);
processNumericValue(numericValue);
}
}
}
if (parameters != null) {
for (Number value : Parameter.toList(Number.class, ctx, parameters)) {
for (Number parameterValue : Parameter.toList(Number.class, ctx, parameters)) {
count++;
next(value);
processNumericValue(parameterValue);
}
}
if (count != 0) {
return getValue();
return getAggregatedValue();
}
throw new JsonPathException("Aggregation function attempted to calculate value using empty array");
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Average.java

@ -11,13 +11,13 @@ public class Average extends AbstractAggregation {
private Double count = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
count++;
summation += value.doubleValue();
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
if (count != 0d) {
return summation / count;
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Max.java

@ -9,14 +9,14 @@ public class Max extends AbstractAggregation {
private Double max = Double.MIN_VALUE;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
if (max < value.doubleValue()) {
max = value.doubleValue();
}
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return max;
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Min.java

@ -9,14 +9,14 @@ public class Min extends AbstractAggregation {
private Double min = Double.MAX_VALUE;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
if (min > value.doubleValue()) {
min = value.doubleValue();
}
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return min;
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/StandardDeviation.java

@ -11,14 +11,14 @@ public class StandardDeviation extends AbstractAggregation {
private Double count = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
sum += value.doubleValue();
sumSq += value.doubleValue() * value.doubleValue();
count++;
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return Math.sqrt(sumSq/count - sum*sum/count/count);
}
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/Sum.java

@ -9,12 +9,12 @@ public class Sum extends AbstractAggregation {
private Double summation = 0d;
@Override
protected void next(Number value) {
protected void processNumericValue(Number value) {
summation += value.doubleValue();
}
@Override
protected Number getValue() {
protected Number getAggregatedValue() {
return summation;
}
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java

@ -19,7 +19,7 @@ public abstract class AbstractSequenceAggregation implements PathFunction {
protected abstract int targetIndex(EvaluationContext ctx, List<Parameter> parameters);
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);

2
json-path/src/main/java/com/jayway/jsonpath/internal/function/text/Concatenate.java

@ -14,7 +14,7 @@ import java.util.List;
*/
public class Concatenate implements PathFunction {
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
StringBuilder result = new StringBuilder();
if(ctx.configuration().jsonProvider().isArray(model)){
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);

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

@ -40,7 +40,7 @@ public class Length implements PathFunction {
* @return
*/
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
public Object aggregateAndInvoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
if (null != parameters && parameters.size() > 0) {
// Set the tail of the first parameter, when its not a function path parameter (which wouldn't make sense

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

@ -38,7 +38,7 @@ public class FunctionPathToken extends PathToken {
public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
PathFunction pathFunction = PathFunctionFactory.newFunction(functionName);
evaluateParameters(currentPath, parent, model, ctx);
Object result = pathFunction.invoke(currentPath, parent, model, ctx, functionParams);
Object result = pathFunction.aggregateAndInvoke(currentPath, parent, model, ctx, functionParams);
ctx.addResult(currentPath + "." + functionName, parent, result);
cleanWildcardPathToken();
if (!isLeaf()) {

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

@ -215,7 +215,7 @@ public abstract class PathToken {
}
public void invoke(PathFunction pathFunction, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
ctx.addResult(currentPath, parent, pathFunction.invoke(currentPath, parent, model, ctx, null));
ctx.addResult(currentPath, parent, pathFunction.aggregateAndInvoke(currentPath, parent, model, ctx, null));
}
public abstract void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx);

32
json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java

@ -212,18 +212,42 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
@Override
public int length(Object obj) {
JsonLengthCalculator lengthCalculator = getLengthCalculator(obj);
return lengthCalculator.calculateLength(obj);
}
private JsonLengthCalculator getLengthCalculator(Object obj) {
if (isArray(obj)) {
return toJsonArray(obj).size();
return new JsonArrayLengthCalculator();
} else if (isMap(obj)) {
return toJsonObject(obj).size();
return new JsonObjectLengthCalculator();
} else {
return new TextNodeLengthCalculator();
}
}
public class JsonArrayLengthCalculator implements JsonLengthCalculator {
@Override
public int calculateLength(Object obj) {
return toJsonArray(obj).size();
}
}
public class JsonObjectLengthCalculator implements JsonLengthCalculator {
@Override
public int calculateLength(Object obj) {
return toJsonObject(obj).size();
}
}
public class TextNodeLengthCalculator implements JsonLengthCalculator {
@Override
public int calculateLength(Object obj) {
if (obj instanceof TextNode) {
TextNode element = (TextNode) obj;
return element.size();
}
throw new JsonPathException("length operation cannot be applied to " + (obj != null ? obj.getClass().getName() : "null"));
}
throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName()
: "null"));
}
@Override

5
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonLengthCalculator.java

@ -0,0 +1,5 @@
package com.jayway.jsonpath.spi.json;
public interface JsonLengthCalculator {
int calculateLength(Object obj);
}

34
json-path/src/test/java/com/jayway/jsonpath/internal/JettisonProviderTest.java

@ -0,0 +1,34 @@
//package com.jayway.jsonpath.internal;
//
//import com.jayway.jsonpath.spi.json.JettisonProvider;
//import org.junit.Before;
//import org.junit.Test;
//
//import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
//
//public class JettisonProviderTest {
//
// private JettisonProvider jsonProvider;
//
// @Before
// public void setUp() {
// jsonProvider = new JettisonProvider();
// }
//
// @Test
// public void parseValidJsonString() {
// //Input json
// String jsonString = "{\"name\":\"Vishesh\",\"age\":21}";
//
// // When parsing a valid JSON string
// Object result = jsonProvider.parse(jsonString);
//
// assertTrue(jsonProvider.isMap(result));
//
// // expected values
// assertEquals("Vishesh", jsonProvider.getMapValue(result, "name"));
// assertEquals(21, jsonProvider.getMapValue(result, "age"));
// }
//
//}

46
json-path/src/test/java/com/jayway/jsonpath/internal/JsonOrgJsonProviderTest.java

@ -0,0 +1,46 @@
//package com.jayway.jsonpath.internal;
//
//import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
//import org.junit.Before;
//import org.junit.Test;
//
//import java.io.ByteArrayInputStream;
//import java.io.InputStream;
//import java.nio.charset.StandardCharsets;
//
//import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
//
//public class JsonOrgJsonProviderTest {
// private JsonOrgJsonProvider jsonProvider;
// @Before
// public void setUp() {
// jsonProvider = new JsonOrgJsonProvider();
// }
// @Test
// public void parseValidJsonString() {
// //input json
// String jsonString = "{\"name\":\"Raj\",\"age\":31}";
//
// // When parsing a valid JSON string
// Object result = jsonProvider.parse(jsonString);
//
// assertTrue(jsonProvider.isMap(result));
// assertEquals("Raj", jsonProvider.getMapValue(result, "name"));
// assertEquals(31, jsonProvider.getMapValue(result, "age"));
// }
// @Test
// public void parseValidJsonInputStream() {
//
// String jsonString = "{\"name\":\"Raj\",\"age\":31}";
// InputStream jsonStream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
//
// // When parsing valid JSON from an InputStream
// Object result = jsonProvider.parse(jsonStream, StandardCharsets.UTF_8.name());
//
// assertTrue(jsonProvider.isMap(result));
// // expected values
// assertEquals("Raj", jsonProvider.getMapValue(result, "name"));
// assertEquals(31, jsonProvider.getMapValue(result, "age"));
// }
//}

42
json-path/src/test/java/com/jayway/jsonpath/internal/TapestryMappingProviderTest.java

@ -0,0 +1,42 @@
//package com.jayway.jsonpath.internal;
//
//import com.jayway.jsonpath.Configuration;
//import com.jayway.jsonpath.TypeRef;
//import com.jayway.jsonpath.spi.mapper.TapestryMappingProvider;
//import org.junit.Before;
//import org.junit.Test;
//
//import java.util.List;
//
//import static org.junit.Assert.assertNull;
//import static org.junit.Assert.fail;
//
//public class TapestryMappingProviderTest {
// private TapestryMappingProvider mappingProvider;
// private Configuration configuration;
// @Before
// public void setUp() {
// mappingProvider = new TapestryMappingProvider();
// configuration = Configuration.defaultConfiguration();
// }
// @Test
// public void mapNullSourceToNullTarget() {
// Object source = null;
//
// Object result = mappingProvider.map(source, List.class, configuration);
//
// //result should be null
// assertNull(result);
// }
// @Test
// public void mapArrayToJsonRef() {
// String[] source = new String[]{"Red", "Green", "Blue"};
//
// try {
// Object result = mappingProvider.map(source, new TypeRef<List<String>>() {}, configuration);
// fail("Expected UnsupportedOperationException");
// } catch (UnsupportedOperationException e) {
// // UnsupportedOperationException should be thrown
// }
// }
//}
Loading…
Cancel
Save