Resolving¶
- class jsonlogic.resolving.ParsedReference(reference: 'str', segments: 'list[str]')¶
- exception jsonlogic.resolving.Unresolvable(parsed_reference: ParsedReference, /)¶
A reference is unresolvable.
- class jsonlogic.resolving.ReferenceParser(*args, **kwargs)¶
A callback protocol used to parse a string reference.
- class jsonlogic.resolving.BaseReferenceParser(*args, **kwargs)¶
An abstract reference parser that supports specifying a scope in the reference.
A scope specifier is an optional string appended to the end of the reference. It consists of an
@sign followed by a positive number (possibly null), indicating the index of the data stack to use before resolving the reference against the corresponding data. If not provided, a scope value of0is implied (the current data).This scope specifier allows referencing variables higher up in the stack, e.g. when using operators such as map or reduce.
The actual segment parsing logic is implemented by subclasses.
- class jsonlogic.resolving.DotReferenceParser(*args, **kwargs)¶
A reference parser able to parse references with a dot-notation.
This implements the reference format of the original JsonLogic project.
Caution
Using the dot-notation reference format can be confusing when dots are present in actual data keys. For example, with the reference
"path.to"and the following data:{ "path": {"to": 1}, "path.to": 2 }
The resolved data can be unexpected. For this reason, it is recommended to use the
PointerReferenceParserwhich provides an escape mechanism.
- class jsonlogic.resolving.PointerReferenceParser(*args, **kwargs)¶
A reference parser able to parse JSON Pointer references, as specified by RFC 6901.
- jsonlogic.resolving.resolve_json_schema(parsed_reference: ParsedReference, schema: dict[str, Any]) dict[str, Any]¶
Resolve the (sub) JSON Schema by looking up by the parsed reference.
- Parameters:
- Returns:
The resolved (sub) JSON Schema.
Example
ref = PointerReferenceParser()("/path/123") schema = { "type": "object", "properties": { "path": { "type": "array", "items": { "type": "string", }, }, }, } assert resolve_json_schema(ref, schema) == {"type": "string"}
- jsonlogic.resolving.resolve_data(parsed_reference: ParsedReference, data: JSON) JSON¶
Resolve the data by looking up by the parsed reference.
- Parameters:
- Returns:
The resolved data.
Example
ref = PointerReferenceParser()("/path/0") data = { "path": ["some_value"] } assert resolve_data(ref, data) == "some_value"