Skip to content

[Request] Add a CBOR codec to smithy-python, for wire parity with smithy-java #724

Description

@humanzz

Ask

Add a CBOR codec to smithy-python, mirroring the existing JSON codec (JSONCodec in smithy-json) and wire-compatible with smithy-java's Rpcv2CborCodec. The JSON half already exists in both languages; this is the missing CBOR half on the Python side, so shapes generated from one Smithy model can be serde'd in both Java and Python and stay wire-compatible.

Use case: one Smithy model, codegen'd into Java and Python (SageMaker endpoint invocation)

SageMaker endpoint invocation crosses a language boundary: the model server is Python, and the clients calling it are Java and Python. The goal is to model the request/response shapes once in Smithy, codegen typed classes into both languages, and serde them with matching codecs — CBOR on the wire (compact, binary, schema-driven), JSON when readability matters. The requirement is wire compatibility: bytes written by the Java codec have to deserialize with the Python codec, and vice versa.

Java today: smithy-java

A generated shape round-trips through CBOR or JSON by swapping the codec — serde is schema-driven, nothing hand-written:

import software.amazon.smithy.java.cbor.Rpcv2CborCodec;
import software.amazon.smithy.java.json.JsonCodec;
import software.amazon.smithy.java.core.serde.Codec;
import java.nio.ByteBuffer;

// `request` is a code-generated smithy-java shape (a SerializableShape).

// --- CBOR (RPC v2 CBOR) ---
Codec cbor = Rpcv2CborCodec.builder().build();
ByteBuffer encoded = cbor.serialize(request);
MyShape decoded = cbor.deserializeShape(encoded, MyShape.builder());

// --- JSON (same shapes, same API) ---
Codec json = JsonCodec.builder().build();
ByteBuffer encodedJson = json.serialize(request);
MyShape decodedJson = json.deserializeShape(encodedJson, MyShape.builder());

Python today (smithy-python): JSON only

JSONCodec implements the format-agnostic Codec protocol from smithy_core.codecs:

from smithy_json import JSONCodec

codec = JSONCodec()
encoded: bytes = codec.serialize(request)      # `request` is a code-generated shape
decoded = codec.deserialize(encoded, MyShape)

Python already has the generic Codec protocol (smithy-core) and concrete JSON (smithy-json) and XML (smithy-xml) codecs — but no CBOR codec (no smithy-cbor package).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions