Browse Source

PathFunctions must exist when compiled. 'size' alias for 'length'.

pull/158/head
Kalle Stenflo 9 years ago
parent
commit
86ea0ac062
  1. 19
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java
  2. 6
      json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java
  3. 5
      json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java

19
json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java

@ -37,13 +37,13 @@ public class PathFunctionFactory {
// JSON Entity Functions
map.put("length", Length.class);
map.put("size", Length.class);
FUNCTIONS = Collections.unmodifiableMap(map);
}
/**
* Either provides a pass thru function when the function cannot be properly mapped or otherwise returns the function
* implementation based on the name using the internal FUNCTION map
* Returns the function by name or throws InvalidPathException if function not found.
*
* @see #FUNCTIONS
* @see PathFunction
@ -57,18 +57,15 @@ public class PathFunctionFactory {
* @throws InvalidPathException
*/
public static PathFunction newFunction(String name) throws InvalidPathException {
PathFunction result = new PassthruPathFunction();
if (null != name && FUNCTIONS.containsKey(name) && PathFunction.class.isAssignableFrom(FUNCTIONS.get(name))) {
Class functionClazz = FUNCTIONS.get(name);
if(functionClazz == null){
throw new InvalidPathException("Function with name: " + name + " does not exists.");
} else {
try {
result = (PathFunction)FUNCTIONS.get(name).newInstance();
} catch (InstantiationException e) {
throw new InvalidPathException("Function of name: " + name + " cannot be created", e);
} catch (IllegalAccessException e) {
return (PathFunction)functionClazz.newInstance();
} catch (Exception e) {
throw new InvalidPathException("Function of name: " + name + " cannot be created", e);
}
}
return result;
}
}

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

@ -298,4 +298,10 @@ public class DeepScanTest extends BaseTest {
assertThat(using(JSON_SMART_CONFIGURATION.addOptions(Option.REQUIRE_PROPERTIES)).parse(animals).read("$..[?(@.mammal == true)].color", List.class)).containsExactly(brown, white);
}
@Test
public void scan_with_a_function_filter() {
List result = JsonPath.parse(JSON_DOCUMENT).read("$..*[?(@.length() > 5)]");
assertThat(result).hasSize(1);
}
}

5
json-path/src/test/java/com/jayway/jsonpath/internal/function/JSONEntityPathFunctionTest.java

@ -49,17 +49,22 @@ public class JSONEntityPathFunctionTest extends BaseFunctionTest {
" }\n" +
"}";
private Configuration conf = Configurations.JSON_SMART_CONFIGURATION;
@Test
public void testLengthOfTextArray() {
// The length of JSONArray is an integer
verifyFunction(conf, "$['text'].length()", TEXT_SERIES, 6);
verifyFunction(conf, "$['text'].size()", TEXT_SERIES, 6);
}
@Test
public void testLengthOfNumberArray() {
// The length of JSONArray is an integer
verifyFunction(conf, "$.numbers.length()", NUMBER_SERIES, 10);
verifyFunction(conf, "$.numbers.size()", NUMBER_SERIES, 10);
}

Loading…
Cancel
Save