dynamodb_marshall

Crates.iodynamodb_marshall
lib.rsdynamodb_marshall
version1.48.0
sourcesrc
created_at2023-05-20 03:42:32.886961
updated_at2024-10-02 01:44:05.732421
descriptionSerialize and deserialize AttributeValue to Value
homepagehttps://github.com/TheBinaryBrigade/dynamodb_marshall
repositoryhttps://github.com/TheBinaryBrigade/dynamodb_marshall
max_upload_size
id869198
size11,824
Rusty (DustinDiazLopez)

documentation

README

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,
    );
}
Commit count: 46

cargo fmt