intellicrack.core.json_payload
Type narrowing for decoded JSON payloads.
Every value that arrives from json.loads(), an HTTP response body or a raw JSON Schema is statically an object. A bare
isinstance(value, dict) narrows it to dict[Unknown, Unknown], because the check proves nothing about the key or value types, and
that partial unknown then propagates through every expression downstream.
JSON itself does carry that guarantee: an object always has string keys, and its members are always JSON values. The predicates here state
that guarantee once, as typing.TypeIs, so a caller narrows straight to a usable type in both the positive and the negative branch
and nothing downstream is unknown.
- JsonObject
A decoded JSON object.
Keys are always strings; values are JSON values.
- as_json_object(value)[source]
Return a decoded JSON value as an object, or
Noneif it is not one.- Parameters:
value (object) – The value to convert.
- Returns:
valuenarrowed to a JSON object, orNonewhen it is any other kind of JSON value.- Return type:
JsonObject | None
- copy_json(value)[source]
Copy a JSON value, however deeply it nests.
copy.deepcopy()recurses once per level and fails past the interpreter’s recursion limit; this walk is iterative.
- is_json_object(value)[source]
Check whether a decoded JSON value is an object.
- Parameters:
value (object) – The value to test, typically straight out of a decoded payload.
- Returns:
True when
valueis a mapping, narrowing it toJsonObjectfor the caller.- Return type:
TypeIs[JsonObject]
- json_array_at(container, key)[source]
Read one key of a JSON object, requiring the member to be an array.
- json_equality_key(value)[source]
Build the text under which JSON Schema equality becomes string equality.
JSON Schema compares numbers by value, so
1and1.0are equal, but keeps booleans apart from numbers, sotrueand1are not; arrays compare in order and objects without regard to member order. Python’s own==gets the booleans wrong, and neither lists nor dicts can be hashed. The key is canonical JSON text – members sorted by name, integral numbers written as integers – so equal keys mean equal JSON values, and since a string hashes, comparingnvalues for duplicates takesnsteps rather thannsquared. Both the walk and the key are flat, so no nesting depth is too deep.
- json_object_at(container, key)[source]
Read one key of a JSON object, requiring the member to be an object.
- Parameters:
- Returns:
The member narrowed to a JSON object, or
Nonewhen the key is absent or the member is another kind of value.- Return type:
JsonObject | None
- json_str_at(container, key)[source]
Read one key of a JSON object, requiring the member to be a string.