ezmsg.dashboard.backend.json_encoding#

Wire encoding for values JSON cannot represent natively.

JSON has no literal for inf, -inf or nan, and the two transports the dashboard uses disagree about what to do with them: JSONResponse renders with allow_nan=False and raises, while pydantic’s model_dump(mode="json") silently rewrites them to null. Settings classes are free to hold such values (clip_max: float = np.inf is perfectly reasonable), so the dashboard encodes them as the string tokens below on the way out and decodes them again on the way in.

The tokens are spelled the way JavaScript spells them, so the frontend can recover the value with Number(token).

Functions

decode_float_token(value)[source]#

Return the float a token stands for, or None if it is not a token.

Return type:

float | None

Parameters:

value (Any)

encode_float(value)[source]#

Return value unchanged, or its token if it is not finite.

Return type:

float | str

Parameters:

value (float)

is_non_finite_token(value)[source]#
Return type:

bool

Parameters:

value (Any)

sanitize_json_value(value)[source]#

Recursively encode non-finite floats anywhere in an already-JSON-safe payload.

Applied to whole responses as a backstop; payloads built by adapters are already encoded.

Return type:

Any

Parameters:

value (Any)