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

Struct as map representation and leftover field collection #1178

Closed
wants to merge 7 commits into from

Conversation

mitsuhiko
Copy link
Contributor

@mitsuhiko mitsuhiko commented Mar 14, 2018

This PR is similar to #1177 in spirit but very different in implementation. Because of current restrictions that serialize_field on the struct visitor requires static strings and because there is a hint about the incoming field names on the struct visitor I think this "collect other things" feature currently only really works when serializing as maps.

I tried a different approach now which also adds a neat additional feature. A (struct) container can now be marked with #[serde(repr="map")] which serializes the struct not as a struct but as a map. This then also unlocks #[serde(unknown_fields_into="...")]

So the example looks slightly modified like this:

#[derive(Deserialize, Serialize, Debug)]
#[serde(unknown_fields_into="other", repr="map")]
pub struct Breadcrumb {
    #[serde(rename="type")]
    ty: String,
    timestamp: f64,
    other: HashMap<String, String>,
}

Unlike the other one this however has a chance of being merged I think because it can be made sound and user friendly without requiring changes to the already existing traits. In a far serde 2.0 future the repr="map" requirement could then be removed.

Known issues:

  • calls to_string which does not work in no_std. Generally this right now does not work in no_std mode. That might be okay if it's shimmed properly.
  • I don't like the fact that I added a second bool to some methods.
  • duplicated key checking is missing
  • needs tests
  • the branch that decides on __value decoding is complex. Could need some refactoring

@mitsuhiko mitsuhiko changed the title WIP: Added struct as map representation and leftover field collection Struct as map representation and leftover field collection Mar 14, 2018
@mitsuhiko mitsuhiko closed this Mar 15, 2018
@mitsuhiko
Copy link
Contributor Author

Closing this. Working on a version of this PR with a coworker that actually implements flattening instead. This turns out to be what we actually need for most of our usecase anyways. Will push out a new PR soon.

@mitsuhiko mitsuhiko mentioned this pull request Mar 16, 2018
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant