diff --git a/README.md b/README.md index 1982856b..ac56d643 100644 --- a/README.md +++ b/README.md @@ -506,8 +506,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 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(); + } +} + +``` diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java index 2fcd3a33..4c7334c5 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/function/PathFunctionFactory.java @@ -29,6 +29,20 @@ public class PathFunctionFactory { static { // New functions should be added here and ensure the name is not overridden + Map 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 getFunctions() { Map map = new HashMap(); // Math Functions @@ -47,11 +61,11 @@ public class PathFunctionFactory { map.put("append", Append.class); map.put("keys", KeySetFunction.class); - - FUNCTIONS = Collections.unmodifiableMap(map); + return map; } /** + * @deprecated * Returns the function by name or throws InvalidPathException if function not found. * * @see #FUNCTIONS diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java index 2d24b70d..afda71c6 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java @@ -3,7 +3,7 @@ package 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;