pub struct Deserializer { /* private fields */ }
Expand description

A structure that deserializes AttributeValues into Rust values.

Implementations§

Create a Deserializer from an AttributeValue

Examples found in repository?
src/de/deserializer_enum.rs (line 59)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    fn newtype_variant_seed<S>(self, seed: S) -> Result<S::Value>
    where
        S: DeserializeSeed<'de>,
    {
        let deserializer = Deserializer::from_attribute_value(self.input);
        seed.deserialize(deserializer)
    }

    fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let deserializer = Deserializer::from_attribute_value(self.input);
        deserializer.deserialize_seq(visitor)
    }

    fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        let deserializer = Deserializer::from_attribute_value(self.input);
        deserializer.deserialize_map(visitor)
    }
More examples
Hide additional examples
src/de/mod.rs (line 28)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
pub fn from_attribute_value<'a, AV, T>(attribute_value: AV) -> Result<T>
where
    AV: Into<AttributeValue>,
    T: Deserialize<'a>,
{
    let attribute_value: AttributeValue = attribute_value.into();
    let deserializer = Deserializer::from_attribute_value(attribute_value);
    T::deserialize(deserializer)
}

/// Interpret an [`Item`] as an instance of type `T`.
///
/// ```no_run
/// # use __aws_sdk_dynamodb_0_22::client::Client;
/// # use serde_derive::{Serialize, Deserialize};
/// # use serde_dynamo::from_item;
/// #
/// # async fn scan(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
/// #[derive(Serialize, Deserialize)]
/// pub struct User {
///     id: String,
///     name: String,
///     age: u8,
/// };
///
/// // Get documents from DynamoDB
/// let result = client.scan().table_name("user").send().await?;
///
/// // And deserialize them as strongly-typed data structures
/// for item in result.items().map(|slice| slice.to_vec()).unwrap() {
///     let user: User = from_item(item)?;
///     println!("{} is {}", user.name, user.age);
/// }
/// # Ok(())
/// # }
/// ```
pub fn from_item<'a, I, T>(item: I) -> Result<T>
where
    I: Into<Item>,
    T: Deserialize<'a>,
{
    let item: Item = item.into();
    let deserializer = Deserializer::from_attribute_value(AttributeValue::M(item.into()));
    T::deserialize(deserializer)
}

/// Interpret a [`Items`] as a `Vec<T>`.
///
/// ```no_run
/// # use __aws_sdk_dynamodb_0_22::client::Client;
/// # use serde_derive::{Serialize, Deserialize};
/// # use serde_dynamo::from_items;
/// #
/// # async fn scan(client: &Client) -> Result<(), Box<dyn std::error::Error>> {
/// #[derive(Serialize, Deserialize)]
/// pub struct User {
///     id: String,
///     name: String,
///     age: u8,
/// };
///
/// // Get documents from DynamoDB
/// let result = client.scan().table_name("user").send().await?;
///
/// // And deserialize them as strongly-typed data structures
/// if let Some(items) = result.items().map(|slice| slice.to_vec()) {
///     let users: Vec<User> = from_items(items)?;
///     println!("Got {} users", users.len());
/// }
/// # Ok(())
/// # }
/// ```
pub fn from_items<'a, Is, T>(items: Is) -> Result<Vec<T>>
where
    Is: Into<Items>,
    T: Deserialize<'a>,
{
    let items: Items = items.into();
    let items = Vec::<HashMap<String, AttributeValue>>::from(items);
    let attribute_value = AttributeValue::L(items.into_iter().map(AttributeValue::M).collect());
    let deserializer = Deserializer::from_attribute_value(attribute_value);
    Vec::<T>::deserialize(deserializer)
}
src/de/deserializer_seq.rs (line 26)
21
22
23
24
25
26
27
28
29
30
31
    fn next_element_seed<S>(&mut self, seed: S) -> Result<Option<S::Value>, Self::Error>
    where
        S: DeserializeSeed<'de>,
    {
        if let Some(value) = self.iter.next() {
            let de = Deserializer::from_attribute_value(value);
            seed.deserialize(de).map(Some)
        } else {
            Ok(None)
        }
    }
src/de/deserializer_map.rs (line 43)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
    where
        V: DeserializeSeed<'de>,
    {
        if let Some(value) = self.remaining_value.take() {
            let de = Deserializer::from_attribute_value(value);
            seed.deserialize(de)
        } else {
            unreachable!("Value without a corresponding key")
        }
    }
}

struct DeserializerMapKey {
    input: String,
}

impl DeserializerMapKey {
    fn from_string(input: String) -> Self {
        Self { input }
    }
}

macro_rules! deserialize_integer_key {
    ($method:ident => $visit:ident) => {
        fn $method<V>(self, visitor: V) -> Result<V::Value>
        where
            V: de::Visitor<'de>,
        {
            let number = self
                .input
                .parse()
                .map_err(|_| ErrorImpl::ExpectedNum.into())?;

            visitor.$visit(number)
        }
    };
}

impl<'de> de::Deserializer<'de> for DeserializerMapKey {
    type Error = Error;

    // Look at the input data to decide what Serde data model type to
    // deserialize as. Not all data formats are able to support this operation.
    // Formats that support `deserialize_any` are known as self-describing.
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
    where
        V: Visitor<'de>,
    {
        self.deserialize_string(visitor)
    }

    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_string(self.input)
    }

    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_string(self.input)
    }

    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>,
    {
        visitor.visit_string(self.input)
    }

    fn deserialize_enum<V>(
        self,
        name: &'static str,
        variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value>
    where
        V: de::Visitor<'de>,
    {
        let de = Deserializer::from_attribute_value(AttributeValue::S(self.input));
        de.deserialize_enum(name, variants, visitor)
    }

Trait Implementations§

Formats the value using the given formatter. Read more
The error type that can be returned if some error occurs during deserialization.
Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Hint that the Deserialize type is expecting an i8 value.
Hint that the Deserialize type is expecting a u8 value.
Hint that the Deserialize type is expecting an i16 value.
Hint that the Deserialize type is expecting an i32 value.
Hint that the Deserialize type is expecting an i64 value.
Hint that the Deserialize type is expecting a u16 value.
Hint that the Deserialize type is expecting a u32 value.
Hint that the Deserialize type is expecting a u64 value.
Hint that the Deserialize type is expecting a f32 value.
Hint that the Deserialize type is expecting a f64 value.
Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Hint that the Deserialize type is expecting a sequence of values.
Hint that the Deserialize type is expecting a map of key-value pairs.
Hint that the Deserialize type is expecting a bool value.
Hint that the Deserialize type is expecting a char value.
Hint that the Deserialize type is expecting a unit value.
Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data.
Hint that the Deserialize type is expecting an optional value. Read more
Hint that the Deserialize type is expecting a struct with a particular name and fields.
Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant.
Hint that the Deserialize type is expecting a unit struct with a particular name.
Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields.
Hint that the Deserialize type is expecting a newtype struct with a particular name.
Hint that the Deserialize type is expecting an i128 value. Read more
Hint that the Deserialize type is expecting an u128 value. Read more
Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more