|
| 1 | +Wire format |
| 2 | +=========== |
| 3 | + |
| 4 | +queuebridge uses a **JSON-first** wire format with optional **type tags** for values that lose type information in JSON. |
| 5 | + |
| 6 | +The ``__qb__`` envelope |
| 7 | +----------------------- |
| 8 | + |
| 9 | +Non-primitive values that need type information are wrapped like this: |
| 10 | + |
| 11 | +.. code-block:: json |
| 12 | +
|
| 13 | + { |
| 14 | + "__qb__": { |
| 15 | + "t": "myapp.models.OrderCreate", |
| 16 | + "v": 1, |
| 17 | + "d": {"id": 1, "sku": "ABC"} |
| 18 | + } |
| 19 | + } |
| 20 | +
|
| 21 | +.. list-table:: |
| 22 | + :header-rows: 1 |
| 23 | + |
| 24 | + * - Field |
| 25 | + - Meaning |
| 26 | + * - ``t`` |
| 27 | + - Fully-qualified type name (module + class) |
| 28 | + * - ``v`` |
| 29 | + - Wire format version (always ``1`` for now) |
| 30 | + * - ``d`` |
| 31 | + - JSON-safe payload |
| 32 | + |
| 33 | +Constants (Python) |
| 34 | +------------------ |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + from queuebridge.types import QB_TAG, QB_VERSION |
| 39 | +
|
| 40 | + QB_TAG == "__qb__" |
| 41 | + QB_VERSION == 1 |
| 42 | +
|
| 43 | +Types and their wire representation |
| 44 | +----------------------------------- |
| 45 | + |
| 46 | +.. list-table:: |
| 47 | + :header-rows: 1 |
| 48 | + :widths: 25 35 40 |
| 49 | + |
| 50 | + * - Python type |
| 51 | + - On the wire |
| 52 | + - Decoded via |
| 53 | + * - ``BaseModel`` |
| 54 | + - ``__qb__`` envelope, ``d`` = ``model_dump(mode="json")`` |
| 55 | + - ``model_validate`` or FQN import |
| 56 | + * - ``UUID`` |
| 57 | + - ``__qb__`` with ``t: uuid.UUID``, ``d: "<uuid string>"`` |
| 58 | + - ``UUID(d)`` |
| 59 | + * - ``datetime``, ``date``, ``time`` |
| 60 | + - ``__qb__`` with ISO string in ``d`` |
| 61 | + - ``fromisoformat`` |
| 62 | + * - ``Decimal`` |
| 63 | + - ``__qb__`` with string in ``d`` |
| 64 | + - ``Decimal(d)`` |
| 65 | + * - ``Enum`` |
| 66 | + - ``__qb__`` with enum FQN and value |
| 67 | + - Enum class lookup |
| 68 | + * - ``list``, ``tuple``, ``set`` |
| 69 | + - JSON list (elements encoded recursively) |
| 70 | + - Hint-driven (``list[T]``, etc.) |
| 71 | + * - ``dict`` |
| 72 | + - JSON object (keys coerced to str) |
| 73 | + - Hint-driven |
| 74 | + * - ``str``, ``int``, ``float``, ``bool``, ``null`` |
| 75 | + - Pass through unchanged |
| 76 | + - Pass through |
| 77 | + |
| 78 | +Encode and decode API |
| 79 | +--------------------- |
| 80 | + |
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + from queuebridge import encode, decode |
| 84 | +
|
| 85 | + wire = encode(my_model) |
| 86 | + restored = decode(wire, OrderCreate) |
| 87 | +
|
| 88 | +``decode_wire()`` unwraps all ``__qb__`` envelopes recursively without hints. Dramatiq uses this on incoming messages. |
| 89 | + |
| 90 | +Celery note |
| 91 | +----------- |
| 92 | + |
| 93 | +The Celery serializer uses ``encode(..., tag_models=False)`` for compatibility with ``pydantic=True``. Models become plain dicts on the wire; UUID/datetime inside ``model_dump(mode="json")`` are already strings. |
| 94 | + |
| 95 | +Arq note |
| 96 | +-------- |
| 97 | + |
| 98 | +Job dicts are msgpack-encoded **after** ``encode()``, so the binary blob contains queuebridge-tagged JSON-compatible structures. |
0 commit comments