Skip to content

serialize::json can't handle HashMap<uint, V> #12967

Closed
@olsonjeffery

Description

@olsonjeffery

or, AFAIK, any other numeric value.. I've isolated a few test cases in my local branch:

#[test]
    fn test_hashmap_with_numeric_key() {
        use std::str::from_utf8;
        use std::io::Writer;
        use std::io::MemWriter;
        use collections::HashMap;
        let mut hm: HashMap<uint, bool> = HashMap::new();
        hm.insert(1, true);
        let mut mem_buf = MemWriter::new();
        {
            let mut encoder = Encoder::new(&mut mem_buf as &mut io::Writer);
            hm.encode(&mut encoder)
        }
        let bytes = mem_buf.unwrap();
        let json_str = from_utf8(bytes).unwrap();
        match from_str(json_str) {
            Err(_) => fail!("Unable to parse json_str: {:?}", json_str),
            _ => {} // it parsed and we are good to go
        }
    }
    #[test]
    fn test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key() {
        use collections::HashMap;
        use Decodable;
        let json_str = "{\"1\":true}";
        let json_obj = match from_str(json_str) {
            Err(_) => fail!("Unable to parse json_str: {:?}", json_str),
            Ok(o) => o
        };
        let mut decoder = Decoder::new(json_obj);
        let hm: HashMap<uint, bool> = Decodable::decode(&mut decoder);
    }

here be the spew:

failures:

---- json::tests::test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key stdout ----
        task 'json::tests::test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key' failed at 'JSON decode error: expected number but found string: "1"', /Users/jeff/src/rust/src/libserialize/json.rs:1259

---- json::tests::test_hashmap_with_numeric_key stdout ----
        task 'json::tests::test_hashmap_with_numeric_key' failed at 'Unable to parse json_str: "{1:true}"', /Users/jeff/src/rust/src/libserialize/json.rs:2539


failures:
    json::tests::test_hashmap_with_numeric_key
    json::tests::test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key

test result: FAILED. 60 passed; 2 failed; 0 ignored; 8 measured

In case it isn't obvious: {1:true} is not valid JSON. Not only does it not pass muster for the web-based validators I checked (plus firefox's JSONView addon), but even serialize::json will not decode a JSON string created by it's own encoder.

The other test was something I created to catch/anticipate what the solution would end up producing.

I dug into the code for json::Encoder a bit and one of the solutions I wanted to pursue was to, in emit_map_elt_key, look at the state of the output bytes after running the f param that actually maps the key value. You can then see if the last key added is wrapped in quotes and add them if they're missing.

Sadly, the output state is kept as a &mut Writer so you have no ability examine/mutate it.. So I guess Decoder/Encoder will have to keep their own ~[u8] that it will grow/mutate during the process, and then dump into the wr field at the end, instead of growing it organically during the parsing process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions