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:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Expression(String expression)
    Constructor for Expression
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    assign(Binding binding)
    Assign the binding to the environment preparing for evaluation
    void
    assign(String varname, String expression)
    Assign the expression (variable or function declaration) to the variable name supplied
    void
    Remove all bindings
    static List<Binding>
    createBindings(com.fasterxml.jackson.databind.JsonNode bindingObj)
    Establish a list of bindings for the given json object
    com.fasterxml.jackson.databind.JsonNode
    evaluate(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.JsonNode
    evaluate(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.JsonNode
    evaluate(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 object
    com.fasterxml.jackson.databind.JsonNode
    evaluate(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.JsonNode
    evaluate(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.JsonNode
    evaluate(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
    com.fasterxml.jackson.databind.JsonNode
    evaluateSynced(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.JsonNode
    evaluateSynced(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.JsonNode
    evaluateSynced(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 object
    com.fasterxml.jackson.databind.JsonNode
    evaluateSynced(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.JsonNode
    evaluateSynced(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.JsonNode
    evaluateSynced(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 Expression
    jsonata(String expression)
    Generate a new Expression based on evaluating the supplied expression
    static void
    main(String[] args)
    Testing the various methods based on https://docs.jsonata.org/embedding-extending#expressionregisterfunctionname-implementation-signature
    void
    registerFunction(String fctName, String implementation)
    Registers a function implementation (declaration) by name

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • 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:
      ParseException
      IOException
    • 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 expression
      expression - logic to be assigned to the variable name
      Throws:
      ParseException
      IOException
    • clear

      public void clear()
      Remove all bindings
    • evaluate

      public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext) throws ParseException
      Generate 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, IOException
      Generate 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 expression
      bindingObj - 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:
      ParseException
      IOException
    • 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, EvaluateException
      Generate 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 expression
      bindingObj - a JSON object containing the assignments of variable names to variable expressions or function declarations
      timeoutMS - 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:
      ParseException
      IOException
      EvaluateException
    • evaluate

      public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings) throws ParseException
      Generate 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 expression
      bindings - 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, EvaluateException
      Generate 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 expression
      bindings - assignments of variable names to variable expressions or function declarations
      timeoutMS - 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:
      ParseException
      EvaluateException
    • evaluate

      public com.fasterxml.jackson.databind.JsonNode evaluate(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth) throws EvaluateException
      Generate 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
      timeoutMS - 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 ParseException
      Generate 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, IOException
      Generate 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 expression
      bindingObj - 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:
      ParseException
      IOException
    • 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, EvaluateException
      Generate 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 expression
      bindingObj - a JSON object containing the assignments of variable names to variable expressions or function declarations
      timeoutMS - 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:
      ParseException
      IOException
      EvaluateException
    • evaluateSynced

      public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, List<Binding> bindings) throws ParseException
      Generate 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 expression
      bindings - 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, EvaluateException
      Generate 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 expression
      bindings - assignments of variable names to variable expressions or function declarations
      timeoutMS - 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:
      ParseException
      EvaluateException
    • evaluateSynced

      public com.fasterxml.jackson.databind.JsonNode evaluateSynced(com.fasterxml.jackson.databind.JsonNode rootContext, long timeoutMS, int maxDepth) throws EvaluateException
      Generate 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
      timeoutMS - 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 function
      implementation - the function declaration
      Throws:
      ParseException
      IOException