Metadata-Version: 2.5
Name: jsonology
Version: 0.1.0
Summary: Evaluate a strict, tiny JSON DSL (comparisons, if, and/or/not) against a $-rooted context object
Author: The jsonology authors
License: MIT
License-File: LICENSE
Keywords: dsl,evaluator,expression,json,jsonlogic,rules
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: hypothesis>=6.100; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# jsonology

Evaluate a strict, tiny JSON DSL against a context object.

```python
>>> from jsonology import evaluate
>>> evaluate({"==": ["$.operation", "edit"]}, {"operation": "edit"})
True
>>> evaluate({"if": [{"==": ["$.operation", "edit"]}, "an edit!", "not an edit"]},
...         {"operation": "delete"})
'not an edit'
```

Expressions are JSON values, authored by trusted teammates, stored in rules or
config. `jsonology` interprets them strictly and errors loudly. Zero runtime
dependencies. Python ≥ 3.10, fully typed.

## Install

```bash
pip install jsonology
```

## The language

An **expression** is one of:

- a JSON scalar — a literal (`5`, `5.5`, `"edit"`, `true`, `false`, `null`)
- a string starting with `$.` — a path into the context
- an operator object `{op: [args...]}` — where every argument is itself an expression

### Operators

| Operator | Arity | Semantics |
| --- | --- | --- |
| `==`, `!=` | exactly 2 | strict equality, no coercion |
| `<`, `<=`, `>`, `>=` | exactly 2 | ordering: two numbers or two strings only |
| `if` | exactly 3 | `[condition, if_true, if_false]`, **lazy** |
| `and`, `or` | 1 or more | short-circuit, return strict booleans |
| `not` | exactly 1 | negation of truthiness |

Operator objects must have exactly one key. Bare lists are not expressions.
Unknown operators are rejected with an error naming them.

### Paths

- `$.user.address.city` — dot-nested object access
- `$.items.0` — numeric segments index lists (no negative indices)
- a missing path evaluates to `null`, so `{"==": ["$.nickname", null]}` is the "is unset" idiom
- `$` alone is the entire context (which may itself be a list)

### Truthiness

`if` conditions and `and`/`or`/`not` operands are interpreted Python-style:

| Value | Truthy? |
| --- | --- |
| `false`, `null` | no |
| `0` | no |
| `""` | no |
| `[]`, `{}` | no |
| everything else | yes |

### Strict typing

- No coercion: `{"==": ["5", 5]}` is `false`.
- `int` and `float` unify: `5 == 5.0` is `true`.
- `bool` is its own type: `true == 1` is `false`, and ordering booleans is an error.
- Cross-type equality is always decided, never an error: `{"==": [1, "1"]}` → `false`.
- Ordering is only defined for two numbers or two strings; anything else —
  cross-type, `null` on either side, booleans, lists — raises `EvaluationError`.
  Note `{"<": ["$.missing", 5]}` raises, because a missing path is `null`.
- Strings order lexicographically: `{"<": ["10", "9"]}` is `true`.
- Equality is deep and structural over arrays and objects.

### Laziness and short-circuiting

`if` evaluates only the taken branch. `and` stops at the first falsy operand,
`or` at the first truthy one — later operands are never evaluated (or errored):

```python
evaluate({"and": [False, {"<": [None, 1]}]}, {})   # False — the broken branch never runs
```

Because of this laziness, syntax errors inside dead branches stay silent —
`evaluate({"if": [True, 1, {"frobnicate": [1]}]}, {})` is `1`.

## API

```python
evaluate(expression, data) -> Any
```

- `expression`: an already-parsed Python object **or** a raw JSON string.
  A string that is `"$"` or starts with `"$."` is treated as a path, not JSON
  text; any other string is parsed as JSON and failures raise
  `ExpressionSyntaxError`. There is **no escape hatch** for literal strings
  that start with `$.` — they are always paths.
- `data`: the context object (a mapping, or a list if your paths start `$.0` …).
- Pure, stateless, thread-safe: neither `expression` nor `data` is mutated.

Two exceptions, and only two:

| Exception | Raised for |
| --- | --- |
| `ExpressionSyntaxError` | malformed DSL: structure, arity, unknown operator, invalid JSON text, nesting too deep |
| `EvaluationError` | undefined operation during evaluation (ordering rules above) |

Error messages name the offending operator and locate the failing
subexpression within the expression tree, e.g. `$.if[0].==[1]` — "argument 1
of the `==` at argument 0 of the `if`".

```python
>>> from jsonology import evaluate, ExpressionSyntaxError
>>> try:
...     evaluate('{"if": [{"==": ["$.a", {"x": 1, "y": 2}]}, 1, 2]}', {})
... except ExpressionSyntaxError as e:
...     print(e)
operator object must have exactly one key, got 2: {"x": 1, "y": 2} (at $.if[0].==[1])
```

## Limitations (deliberate, v1)

- No literal escape hatch for strings starting with `$.`.
- No array or object literals: every operand is a scalar, a path, or a nested
  operator. `{"==": ["$.tags", ["a", "b"]]}` is a syntax error (compare two
  paths instead: `{"==": ["$.tags", "$.want"]}`), and an object literal like
  `{"x": 1}` in operand position fails as an unknown operator `'x'`.
- Dead `if` branches and short-circuited `and`/`or` operands are never
  evaluated, so their syntax is not validated.
- Context keys containing dots cannot be addressed by paths.
- The contract is JSON types. Non-JSON Python values in `data` (datetime,
  Decimal, …) are out of contract — same-type pairs fall through to Python
  semantics, everything else is unspecified. For time comparisons store
  ISO-8601 strings (lexicographic `<` does the right thing) or epoch integers.
- No custom operators, no depth or size limits (trusted-input model).
- No `compile()` step — expressions are interpreted directly; reach for one
  only if profiling demands it.

## Development

```bash
uv venv && uv pip install -e '.[dev]'
python -m pytest          # 149 cases: table-driven goldens + property-based invariants
python -m ruff check .
python -m mypy
```

The property suite (Hypothesis) checks the contract as invariants: only the
two documented exceptions ever escape, `==`/ordering obey the algebraic laws
(reflexivity, symmetry, transitivity, connexity), truthiness matches the
reference table, path resolution matches an independent reference walk, dead
branches never evaluate, inputs are never mutated, and JSON-text input agrees
with object input.

## License

MIT — see [LICENSE](LICENSE).
