Skip to content
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

Merged
merged 9 commits into from
Apr 2, 2019
Merged

Add a skip_serializing_null attribute #46

merged 9 commits into from
Apr 2, 2019

Conversation

jonasbb
Copy link
Owner

@jonasbb jonasbb commented Mar 19, 2019

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

#[skip_serializing_null]
#[derive(Serialize)]
struct Data {
    a: Option<String>,
    b: Option<String>,
    c: Option<String>,
    d: Option<String>,
}

into

#[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

  • Is skip_serializing_null the best name? That is how serde or JSON name the empty value, in Rust it is none.
  • Support tuple structs
  • Support enums
  • Handle existing skip_serializing_if annotations, by skipping those fields
  • Support an additional attribute, which ensures the field is always serialized
  • Write compile tests, which ensure the correct error message
  • Write documentation for the feature

@jonasbb jonasbb changed the title Add a skip_serializing_null attribute [WIP] Add a skip_serializing_null attribute Mar 19, 2019
Repository owner deleted a comment from codecov bot Mar 19, 2019
Repository owner deleted a comment from codecov bot Mar 21, 2019
Repository owner deleted a comment from codecov bot Mar 21, 2019
Repository owner deleted a comment from codecov bot Mar 21, 2019
@codecov
Copy link

codecov bot commented Mar 21, 2019

Codecov Report

Merging #46 into master will decrease coverage by 3.59%.
The diff coverage is 41.37%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/lib.rs 25% <ø> (ø) ⬆️
serde_with_macros/src/lib.rs 0% <0%> (ø)
serde_with_macros/tests/skip_serializing_null.rs 100% <100%> (ø)
serde_with_macros/tests/version_numbers.rs 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a05488a...e078ca8. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 21, 2019

Codecov Report

Merging #46 into master will decrease coverage by 2.5%.
The diff coverage is 51.72%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/lib.rs 25% <ø> (ø) ⬆️
serde_with_macros/src/lib.rs 0% <0%> (ø)
serde_with_macros/tests/skip_serializing_null.rs 100% <100%> (ø)
serde_with_macros/tests/version_numbers.rs 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 26d84b1...cf89b47. Read the comment docs.

@jonasbb jonasbb changed the title [WIP] Add a skip_serializing_null attribute Add a skip_serializing_null attribute Mar 31, 2019
@jonasbb
Copy link
Owner Author

jonasbb commented Mar 31, 2019

bors r+

bors bot added a commit that referenced this pull request 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
Copy link
Contributor

bors bot commented Mar 31, 2019

Build failed

@jonasbb
Copy link
Owner Author

jonasbb commented Apr 2, 2019

bors try

bors bot added a commit that referenced this pull request Apr 2, 2019
@bors
Copy link
Contributor

bors bot commented Apr 2, 2019

try

Build succeeded

@jonasbb
Copy link
Owner Author

jonasbb commented Apr 2, 2019

bors r+

bors bot added a commit that referenced this pull request 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]>
@bors
Copy link
Contributor

bors bot commented Apr 2, 2019

Build succeeded

@bors bors bot merged commit cf89b47 into master Apr 2, 2019
@bors bors bot deleted the proc_macros branch April 2, 2019 19:25
@doivosevic
Copy link

I cannot find this macro. How do we include it? Googling for it gives no results

@jonasbb
Copy link
Owner Author

jonasbb commented Jan 12, 2022

@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.

Repository owner locked as off-topic and limited conversation to collaborators Jan 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants