#dynamo-db #aws-sdk #marshall #unmarshall #typeserializer #typedeserializer

dynamodb_marshall

将 AttributeValue 序列化和反序列化为 Value

22 个版本 (10 个稳定版)

1.42.0 2024 年 8 月 19 日
1.37.0 2024 年 7 月 6 日
1.36.0 2024 年 6 月 24 日
1.19.0 2024 年 3 月 21 日
0.28.0 2023 年 5 月 25 日

#704 in 解析器实现

Download history 12/week @ 2024-05-17 2/week @ 2024-05-24 21/week @ 2024-06-07 23/week @ 2024-06-14 170/week @ 2024-06-21 31/week @ 2024-06-28 248/week @ 2024-07-05 79/week @ 2024-07-12 42/week @ 2024-07-19 33/week @ 2024-07-26 6/week @ 2024-08-02 6/week @ 2024-08-09 149/week @ 2024-08-16

每月 205 次下载

MIT/Apache

12KB
182

DynamoDB 类型序列化/反序列化器

简单示例:将 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
    );
}

对于从 struct 派生的 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,
    );
}

依赖项

~17MB
~281K SLoC