#dynamo-db #serialize-deserialize #serde-json #aws-sdk #marshall #unmarshall

dynamodb_marshall

Serialize and deserialize AttributeValue to Value

26 releases (14 stable)

1.48.0 Oct 2, 2024
1.42.0 Aug 19, 2024
1.37.0 Jul 6, 2024
1.19.0 Mar 21, 2024
0.28.0 May 25, 2023

#5 in #dynamodb

Download history 13/week @ 2024-07-29 9/week @ 2024-08-05 5/week @ 2024-08-12 164/week @ 2024-08-19 9/week @ 2024-08-26 23/week @ 2024-09-02 209/week @ 2024-09-09 109/week @ 2024-09-16 354/week @ 2024-09-23 335/week @ 2024-09-30 54/week @ 2024-10-07 45/week @ 2024-10-14 19/week @ 2024-10-21 73/week @ 2024-10-28 69/week @ 2024-11-04 6/week @ 2024-11-11

173 downloads per month

MIT/Apache

12KB
182 lines

DynamoDB Type Serializer/Deserializer

Simple Example for Value -> AttributeValue

use aws_sdk_dynamodb::types::AttributeValue;
use serde_json::{Value, json};
use dynamodb_marshall::dynamodb;

fn main() {
    let input: Value = json!({
        "hello": "world",
        "n": 42,
        "some": {
            "deep": {
                "value": 42
            },
        },
    });

    // transform `Value` into a DynamoDB `AttributeValue`
    let value: AttributeValue = dynamodb::marshall(&input);
    // M({"hello": S("world"), "some": M({"deep": M({"value": N("42")})}), "n": N("42")})
    
    // ... upload value into dynamodb / do stuff

    // transform DynamoDB `AttributeValue` into a `Value`
    let original: Value = dynamodb::unmarshall(&value);
    // Object {"hello": String("world"), "n": Number(42), "some": Object {"deep": Object {"value": Number(42)}}}

    // Compare unmarshalled and input
    assert_eq!(
        input,
        original
    );
}

For struct that derive from Serialize, Deserialize

use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use aws_sdk_dynamodb::types::AttributeValue;
use dynamodb_marshall::dynamodb;

#[derive(Serialize, Deserialize, Debug, Default, Eq, PartialEq, Clone)]
struct Example {
    hello: String,
    world: bool,
    something: HashMap<String, String>,
    other: u64,
}

fn main() {
    let example = Example::default();
    //                                                         V may fail
    let value: AttributeValue = dynamodb::marshall_t(&example).unwrap();
    
    // Turn back to the struct                                            V may fail
    let same_example: Example = dynamodb::unmarshall_t::<Example>(&value).unwrap();
    
    assert_eq!(
        example,
        same_example,
    );
}

Dependencies

~15–22MB
~281K SLoC