Package com.api.jsonata4java
Class Expression
- java.lang.Object
-
- com.api.jsonata4java.Expression
-
- All Implemented Interfaces:
Serializable
public class Expression extends Object implements Serializable
Class to provide embedding and extending JSONata features- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Expression(String expression)Constructor for Expression
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidassign(Binding binding)Assign the binding to the environment preparing for evaluationvoidassign(String varname, String expression)Assign the expression (variable or function declaration) to the variable name suppliedvoidclear()Remove all bindingsstatic List<Binding>createBindings(com.fasterxml.jackson.databind.JsonNode bindingObj)Establish a list of bindings for the given json objectcom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext)Generate a result form the Expression's parsed expression and variable assignments or registered functionscom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functionscom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings objectcom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings objectcom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindingscom.fasterxml.jackson.databind.JsonNodeevaluate(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindingscom.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext)Generate a result form the Expression's parsed expression and variable assignments or registered functions.com.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functions.com.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings objectcom.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings object.com.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings.com.fasterxml.jackson.databind.JsonNodeevaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings, long timeoutMS, int maxDepth)Generate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings.static Expressionjsonata(String expression)Generate a new Expression based on evaluating the supplied expressionstatic voidmain(String[] args)Testing the various methods based on https://docs.jsonata.org/embedding-extending#expressionregisterfunctionname-implementation-signaturevoidregisterFunction(String fctName, String implementation)Registers a function implementation (declaration) by name
-
-
-
Constructor Detail
-
Expression
public Expression(String expression) throws ParseException, IOException
Constructor for Expression- Parameters:
expression- the logic to be parsed for later execution via evaluate methods- Throws:
ParseExceptionIOException
-
-
Method Detail
-
createBindings
public static List<Binding> createBindings(com.fasterxml.jackson.databind.JsonNode bindingObj) throws ParseException
Establish a list of bindings for the given json object- Parameters:
bindingObj- the json to calculate the bindings for- Returns:
- the list of bindings for the provided json
- Throws:
ParseException
-
jsonata
public static Expression jsonata(String expression) throws ParseException, IOException
Generate a new Expression based on evaluating the supplied expression- Parameters:
expression- the logic to be parsed for later execution via the evaluate methods- Returns:
- new Expression object
- Throws:
ParseExceptionIOException
-
main
public static void main(String[] args)
Testing the various methods based on https://docs.jsonata.org/embedding-extending#expressionregisterfunctionname-implementation-signature- Parameters:
args- not used
-
assign
public void assign(Binding binding)
Assign the binding to the environment preparing for evaluation- Parameters:
binding-
-
assign
public void assign(String varname, String expression) throws ParseException, IOException
Assign the expression (variable or function declaration) to the variable name supplied- Parameters:
varname- name of the variable to map to a variable expression or function declaration expressionexpression- logic to be assigned to the variable name- Throws:
ParseExceptionIOException
-
clear
public void clear()
Remove all bindings
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext) throws ParseExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions- Parameters:
rootContext- JSON object specifying the content used to evaluate the expression- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseException
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj) throws ParseException, IOExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings object- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindingObj- a JSON object containing the assignments of variable names to variable expressions or function declarations- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the bindings object
- Throws:
ParseExceptionIOException
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj, long timeoutMS, int maxDepth) throws ParseException, IOException, EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings object- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindingObj- a JSON object containing the assignments of variable names to variable expressions or function declarationstimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the bindings object
- Throws:
ParseExceptionIOExceptionEvaluateException
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings) throws ParseExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindings- assignments of variable names to variable expressions or function declarations- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseException
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings, long timeoutMS, int maxDepth) throws ParseException, EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindings- assignments of variable names to variable expressions or function declarationstimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseExceptionEvaluateException
-
evaluate
public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth) throws EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressiontimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the JsonNode resulting from the expression evaluation against the rootContext. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
EvaluateException- If the given device event is invalid.
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext) throws ParseExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions. This method is synchronized to enable multi-threaded execution.- Parameters:
rootContext- JSON object specifying the content used to evaluate the expression- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseException
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj) throws ParseException, IOExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings object- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindingObj- a JSON object containing the assignments of variable names to variable expressions or function declarations- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the bindings object
- Throws:
ParseExceptionIOException
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, com.fasterxml.jackson.databind.JsonNode bindingObj, long timeoutMS, int maxDepth) throws ParseException, IOException, EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the bindings object. This method is synchronized to enable multi-threaded execution.- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindingObj- a JSON object containing the assignments of variable names to variable expressions or function declarationstimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the bindings object
- Throws:
ParseExceptionIOExceptionEvaluateException
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings) throws ParseExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. This method is synchronized to enable multi-threaded execution.- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindings- assignments of variable names to variable expressions or function declarations- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseException
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings, long timeoutMS, int maxDepth) throws ParseException, EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. This method is synchronized to enable multi-threaded execution.- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressionbindings- assignments of variable names to variable expressions or function declarationstimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the result from executing the Expression's parsed expression and variable assignments or registered functions specified in the list of bindings. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
ParseExceptionEvaluateException
-
evaluateSynced
public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth) throws EvaluateExceptionGenerate a result form the Expression's parsed expression and variable assignments or registered functions. This method is synchronized to enable multi-threaded execution.- Parameters:
rootContext- JSON object specifying the content used to evaluate the expressiontimeoutMS- milliseconds allowed for the evaluation to occur. If it takes longer an exception is thrown. Must be positive number or exception is thrown.maxDepth- the maximum call stack depth allowed before an exception is thrown. Must be a positive number or an exception is thrown.- Returns:
- the JsonNode resulting from the expression evaluation against the rootContext. A null will be returned if no match is found (note a JSON null will result in a JsonNode of type NullNode).
- Throws:
EvaluateException- If the given device event is invalid.
-
registerFunction
public void registerFunction(String fctName, String implementation) throws ParseException, IOException
Registers a function implementation (declaration) by name- Parameters:
fctName- the name of the functionimplementation- the function declaration- Throws:
ParseExceptionIOException
-
-