Coverage for src/hypra/_json.py: 100.00%
13 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
1"""Strict JSON loading shared by the CLI and both backends."""
3import json
4from typing import Any
7def strict_json_loads(text: str) -> Any:
8 """Reject duplicate object keys and JSON's nonstandard nonfinite constants."""
10 def unique_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
11 result: dict[str, Any] = {}
12 for key, value in pairs:
13 if key in result:
14 raise ValueError(f"Duplicate JSON key: {key}")
15 result[key] = value
16 return result
18 def reject_constant(value: str) -> Any:
19 raise ValueError(f"Nonfinite JSON constant: {value}")
21 return json.loads(text, object_pairs_hook=unique_pairs, parse_constant=reject_constant)