Skip to content

aws_smithy_types::Document should have an implementation of the serde::Serializer trait #4642

@MinisculeGirraffe

Description

@MinisculeGirraffe

aws_smithy_types::Document already has #[derive(Serialize, Deserialize)] behind a feature flag, but that only lets you serialize the Document itself. it doesn't let you convert an arbitrary struct to a Document directly.

#[cfg(any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
))]
use serde;

I'd like to be able to do:

  let doc = aws_smithy_types::document::to_document(&my_struct)?;
  let my_struct: MyStruct = aws_smithy_types::document::from_document(doc)?;

Basically the same thing serde_json does with serde_json::to_value / serde_json::from_value, but targeting Document instead of Value.

This would also allow for interoperability between the type types without client facing code.

Right now the workaround i'm using is to round-trip through serde_json::Value and then manually convert:

  let json_value = serde_json::to_value(&my_struct)?;
  let doc = json_to_document(json_value); // hand-rolled conversion function

This works but it's clunky and allocates an intermediate representation for no reason.

It would be a significantly more ergonomic client experience to provide a Serializer implementation analogous to serde_json to allow for conversion from arbitrary types directly.
https://github.com/serde-rs/json/blob/master/src/value/ser.rs#L58

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions