Core

Base structures of the library. The two classes defined, Operator and JSONLogicExpression, can be extended to provide extra functionality.

class jsonlogic.core.Operator(operator: str)

The base class for all operators.

operator: str

The string representation of the operator.

abstractmethod classmethod from_expression(operator: str, arguments: list[TypeAliasForwardRef('OperatorArgument')]) Self

Return an instance of the operator from the list of provided arguments.

Parameters:
  • operator – The ID of the operator, as provided by the OperatorRegistry.

  • arguments – The list of the arguments for this operator. Subclasses are responsible for checking the correct number of arguments and optionally the types.

abstractmethod evaluate(context: EvaluationContext) Any

Evaluate the operator with the provided data.

typecheck(context: TypecheckContext) JSONSchemaType

Typecheck the operator (and all children) given the data schema.

exception jsonlogic.core.JSONLogicSyntaxError(message: str, /)

A syntax error when building an operator tree from a JSONLogicExpression.

class jsonlogic.core.JSONLogicExpression(expression: NormalizedExpression)

A parsed and normalized JSON Logic expression.

The underlying structure of an expression is a single item dictionary, mapping the operator key to a list of arguments.

All JSON Logic expressions should be instantiated using the from_json() constructor:

expr = JSONLogicExpression.from_json({"op": ...})
classmethod from_json(json: JSONObject) Self

Build a JSON Logic expression from JSON data.

Operator arguments are recursively normalized to a list:

expr = JSONLogicExpression.from_json({"var": "varname"})
assert expr.expression == {"var": ["varname"]}
as_operator_tree(operator_registry: OperatorRegistry) Operator

Return a recursive tree of operators, using the provided registry as a reference.

Parameters:

operator_registry – The registry to use to resolve operator IDs.

Returns:

An Operator instance.