Skip to content

reading a numeric key in objects returns expected identifier or string error #25

@AldoMX

Description

@AldoMX

Hi, thank you for writing the JSON5 serializer/deserializer. During my tests I found that an object like { 0: "hello" } returns error, I leave you a test case here:

#[test]
fn json5test() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Duck {
        name: String,
        age: usize,
    }

    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct DuckFamily {
        triplets: BTreeMap<usize, Duck>,
    }

    let family = {
        let mut triplets = BTreeMap::new();
        triplets.insert(
            1,
            Duck {
                name: "Huey".to_owned(),
                age: 9,
            },
        );
        triplets.insert(
            2,
            Duck {
                name: "Dewey".to_owned(),
                age: 9,
            },
        );
        triplets.insert(
            3,
            Duck {
                name: "Louie".to_owned(),
                age: 9,
            },
        );
        DuckFamily { triplets }
    };

    let encoded = json5::to_string(&family).unwrap();
    println!("{:?}", &encoded);
    let decoded: DuckFamily = json5::from_str(&encoded).unwrap();  // error here
    println!("{:?}", &decoded);
    assert_eq!(decoded, family);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions