Skip to content

maintaining order when deserializing to maps #1505

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
samuelcolvin opened this issue Apr 11, 2019 · 2 comments
Closed

maintaining order when deserializing to maps #1505

samuelcolvin opened this issue Apr 11, 2019 · 2 comments

Comments

@samuelcolvin
Copy link

Continuation from #269, but that was a long time ago.

That issue refers to linked-hash-map however that crate is in maintenance mode and as far as I can tell it doesn't seem to support deserializing.

Running

extern crate linked_hash_map;
use linked_hash_map::LinkedHashMap as Map;

fn main() {
    let s = r#"{"x": 1, "y": 2}"#;
    let deserialized: Map<String, i32> = serde_json::from_str(&s).unwrap();
    println!("deserialized = {:?}", deserialized);
}

with rust rustc 1.34.0 (91856ed52 2019-04-10) gives:

cargo run
   Compiling serde v1.0.90
   Compiling ryu v0.2.7
   Compiling itoa v0.4.3
   Compiling linked-hash-map v0.5.2
   Compiling serde_json v1.0.39
   Compiling donkey-make v0.1.1 (/home/samuel/code/donkey-make)
error[E0277]: the trait bound `linked_hash_map::LinkedHashMap<std::string::String, i32>: serde::de::Deserialize<'_>` is not satisfied
 --> src/main.rs:6:42
  |
6 |     let deserialized: Map<String, i32> = serde_json::from_str(&s).unwrap();
  |                                          ^^^^^^^^^^^^^^^^^^^^ the trait `serde::de::Deserialize<'_>` is not implemented for `linked_hash_map::LinkedHashMap<std::string::String, i32>`
  |
  = note: required by `serde_json::de::from_str`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `donkey-make`.

To learn more, run the command again with --verbose.

I've also tried indexmap which seems to be still support however I get a similar error.

@dtolnay
Copy link
Member

dtolnay commented Apr 12, 2019

You need to opt into their serde impls.

[dependencies]
linked-hash-map = { version = "0.5", features = ["serde_impl"] }

@samuelcolvin
Copy link
Author

samuelcolvin commented Apr 12, 2019

thanks, I was being dumb.

indexmap also supports serde it turns out:

[dependencies]
indexmap = {version = "1.0.2", features = ["serde-1"]}

Might be worth adding to docs.

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

2 participants