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

(De-) Serialize option into two fields #2699

Open
Ruhrpottpatriot opened this issue Feb 13, 2024 · 0 comments
Open

(De-) Serialize option into two fields #2699

Ruhrpottpatriot opened this issue Feb 13, 2024 · 0 comments

Comments

@Ruhrpottpatriot
Copy link

Ruhrpottpatriot commented Feb 13, 2024

I have the following JSON given to me:

"report": bool
"report_url": string

I want to (de-)serialize it into a custom struct. The report url is optional and only considered if the report field is set to true. Now, I could write a struct that directly maps the above structure, i.e.

struct Reporter {
    report: bool,
    repoort_url: Option<String>,

However, this essentially duplicates the information that the report_url is not present and I'd rather write:

struct Reporter {
    report_url: Option<String>,

where the report_url field is set to Some(value) iff the report flag in the JSON is set to true, and to None otherwise. Is this possible with serde?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant