Crate serde_dynamo

source ·
Expand description

DynamoDB is an AWS database that stores key/value and document data.

serde_dynamo provides a way to serialize and deserialize between data stored in these items and strongly-typed Rust data structures.

§The full power of serde

serde_dynamo supports the full power of serde.

Most uses of DynamoDB will involve simple structs mapping keys to values in type-safe ways.

#[derive(Serialize, Deserialize)]
pub struct UserId(String);

#[derive(Serialize, Deserialize)]
pub struct User {
    id: UserId,
    name: String,
    age: u8,
    #[serde(with = "serde_bytes")]
    public_key: Vec<u8>

More advanced usage – including flattening, adjacently tagged enums, and untagged enums – is fully supported.

#[derive(Serialize, Deserialize)]
struct Message {
    id: String,
    message_type: MessageType,
    sent: DateTime<Utc>,

/// What type of message this is.
/// By the power of Rust enums and serde serializating, we can guarantee that we _either_ have
/// an email with all of its required fields, _or_ an SMS with all of its required fields.
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "message_type", content = "message_payload")]
enum MessageType {

#[derive(Serialize, Deserialize)]
struct Email {
    email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    subject: String,
    body: String,

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
struct Sms {
    phone_number: String,
    body: String,

let input = r#"
    "id": "HWCqBFBG2Gl4",
    "message_type": "sms",
    "message_payload": {
        "phone_number": "5551234567",
        "body": "Good morning!"
    "sent": "1985-04-21T11:12:13Z"
let message: Message = serde_json::from_str(input)?;
let item = to_item(message)?;

§aws-sdk support

serde_dynamo works well with aws-sdk-dynamodb.

Add the following to your dependencies.

serde_dynamo = { version = "4", features = ["aws-sdk-dynamodb+1"] }

See aws_sdk_dynamodb_1 for examples and more information. See aws_sdk_dynamodbstreams_1 for DynamoDb streams support.

§aws_lambda_events support

aws_lambda_events, starting with version 0.8, uses serde_dynamo directly, so no feature flags need to be enabled.

serde_dynamo = { version = "4" }

§rusoto support

serde_dynamo works well with rusoto_dynamodb.

Add the following to your dependencies.

serde_dynamo = { version = "4", features = ["rusoto_dynamodb+0.48"] }

See rusoto_dynamodb_0_48 for examples and more information.


DynamoDB’s items share strong similarities with JSON, and it is very common to store JSON data in DynamoDB either directly or as a subfield.

To support this, serde_dynamo supports serializing JSON just like any other Rust data structure.

#[derive(Serialize, Deserialize)]
struct IncludesJson {
    id: String,
    data: serde_json::Value,

In addition, serde_dynamo also maps strongly-typed data structures nearly identically as serde_json. This means that, in almost all cases, serializing to JSON first and then to an DynamoDb item will result in the exact same representation as serializing directly to a DynamoDb item. (The caveat here is for byte data, which loses fidelity because JSON doesn’t support byte data natively, but DynamoDB does.)

let user = User {
    name: "Arthur Dent".to_string(),
    age: 42,

// Serialize directly from the data structure to an item
let direct_item = to_item(user.clone())?;

// Serialize indirectly through JSON
let json = serde_json::to_value(user.clone())?;
let indirect_item = to_item(json)?;

// The result should be the same!
assert_eq!(direct_item, indirect_item);


serde_dynamo is a stable library ready to use in production. Because of that, it’s major version is above 1.0.

This creates problems when supporting dynamodb libraries that have version numbers less than 1.0.

To avoid doing a major version bump for every release of aws-sdk-dynamodb and aws_lambda_events, serde_dynamo uses features to opt into the correct version of the dynamodb library.

See the modules section for all possible features. Feature names are largely guessable: the library name, a plus, and the library version (with underscores instead of dots, because doesn’t support feature names with dots). For example, support for aws-sdk-dynamodb version 0.13 is enabled with the feature aws-sdk-dynamodb+0_13.

§Converting to and from DynamoDB JSON

In most cases, libraries already exist to handle the raw DynamoDB JSON and convert it into an item. For example, aws-sdk-dynamodb deals with the raw JSON if you’re making API calls, and aws_lambda_events deals with the raw JSON if you’re writing lambdas that react on DynamoDB change streams.

However, in very rare cases, you may need to convert the DynamoDB JSON yourself. In those cases, both Item and AttributeValue implement serde::Serialize and serde::Deserialize.

let input = r#"{
        "S":"Book 103 Title"

let item: Item = serde_json::from_str(input)
    .expect("expected to deserialize DynamoDB JSON format");




  • A structure that deserializes AttributeValues into Rust values.
  • This type represents all possible errors that can occur when serializing or deserializing DynamoDB data.
  • An item that comes from DynamoDb.
  • Multiple items that come from DynamoDb.
  • A structure for serializing Rust values into AttributeValues.



Type Aliases§

  • Alias for a Result with the error type serde_dynamo::Error