simd-json icon indicating copy to clipboard operation
simd-json copied to clipboard

Using integer as a map key causes incorrect rendering

Open Kogia-sima opened this issue 4 years ago • 0 comments

Description

I'd like to serialize BTreeMap which has integer as a key. However, when I passed map into to_string or to_string_pretty, simd_json outpus invalid JSON data.

use std::collections::BTreeMap;

fn main() {
    let mut map = BTreeMap::new();
    map.insert(0, "foo");
    map.insert(1, "bar");
    map.insert(2, "baz");

    println!("{}", simd_json::to_string_pretty(&map).unwrap());
}

output:

{
  0: "foo",
  1: "bar",
  2: "baz"
}

Expected behavior

I want the JSON output like this:

{
  "0": "foo",
  "1": "bar",
  "2": "baz"
}

Possible solution

serde_json crate defines a new struct named MapKeySerializer to properly render the map keys.

Environment

Software Version
OS Lubuntu 20.04.2 LTS
rustc rustc stable 1.48.0 (7eac88abb 2020-11-16)
simd-json 0.3.23

Kogia-sima avatar Feb 08 '21 12:02 Kogia-sima