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

No way for custom formats to initialize default values. #2833

Open
ConradIrwin opened this issue Oct 15, 2024 · 0 comments
Open

No way for custom formats to initialize default values. #2833

ConradIrwin opened this issue Oct 15, 2024 · 0 comments

Comments

@ConradIrwin
Copy link

ConradIrwin commented Oct 15, 2024

I am trying to write a serve deserializer for an (in-progress) language that omits default values from struct serialization.

This is not a problem for fields like strings, numbers, booleans, lists and maps, because I can use the well-defined default of 0, false or empty. That said, I found no way to ask serde to default the values for structs/tuples and enums in the presence of missing keys.

The things I tried so far:

  • Recursively initializing struct/tuple fields to their primitive types' default values. This works, but does not respect any #[serde(default) or rust-level default values.
  • Asking the user to remember to include #[serde(default)] on all fields/structs. This works, but is quite tedious.

My ideal solution would be something like adding an optional visitor.visit_default() to the Visitor protocol, so that I could correctly initialize default values for this file format.

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