Skip to content

Class swarmauri_signing_dsse.codec_json.DSSEJsonCodec

swarmauri_signing_dsse.codec_json.DSSEJsonCodec

Encode and decode DSSE envelopes using the standard JSON format.

encode

encode(envelope)

Serialize a DSSE envelope into canonical JSON bytes.

Source code in swarmauri_signing_dsse/codec_json.py
14
15
16
17
18
19
20
def encode(self, envelope: DSSEEnvelope) -> bytes:
    """Serialize a DSSE envelope into canonical JSON bytes."""

    payload = envelope.to_mapping()
    return json.dumps(payload, separators=(",", ":"), sort_keys=True).encode(
        "utf-8"
    )

decode

decode(blob)

Deserialize JSON bytes or mappings into a :class:DSSEEnvelope.

Source code in swarmauri_signing_dsse/codec_json.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def decode(
    self, blob: bytes | bytearray | str | Mapping[str, Any] | DSSEEnvelope
) -> DSSEEnvelope:
    """Deserialize JSON bytes or mappings into a :class:`DSSEEnvelope`."""

    if isinstance(blob, DSSEEnvelope):
        return blob
    if isinstance(blob, Mapping):
        return DSSEEnvelope.from_mapping(dict(blob))
    if isinstance(blob, (bytes, bytearray)):
        data = json.loads(blob)
    elif isinstance(blob, str):
        data = json.loads(blob)
    else:
        raise TypeError("Unsupported DSSE envelope representation for decoding")
    if not isinstance(data, Mapping):
        raise TypeError("Decoded DSSE envelope must produce a mapping")
    return DSSEEnvelope.from_mapping(data)