Skip to content

JSON serialization of HashMaps with Enum keys is broken #21470

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
japaric opened this issue Jan 21, 2015 · 3 comments · Fixed by rust-lang-deprecated/rustc-serialize#32 or #21662
Closed

Comments

@japaric
Copy link
Member

japaric commented Jan 21, 2015

STR

extern crate serialize;

use serialize::json;
use std::collections::HashMap;

#[derive(Encodable, Eq, Hash, PartialEq)]
enum Enum {
    Foo,
    Bar,
}

fn main() {
    let mut map = HashMap::new();
    map.insert(0, 0);
    println!("{}", json::encode(&map));

    let mut map = HashMap::new();
    map.insert(Enum::Foo, 0);
    println!("{}", json::encode(&map));
    println!("^ what?!");
}

Output

{"0":0}
{
^ what?!

Version

rustc 1.0.0-nightly (29bd9a06e 2015-01-20 23:03:09 +0000)

The second hashmap was properly being serialized as {"Foo":0} with a two days old nightly (rustc 1.0.0-nightly (dcaeb6aa2 2015-01-18 11:28:53 +0000)).

cc @aturon @alexcrichton @gankro

@alexcrichton
Copy link
Member

cc @oli-obk

@bluss
Copy link
Member

bluss commented Jan 21, 2015

I think it is that json::encode ignores encoder errors and the error case is new -- not a valid map key.

@bluss
Copy link
Member

bluss commented Jan 21, 2015

Related PR #19490 Since encoding errors there now, the obvious bug would be that the error is suppressed silently.

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