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

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

Closed
olsonjeffery opened this issue Mar 17, 2014 · 0 comments
Closed

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

olsonjeffery opened this issue Mar 17, 2014 · 0 comments

Comments

@olsonjeffery
Copy link
Contributor

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.

olsonjeffery added a commit to olsonjeffery/rust that referenced this issue Mar 17, 2014
… numeric type

serialize: ref rust-lang#12697 minor adj. to last char check + prettyencode test
bors added a commit that referenced this issue Mar 19, 2014
Closes #13008 (Made the `clone_from` implementation for `~T` reuse the `T` itself if possible)
Closes #13003 (Make method Vec::remove() public)
Closes #13002 (disallow duplicate methods in trait impls)
Closes #13000 (rustc: test: don't silently ignore bad benches)
Closes #12999 (rustc: buffer the output writer for -Z ast-json[-noexpand].)
Closes #12993 (syntax: Don't parameterize the the pretty printer)
Closes #12990 (`char` reference: s/character/Unicode scalar value/)
Closes #12987 (Move syntax-extension-hexfloat.rs)
Closes #12983 (Fix linkage1 test which fails due to --as-needed)
Closes #12978 (rustc: remove linker_private/linker_private_weak)
Closes #12976 (libsyntax: librustdoc: ignore utf-8 BOM in .rs files)
Closes #12973 (closes #12967 fix [en|de]coding of HashMap<K,V> where K is a numeric type)
Closes #12972 (Add impl IntoStr for ::std::vec_ng::Vec<Ascii>)
Closes #12968 (deny missing docs getopts)
Closes #12965 (Documentation and formatting changes for option.rs.)
Closes #12962 (Relax the memory ordering on the implementation of UnsafeArc)
Closes #12958 (Typo fixes.)
Closes #12950 (Docsprint: Document ops module, primarily Deref.)
Closes #12946 (rustdoc: Implement cross-crate searching)
lnicola pushed a commit to lnicola/rust that referenced this issue Aug 16, 2022
[code] make toggleInlayHints understand {off,on}UntilPressed

fixes rust-lang#12964
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant