Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the possibility to add custom path functions #1015

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.function.json.Append;
import com.jayway.jsonpath.internal.function.json.KeySetFunction;
import com.jayway.jsonpath.internal.function.numeric.Average;
Expand All @@ -14,6 +15,7 @@
import com.jayway.jsonpath.internal.function.text.Concatenate;
import com.jayway.jsonpath.internal.function.text.Length;

import java.io.InvalidClassException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -28,7 +30,7 @@
*/
public class PathFunctionFactory {

public static final Map<String, Class> FUNCTIONS;
private static final Map<String, Class> FUNCTIONS;

static {
// New functions should be added here and ensure the name is not overridden
Expand Down Expand Up @@ -56,7 +58,7 @@ public class PathFunctionFactory {
map.put("index", Index.class);


FUNCTIONS = Collections.unmodifiableMap(map);
FUNCTIONS = map;
}

/**
Expand Down Expand Up @@ -85,4 +87,11 @@ public static PathFunction newFunction(String name) throws InvalidPathException
}
}
}

public static void addCustomFunction(String name,Class function) throws InvalidClassException {
if (!PathFunction.class.isAssignableFrom(function)){
throw new InvalidClassException("Function with name: " + name + "must be a instance of PathFunction class");
}
FUNCTIONS.put(name,function);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Configurations;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import org.junit.jupiter.api.Test;

import java.io.InvalidClassException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CustomFunctionTest extends BaseFunctionTest {

private Configuration conf = Configurations.JSON_SMART_CONFIGURATION;

public class InvalidFunction {

}
@Test
void testAddInvalidFunction(){
assertThrows(InvalidClassException.class, () -> PathFunctionFactory.addCustomFunction("invalid",
InvalidFunction.class));
}

@Test
void testAddValidFunction(){
assertDoesNotThrow(()->PathFunctionFactory.addCustomFunction("toUpperCase",ToUpperCase.class));
verifyTextFunction(conf,"$['text'][0].toUpperCase()","A");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;

import java.util.List;

public class ToUpperCase implements PathFunction{
@Override
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx,
List<Parameter> parameters) {
return ((String) model).toUpperCase();
}
}