Evaluation¶
- class jsonlogic.evaluation.EvaluationContext(root_data: JSON, data_schema: dict[str, Any] | None = None, settings: EvaluationSettingsDict | None = None)¶
A context object used when evaluating operators.
When evaluating an
Operator, an instance of this class should be used.>>> expr = JSONLogicExpression.from_json({"var": "/a_date"}) >>> root_op = expr.as_operator_tree(operator_registry) >>> context = EvaluationContext( ... data={"a_date": "1970-01-01"}, ... data_schema={ ... "type": "object", ... "properties": { ... "a_date": {"type": "string", "format": "date"}, ... }, ... }, ... ) >>> root_op.evaluate(context) datetime.date(1970, 1, 1)
- Parameters:
root_data¶ – The root data available during evaluation.
data_schema¶ – The matching JSON Schema describing the root data. This should be the same JSON Schema used during typechecking (see
root_data_schema).settings¶ – Settings to be used when evaluating an
Operator. SeeEvaluationSettingsfor the available settings and default values.
- resolve_variable(reference: str, *, bare: Literal[True]) JSON¶
- resolve_variable(reference: str, *, bare: Literal[False] = False) Any
Resolve a variable given the string reference pointing to it.
The format of the reference should match the reference parser defined in the
EvaluationSettings.- Parameters:
reference¶ – The string reference of the variable.
bare¶ – Whether the resolved value should be casted to a specific Python type according to the matching JSON Schema. Note that this will only be possible if a
data_schemawas provided.
- class jsonlogic.evaluation.EvaluationSettings(reference_parser: ~jsonlogic.resolving.ReferenceParser = <factory>, variable_casts: dict[str, ~collections.abc.Callable[[str], ~typing.Any]] = <factory>, literal_casts: list[~collections.abc.Callable[[str], ~typing.Any]] = <factory>)¶
Settings used when evaluating an
Operator.- reference_parser: ReferenceParser¶
A reference parser instance to use when resolving variables.
Default:
PointerReferenceParser.
- variable_casts: dict[str, Callable[[str], Any]]¶
A mapping between JSON Schema formats and their corresponding conversion callable.
When an operator reads variables from the provided data (such as the
"var"operator), such variables of typestrmay be converted to a specific Python type if the corresponding JSON Schema of the data was provided during evaluation.This setting is analogous to the
variable_castsconfiguration of theTypecheckSettingsclass.Default:
{"date": date.fromisoformat, "date-time": datetime.fromisoformat}.
- literal_casts: list[Callable[[str], Any]]¶
A list of conversion callables to try when encountering a literal string value during evaluation.
When a literal string value is encountered in a JSON Logic expression, it might be beneficial to convert it to a specific Python type.
This setting is analogous to the
literal_castsconfiguration of theTypecheckSettingsclass.Default:
[](no cast).Warning
The order in which the conversion callables are defined matters. Each callable will be applied one after the other until no exception is raised.
- jsonlogic.evaluation.evaluate(operator: Operator, data: JSON, data_schema: dict[str, Any] | None, settings: EvaluationSettingsDict | None = None) Any¶
Helper function to evaluate an
Operator.- Parameters:
operator¶ – The operator to evaluate.
data¶ – The root data available during evaluation.
data_schema¶ – The matching JSON Schema describing the root data. This should be the same JSON Schema used during typechecking (see
root_data_schema).settings¶ – Settings to be used when evaluating an
Operator. SeeEvaluationSettingsfor the available settings and default values.
- Returns:
The evaluated value.
- jsonlogic.evaluation.get_value(obj: OperatorArgument, context: EvaluationContext) Any¶
Get the value of an operator argument.
- Parameters:
obj¶ – the object to evaluate. If this is an
Operator, it is evaluated and the value is returned. Otherwise, it must be aJSONLogicPrimitive, and the type is inferred from the actual value according to theliteral_castssetting.context¶ – The typecheck context.