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

Nested optional property issue #6

Open
evilrobot-01 opened this issue Jun 29, 2022 · 0 comments
Open

Nested optional property issue #6

evilrobot-01 opened this issue Jun 29, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@evilrobot-01
Copy link

Hi there!

Just trying out your crate and get an issue which seems to be down to optional nested properties, where some of the data appears in the wrong place when unpacking. Details and a sample test below...

image

[dependencies]
jsonm = "0.2.0"
#[cfg(test)]
mod tests {
    use jsonm::packer::{PackOptions, Packer};
    use jsonm::unpacker::Unpacker;
    use serde::{Deserialize, Serialize};
    use std::collections::BTreeMap;

    #[derive(Debug, Deserialize, Serialize)]
    struct Person {
        name: String,
        age: u8,
        address: String,
    }

    impl Person {
        fn new(name: &str, age: u8, address: &str) -> Person {
            Person {
                name: name.to_string(),
                age,
                address: address.to_string(),
            }
        }
    }

    #[derive(Debug, Deserialize, Serialize)]
    struct Record {
        person: Person,
        tag: Option<String>,
    }

    impl Record {
        fn new(person: Person) -> Record {
            Record { person, tag: None }
        }
    }

    #[test]
    // fails as "tag":null becomes "tag": "name 2" through packing to string and then unpacking
    fn test() {
        let records: BTreeMap<u32, Record> = BTreeMap::from([
            (1, Record::new(Person::new("name", 18, "address 1"))),
            (2, Record::new(Person::new("name 2", 60, "address 2"))),
            (3, Record::new(Person::new("name 3", 32, "address 3"))),
            (4, Record::new(Person::new("name 4", 9, "address 2"))),
        ]);
        let serialised = serde_json::to_string(&records).unwrap();

        let packed = pack(records);
        let unpacked: BTreeMap<u32, Record> = unpack(packed);

        assert_eq!(serialised, serde_json::to_string(&unpacked).unwrap())
    }

    fn pack<T: Serialize>(value: T) -> String {
        let mut packer = Packer::new();
        let options = PackOptions::new();
        let value = serde_json::value::to_value(value).unwrap();
        println!("pack input: {value}");
        let packed = packer.pack(&value, &options).unwrap();
        println!("packed: {packed}");
        packed.to_string()
    }

    fn unpack<T: for<'de> Deserialize<'de>>(value: String) -> T {
        println!("unpack input: {value}");
        let packed = serde_json::from_str(&value).unwrap();
        println!("unpacked value: {packed}");
        let mut unpacker = Unpacker::new();
        let unpacked = unpacker.unpack(&packed).unwrap();
        println!("unpacked: {unpacked}");
        let unpacked = serde_json::from_value(unpacked).unwrap();
        unpacked
    }
}
@andrewnester andrewnester self-assigned this Oct 22, 2024
@andrewnester andrewnester added the bug Something isn't working label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants