Skip to content

Deserialize object/map while maintaining order #269

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

Closed
calebmer opened this issue Mar 19, 2016 · 7 comments
Closed

Deserialize object/map while maintaining order #269

calebmer opened this issue Mar 19, 2016 · 7 comments
Assignees

Comments

@calebmer
Copy link

Is there a way to deserialize a map (like a JSON or YAML object) while maintaining the order of key/value pairs? The BTreeMap type deserializes into kind of what I want except it does not maintain order.

Deserializing a map to Vec<(K, V)> would be perfect.

@dtolnay
Copy link
Member

dtolnay commented Mar 20, 2016

Using an ordered map type would be more appropriate than Vec<(K, V)>. I found a linked hash map implementation and made a PR to add Serde support: contain-rs/linked-hash-map#48

LinkedHashMap will work for JSON but unfortunately not for serde-yaml because serde-yaml uses yaml_rust which uses a BTreeMap for maps.

@dtolnay
Copy link
Member

dtolnay commented Mar 20, 2016

I made a PR that adds a preserve_order feature to yaml-rust: chyh1990/yaml-rust#16. Once that merges and yaml-rust does a 0.3.1 release, I can update serde-yaml to depend on it: dtolnay/serde-yaml#14.

I will check off the PRs as they merge:

@calebmer
Copy link
Author

Awesome, thanks so much! If you need anything from me feel free to ask 😊
On Sun, Mar 20, 2016 at 02:06 David Tolnay [email protected] wrote:

I made a PR that adds a preserve_order feature to yaml-rust:
chyh1990/yaml-rust#16 chyh1990/yaml-rust#16.
Once that merges and yaml-rust does a 0.3.1 release, I can update
serde-yaml to depend on it: dtolnay/serde-yaml#14
dtolnay/serde-yaml#14.

I will check off the PRs as they merge:


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#269 (comment)

@dtolnay
Copy link
Member

dtolnay commented Apr 21, 2016

All set here, I think. The answer is to use an ordered map type (linked-hash-map and linear-map both come with serde support). For YAML you additionally need to set the preserve_order feature flag to force yaml-rust to use LinkedHashMap instead of BTreeMap. This issue can be closed.

@oli-obk
Copy link
Member

oli-obk commented Apr 21, 2016

Should we add this feature to the json value type?

@calebmer
Copy link
Author

Awesome, thanks! @oli-obk, maybe that should be a fresh issue?

@dtolnay
Copy link
Member

dtolnay commented Apr 21, 2016

I filed serde-rs/json#54 to follow up on adding a feature flag to use linked-hash-map in serde_json::Value.

@dtolnay dtolnay self-assigned this Jun 15, 2016
rubdos pushed a commit to rubdos/serde that referenced this issue Jun 20, 2017
bigint: Create the parsing error better for nested `+`

If a `+` is encountered in the middle of parsing a BigUint, this should
generate an `ParseIntError::InvalidDigit`.  Since we can't create that
directly, we get it by trying to parse a `u64` from this point, but of
course `+` is a perfectly valid prefix to a `u64`.

Now we include the previous character in the string passed to `u64`, so it
has proper parsing context to understand what's in error.

Fixes serde-rs#268.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants