#view #serde #derive #serde-default

macro serde_view_macros

Serialize views of data

5 releases

0.1.6 Apr 27, 2023
0.1.5 Apr 21, 2023
0.1.3 Apr 18, 2023
0.1.2 Apr 18, 2023
0.1.0 Apr 18, 2023

#23 in #serde-default


Used in serde_view

Apache-2.0

6KB
101 lines

Serialize views of data

Dynamically select during serialization which fields will be included.

Example

use serde_view::View;
use serde_view::ViewFields;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, View)]
pub struct MyStruct {
    id: String,
    #[serde(default)]
    name: String,
    #[serde(default)]
    tags: Vec<String>,
}

fn serialize(my: &MyStruct) -> Result<serde_json::Value, serde_json::Error> {
    serde_json::to_value(my.as_view().with_fields([
       <MyStruct as View>::Fields::Id,
       <MyStruct as View>::Fields::Name,
   ]).unwrap())
}

Dependencies

~1–1.5MB
~28K SLoC