diff --git a/README.md b/README.md index 92b7485..08f7eaf 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ There are known issues with some `serde` advanced features such as: Or types implementing similar strategies such as [`serde_json::Value`][serde_json_value]. +The `rmp-serde` serialization format can optionally support them serializing structs as maps, the `RmpSerdeNamed` struct is provided to support this use-case. + [serde_json_value]: https://docs.rs/serde_json/latest/serde_json/enum.Value.html ## Data model diff --git a/src/codec/rmp_serde_1_3.rs b/src/codec/rmp_serde_1_3.rs index 22c4b50..8cbba4a 100644 --- a/src/codec/rmp_serde_1_3.rs +++ b/src/codec/rmp_serde_1_3.rs @@ -1,11 +1,15 @@ //! [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) ยท //! Enable the `rmp_serde_1_3` feature and //! [`annotate your type`](crate::native_model) with -//! `native_model::rmp_serde_1_3::RmpSerde` to have `native_db` use this crate. +//! `native_model::rmp_serde_1_3::RmpSerde` or `native_model::rmp_serde_1_3::RmpSerdeNamed` +//! to have `native_db` use this crate. /// Used to specify the /// [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) -/// crate for serialization & deserialization. +/// crate for serialization & deserialization, using arrays to serialize structs. +/// +/// Do not use this if you plan to use serde features that skip serializing fields, +/// use [RmpSerdeNamed] instead. /// /// # Basic usage /// @@ -43,3 +47,45 @@ impl serde::Deserialize<'de>> crate::Decode for RmpSerde { rmp_serde_1_3::decode::from_slice(&data) } } + +/// Used to specify the +/// [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) +/// crate for serialization & deserialization, using maps to serialize structs. +/// +/// # Basic usage +/// +/// After enabling the `rmp_serde_1_3` feature in your `Cargo.toml`, use the +/// [`with`](crate::native_model) attribute on your type to instruct +/// `native_model` to use `RmpSerdeNamed` for serialization & deserialization. +/// +/// Example usage: +/// +/// ```rust +/// # use native_model::*; +/// #[derive(Clone, Default, serde::Deserialize, serde::Serialize)] +/// #[native_model(id = 1, version = 1, with = native_model::rmp_serde_1_3::RmpSerdeNamed)] +/// struct MyStruct { +/// #[serde(skip_serializing_if = "String::is_empty")] +/// my_string: String +/// } +/// ``` + +pub struct RmpSerdeNamed; + +#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))] +impl crate::Encode for RmpSerdeNamed { + type Error = rmp_serde_1_3::encode::Error; + /// Serializes a type into bytes using the `rmp-serde` `1.3` crate. + fn encode(obj: &T) -> Result, Self::Error> { + rmp_serde_1_3::encode::to_vec_named(obj) + } +} + +#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))] +impl serde::Deserialize<'de>> crate::Decode for RmpSerdeNamed { + type Error = rmp_serde_1_3::decode::Error; + /// Deserializes a type from bytes using the `rmp-serde` `1.3` crate. + fn decode(data: Vec) -> Result { + rmp_serde_1_3::decode::from_slice(&data) + } +}