Browse Source

Merge 22118a2a97 into 45333e0a31

pull/785/merge
James Jervis 1 year ago committed by GitHub
parent
commit
d14d9f4262
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      README.md
  2. 80
      json-path/src/main/java/com/jayway/jsonpath/PathFunctionFactory.java
  3. 18
      json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java
  4. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java

37
README.md

@ -486,8 +486,45 @@ CacheProvider.setCache(new Cache() {
});
```
### Custom Path Functions
If you want to implement you own path functions, create your PathFunction implementation.
Initializing PathFunctionFactory should only be done when your application is being initialized.
```java
class MyPathFunction implements PathFunction {
public static final String NAME = "mypathfunc";
@Override
public Object invoke(
String currentPath,
PathRef parent,
Object model,
EvaluationContext ctx,
List<Parameter> parameters
) {
// Do something here
return model;
}
}
// Example application initialization
class ApplicationBoot {
public static void main(String[] args) {
PathFunctionFactory.add(
MyPathFunction.NAME,
MyPathFunction.class
);
PathFunctionFactory.init();
}
}
```

80
json-path/src/main/java/com/jayway/jsonpath/PathFunctionFactory.java

@ -0,0 +1,80 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.function.PathFunction;
import java.util.Collections;
import java.util.Map;
public class PathFunctionFactory {
private static boolean initialized = false;
/**
* by default, we add th common path functions
*/
private static Map<String, Class> functions = com.jayway.jsonpath.internal.function.PathFunctionFactory.getFunctions();
/**
* Add a single custom path function
* Should only be done when your application is being initialized
*
* @param name The name of the function
* @param clazz Class of a function
* @throws InvalidPathException
*/
public static void add(String name, Class clazz) throws InvalidPathException {
assertNotInitialized();
if (functions.containsKey(name)) {
throw new InvalidPathException("Path function with name: " + name + " already exists");
}
functions.put(name, clazz);
}
/**
* Add a map custom path function
* Should only be done when your application is being initialized
*
* @param functionMap The name of the function as key, Class of a function as Value
* @throws InvalidPathException
*/
public static void add(Map<String, Class> functionMap) throws InvalidPathException {
for (Map.Entry<String, Class> entry : functionMap.entrySet()) {
add(entry.getKey(), entry.getValue());
}
}
/**
* Should only your application initialized to
*/
public static void init() {
if (initialized) {
return;
}
functions.putAll(com.jayway.jsonpath.internal.function.PathFunctionFactory.getFunctions());
functions = Collections.unmodifiableMap(functions);
initialized = true;
}
public static PathFunction newFunction(String name) throws InvalidPathException {
// Lazy init
init();
Class functionClazz = functions.get(name);
if (functionClazz == null) {
throw new InvalidPathException("Function with name: " + name + " does not exist.");
} else {
try {
return (PathFunction) functionClazz.newInstance();
} catch (Exception e) {
throw new InvalidPathException("Function of name: " + name + " cannot be created", e);
}
}
}
private static void assertNotInitialized() {
if (initialized) {
throw new InvalidPathException("Can not change path function factory after it is initialized");
}
}
}

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

@ -32,6 +32,20 @@ public class PathFunctionFactory {
static {
// New functions should be added here and ensure the name is not overridden
Map<String, Class> map = getFunctions();
FUNCTIONS = Collections.unmodifiableMap(map);
}
/**
* Returns the common/core functions.
*
* @see #FUNCTIONS
* @see PathFunction
*
* @return Map of path functions keyed by name
*/
public static Map<String, Class> getFunctions() {
Map<String, Class> map = new HashMap<String, Class>();
// Math Functions
@ -55,11 +69,11 @@ public class PathFunctionFactory {
map.put("last", Last.class);
map.put("index", Index.class);
FUNCTIONS = Collections.unmodifiableMap(map);
return map;
}
/**
* @deprecated
* Returns the function by name or throws InvalidPathException if function not found.
*
* @see #FUNCTIONS

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

@ -4,7 +4,7 @@ import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;
import com.jayway.jsonpath.internal.function.PathFunctionFactory;
import com.jayway.jsonpath.PathFunctionFactory;
import com.jayway.jsonpath.internal.function.latebinding.JsonLateBindingValue;
import com.jayway.jsonpath.internal.function.latebinding.PathLateBindingValue;

Loading…
Cancel
Save