Skip to content

smithy-json

JSONCodec

Bases: Codec

A codec for converting shapes to/from JSON.

Source code in packages/smithy-json/src/smithy_json/__init__.py
class JSONCodec(Codec):
    """A codec for converting shapes to/from JSON."""

    def __init__(
        self,
        use_json_name: bool = True,
        use_timestamp_format: bool = True,
        default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME,
        default_namespace: str | None = None,
        document_class: type[JSONDocument] = JSONDocument,
    ) -> None:
        """Initializes a JSONCodec.

        :param use_json_name: Whether the codec should use `smithy.api#jsonName` trait,
            if present.
        :param use_timestamp_format: Whether the codec should use the
            `smithy.api#timestampFormat` trait, if present.
        :param default_timestamp_format: The default timestamp format to use if the
            `smithy.api#timestampFormat` trait is not enabled or not present.
        :param default_namespace: The default namespace to use when determining a
            document's discriminator.
        :param document_class: The document class to deserialize to.
        """
        self._settings = JSONSettings(
            use_json_name=use_json_name,
            use_timestamp_format=use_timestamp_format,
            default_timestamp_format=default_timestamp_format,
            default_namespace=default_namespace,
            document_class=document_class,
        )

    @property
    def media_type(self) -> str:
        return "application/json"

    def create_serializer(self, sink: BytesWriter) -> "ShapeSerializer":
        return _JSONShapeSerializer(sink, settings=self._settings)

    def create_deserializer(self, source: bytes | BytesReader) -> "ShapeDeserializer":
        if isinstance(source, bytes):
            source = BytesIO(source)
        return _JSONShapeDeserializer(source, settings=self._settings)

__init__(use_json_name=True, use_timestamp_format=True, default_timestamp_format=TimestampFormat.DATE_TIME, default_namespace=None, document_class=JSONDocument)

Initializes a JSONCodec.

:param use_json_name: Whether the codec should use smithy.api#jsonName trait, if present. :param use_timestamp_format: Whether the codec should use the smithy.api#timestampFormat trait, if present. :param default_timestamp_format: The default timestamp format to use if the smithy.api#timestampFormat trait is not enabled or not present. :param default_namespace: The default namespace to use when determining a document's discriminator. :param document_class: The document class to deserialize to.

Source code in packages/smithy-json/src/smithy_json/__init__.py
def __init__(
    self,
    use_json_name: bool = True,
    use_timestamp_format: bool = True,
    default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME,
    default_namespace: str | None = None,
    document_class: type[JSONDocument] = JSONDocument,
) -> None:
    """Initializes a JSONCodec.

    :param use_json_name: Whether the codec should use `smithy.api#jsonName` trait,
        if present.
    :param use_timestamp_format: Whether the codec should use the
        `smithy.api#timestampFormat` trait, if present.
    :param default_timestamp_format: The default timestamp format to use if the
        `smithy.api#timestampFormat` trait is not enabled or not present.
    :param default_namespace: The default namespace to use when determining a
        document's discriminator.
    :param document_class: The document class to deserialize to.
    """
    self._settings = JSONSettings(
        use_json_name=use_json_name,
        use_timestamp_format=use_timestamp_format,
        default_timestamp_format=default_timestamp_format,
        default_namespace=default_namespace,
        document_class=document_class,
    )

JSONSettings dataclass

Settings for the JSON codec.

Source code in packages/smithy-json/src/smithy_json/settings.py
@dataclass(slots=True)
class JSONSettings:
    """Settings for the JSON codec."""

    document_class: type["JSONDocument"]
    """The document class to deserialize to."""

    use_json_name: bool = True
    """Whether the codec should use `smithy.api#jsonName` trait, if present."""

    use_timestamp_format: bool = True
    """Whether the codec should use the `smithy.api#timestampFormat` trait, if
    present."""

    default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME
    """The default timestamp format to use if the `smithy.api#timestampFormat` trait is
    not enabled or not present."""

    default_namespace: str | None = None
    """The default namespace to use when determining a document's discriminator."""

default_namespace = None class-attribute instance-attribute

The default namespace to use when determining a document's discriminator.

default_timestamp_format = TimestampFormat.DATE_TIME class-attribute instance-attribute

The default timestamp format to use if the smithy.api#timestampFormat trait is not enabled or not present.

document_class instance-attribute

The document class to deserialize to.

use_json_name = True class-attribute instance-attribute

Whether the codec should use smithy.api#jsonName trait, if present.

use_timestamp_format = True class-attribute instance-attribute

Whether the codec should use the smithy.api#timestampFormat trait, if present.

settings

JSONSettings dataclass

Settings for the JSON codec.

Source code in packages/smithy-json/src/smithy_json/settings.py
@dataclass(slots=True)
class JSONSettings:
    """Settings for the JSON codec."""

    document_class: type["JSONDocument"]
    """The document class to deserialize to."""

    use_json_name: bool = True
    """Whether the codec should use `smithy.api#jsonName` trait, if present."""

    use_timestamp_format: bool = True
    """Whether the codec should use the `smithy.api#timestampFormat` trait, if
    present."""

    default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME
    """The default timestamp format to use if the `smithy.api#timestampFormat` trait is
    not enabled or not present."""

    default_namespace: str | None = None
    """The default namespace to use when determining a document's discriminator."""

default_namespace = None class-attribute instance-attribute

The default namespace to use when determining a document's discriminator.

default_timestamp_format = TimestampFormat.DATE_TIME class-attribute instance-attribute

The default timestamp format to use if the smithy.api#timestampFormat trait is not enabled or not present.

document_class instance-attribute

The document class to deserialize to.

use_json_name = True class-attribute instance-attribute

Whether the codec should use smithy.api#jsonName trait, if present.

use_timestamp_format = True class-attribute instance-attribute

Whether the codec should use the smithy.api#timestampFormat trait, if present.