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

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

Open
AldoMX opened this issue Oct 3, 2020 · 1 comment

Comments

@AldoMX
Copy link

AldoMX commented Oct 3, 2020

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);
}
@AldoMX
Copy link
Author

AldoMX commented Oct 3, 2020

Researching more about JSON5 I found out that numeric keys (ie. { 0: "hello" }) are not valid, but string keys (ie. { "0": "hello" }) are.

Then there are 2 different issues with the test I provided:

  1. Number keys are being serialized without quotes
  2. String keys are not being parsed as numbers when the target type expects a number

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

No branches or pull requests

1 participant