This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Attribute macro to skip serializing all fields of Option type #18
Comments
jonasbb
added a commit
to jonasbb/serde_with
that referenced
this issue
Mar 19, 2019
A proc_macro is required to implement skip_serializing_null from dtolnay/request-for-implementation#18
7 tasks
I'm tackling this issue over at jonasbb/serde_with#46 EDIT: This feature is included in version 1.3.0 of |
jonasbb
added a commit
to jonasbb/serde_with
that referenced
this issue
Mar 31, 2019
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 bot
added a commit
to jonasbb/serde_with
that referenced
this issue
Mar 31, 2019
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]>
bors bot
added a commit
to jonasbb/serde_with
that referenced
this issue
Apr 2, 2019
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]>
Looks like this feature is completed, no? |
??? |
I think this already shipped in the current crate |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Some JSON workflows involve practically every piece of data being nullable and none of then being serialized when null. The Serde attributes in this situation can be verbose and distracting.
It would be better to have an attribute macro that finds all fields of type Option<_> and inserts a skip_serializing_if attribute.
Optionally consider supporting an attribute to annotate optional fields that always need to be serialized, even when null.
The text was updated successfully, but these errors were encountered: