-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a skip_serializing_null
attribute
#46
Conversation
skip_serializing_null
attributeskip_serializing_null
attribute
Codecov Report
@@ Coverage Diff @@
## master #46 +/- ##
=========================================
- Coverage 75.44% 71.84% -3.6%
=========================================
Files 11 14 +3
Lines 737 824 +87
=========================================
+ Hits 556 592 +36
- Misses 181 232 +51
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #46 +/- ##
==========================================
- Coverage 75.44% 72.93% -2.51%
==========================================
Files 11 14 +3
Lines 737 824 +87
==========================================
+ Hits 556 601 +45
- Misses 181 223 +42
Continue to review full report at Codecov.
|
skip_serializing_null
attributeskip_serializing_null
attribute
A proc_macro is required to implement skip_serializing_null from dtolnay/request-for-implementation#18 Expose the macro crate in serde_with * Add a "macros"-feature to disable compiling proc_macros * Add the feature to the testing matrix
bors r+ |
46: Add a `skip_serializing_null` attribute r=jonasbb a=jonasbb The `skip_serializing_null` attribute can be added to any struct and adds `#[serde(skip_serializing_if = "Option::is_none")]` to every `Option` field. The intended use is for parsing data, e.g., from APIs, which has many optional values. It turns ```rust #[skip_serializing_null] #[derive(Serialize)] struct Data { a: Option<String>, b: Option<String>, c: Option<String>, d: Option<String>, } ``` into ```rust #[derive(Serialize)] struct Data { #[serde(skip_serializing_if = "Option::is_none")] a: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] b: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] c: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] d: Option<String>, } ``` The issue was originally suggested at dtolnay/request-for-implementation#18. # Missing * [x] Is `skip_serializing_null` the best name? That is how serde or JSON name the empty value, in Rust it is none. * [x] Support tuple structs * [x] Support enums * [x] Handle existing `skip_serializing_if` annotations, by skipping those fields * [x] Support an additional attribute, which ensures the field is always serialized * [x] Write compile tests, which ensure the correct error message * [x] Write documentation for the feature Co-authored-by: Jonas Bushart <[email protected]>
Build failed |
bors try |
tryBuild succeeded |
bors r+ |
46: Add a `skip_serializing_null` attribute r=jonasbb a=jonasbb The `skip_serializing_null` attribute can be added to any struct and adds `#[serde(skip_serializing_if = "Option::is_none")]` to every `Option` field. The intended use is for parsing data, e.g., from APIs, which has many optional values. It turns ```rust #[skip_serializing_null] #[derive(Serialize)] struct Data { a: Option<String>, b: Option<String>, c: Option<String>, d: Option<String>, } ``` into ```rust #[derive(Serialize)] struct Data { #[serde(skip_serializing_if = "Option::is_none")] a: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] b: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] c: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] d: Option<String>, } ``` The issue was originally suggested at dtolnay/request-for-implementation#18. # Missing * [x] Is `skip_serializing_null` the best name? That is how serde or JSON name the empty value, in Rust it is none. * [x] Support tuple structs * [x] Support enums * [x] Handle existing `skip_serializing_if` annotations, by skipping those fields * [x] Support an additional attribute, which ensures the field is always serialized * [x] Write compile tests, which ensure the correct error message * [x] Write documentation for the feature Co-authored-by: Jonas Bushart <[email protected]>
Build succeeded |
I cannot find this macro. How do we include it? Googling for it gives no results |
@doivosevic You find all the information in the documentation https://docs.rs/serde_with. Please open a new discussion if you need support and do not comment on year old PRs. |
The
skip_serializing_null
attribute can be added to any struct and adds#[serde(skip_serializing_if = "Option::is_none")]
to everyOption
field.The intended use is for parsing data, e.g., from APIs, which has many optional values.
It turns
into
The issue was originally suggested at dtolnay/request-for-implementation#18.
Missing
skip_serializing_null
the best name? That is how serde or JSON name the empty value, in Rust it is none.skip_serializing_if
annotations, by skipping those fields