# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """Serialize Huey messages as JSON without reconstructing arbitrary Python objects.""" import json import uuid from datetime import datetime from typing import Any from huey.registry import Message from huey.serializer import Serializer from huey.utils import Error, SKIPPED class JSONSerializer(Serializer): """A deliberately restricted, non-pickle serializer for our Huey tasks. JSON has no native UUID, datetime, tuple or Huey message representation. Each non-primitive value is encoded with a fixed tag and decoded using an allowlist; no module imports, user-supplied classes or object constructors are evaluated. Unsupported objects fail closed rather than being pickled. """ def _serialize(self, data: Any) -> bytes: return json.dumps( self._encode(data), ensure_ascii=False, allow_nan=False, separators=(",", ":"), ).encode("utf-8") def _deserialize(self, data: bytes) -> Any: def reject_nonfinite(value: str) -> None: raise ValueError(f"Invalid non-finite JSON value: {value}") return self._decode(json.loads(data, parse_constant=reject_nonfinite)) @classmethod def _encode(cls, value: Any) -> Any: if value is None or type(value) in {str, int, float, bool}: return value if isinstance(value, uuid.UUID): return {"type": "uuid", "value": str(value)} if isinstance(value, datetime): return {"type": "datetime", "value": value.isoformat()} if isinstance(value, Message): return {"type": "message", "value": [cls._encode(v) for v in value]} if isinstance(value, Error): return {"type": "error", "value": cls._encode(value.metadata)} if value is SKIPPED: return {"type": "skipped", "value": None} if isinstance(value, tuple): return {"type": "tuple", "value": [cls._encode(v) for v in value]} if isinstance(value, list): return {"type": "list", "value": [cls._encode(v) for v in value]} if isinstance(value, dict) and all(type(k) is str for k in value): return { "type": "dict", "value": [[k, cls._encode(v)] for k, v in value.items()], } raise TypeError(f"Unsupported Huey JSON value: {type(value).__name__}") @classmethod def _decode(cls, value: Any) -> Any: if value is None or type(value) in {str, int, float, bool}: return value if not isinstance(value, dict) or set(value) != {"type", "value"}: raise ValueError("Invalid Huey JSON data") kind, payload = value["type"], value["value"] if kind == "uuid" and isinstance(payload, str): return uuid.UUID(payload) if kind == "datetime" and isinstance(payload, str): return datetime.fromisoformat(payload) if kind == "message" and isinstance(payload, list): if len(payload) != len(Message._fields): raise ValueError("Invalid Huey JSON message field count") return Message(*(cls._decode(v) for v in payload)) if kind == "error": metadata = cls._decode(payload) if not isinstance(metadata, dict): raise ValueError("Invalid Huey JSON error metadata") return Error(metadata) if kind == "skipped" and payload is None: return SKIPPED if kind in {"tuple", "list"} and isinstance(payload, list): items = [cls._decode(v) for v in payload] return tuple(items) if kind == "tuple" else items if kind == "dict" and isinstance(payload, list): result: dict[str, Any] = {} for pair in payload: if ( not isinstance(pair, list) or len(pair) != 2 or type(pair[0]) is not str ): raise ValueError("Invalid Huey JSON dictionary entry") if pair[0] in result: raise ValueError("Duplicate Huey JSON dictionary key") result[pair[0]] = cls._decode(pair[1]) return result raise ValueError("Unsupported Huey JSON type")