Skip to content

tech-debt: standardize JSON library usage (json vs ujson) across the SDK #1165

Description

@dgarros

Need

The SDK uses two JSON libraries interchangeably with no documented rule for
which to use when. Across infrahub_sdk/, ~16 modules use ujson (the apparent
house standard) and ~7 use stdlib json; playback.py and utils.py import
both. This makes the codebase harder to reason about and creates a latent
exception-handling hazard: ujson raises its own ujson.JSONDecodeError and
stdlib raises json.JSONDecodeError, and the two do not catch each other. Today
every decode/except pair happens to be internally consistent, but nothing
enforces that — a future ujson.loads guarded by except json.JSONDecodeError
(or the reverse) would silently fail to catch a malformed-input error.

Context

Concrete smells found while surveying usage:

  • playback.py serializes with json.dumps (:52) but reads the recorded
    files back with ujson.load (:57). This works today (both speak valid JSON)
    but the asymmetry is a smell and imports both libraries in one module.
  • utils.py also imports both json and ujson.
  • The decode/except pairs are currently consistent everywhere
    (e.g. pytest_plugin items pair ujson.loads with except ujson.JSONDecodeError;
    infrahub_filters.py and parsers.py pair stdlib json.loads with
    except json.JSONDecodeError), so this is a maintainability/consistency
    concern rather than an active bug.

Note: an earlier framing suspected utils.py:97 (decode_json) of catching the
wrong exception for a ujson failure — that turned out to be a false alarm. That
line decodes via httpx's response.json() (stdlib-backed), so its
except json.decoder.JSONDecodeError is correct.

Affects: Python SDK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowThis issue should be completed in a couple of hourstype/tech-debtItem we know we need to improve way it is implemented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions