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

Round-trip failure: Empty maps/sequences gain an extra layer of nesting #338

Closed
saethlin opened this issue Nov 16, 2021 · 4 comments · Fixed by #341
Closed

Round-trip failure: Empty maps/sequences gain an extra layer of nesting #338

saethlin opened this issue Nov 16, 2021 · 4 comments · Fixed by #341
Labels

Comments

@saethlin
Copy link

This example program probably ought to exit successfully

fn main() {
    let input = "{}";
    let v: ron::Value = ron::from_str(input).unwrap();
    println!("{:?}", v);
    let ser = ron::to_string(&v).unwrap();
    println!("{:?}", ser);
    let roundtrip = ron::from_str(&ser).unwrap();
    println!("{:?}", roundtrip);
    assert_eq!(v, roundtrip);
}

But it doesn't:

Map(Map({}))
"({})"
Seq([Map(Map({}))])
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Map(Map({}))`,
 right: `Seq([Map(Map({}))])`', src/main.rs:9:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This actually continues indefinitely. "{}" becomes "({})" which becomes "[({})]" and from then on we just add square brackets every round-trip.

@juntyr
Copy link
Member

juntyr commented Nov 17, 2021

It seems the wrapping here is to blame:

ron/src/value.rs

Lines 25 to 26 in c4a7834

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Map(MapInner);

Adding the #[serde(transparent)] attribute to the struct seems to fix the problem.

@torkleyy torkleyy added the bug label Nov 17, 2021
@torkleyy
Copy link
Contributor

This is definitely a bug and should be fixed, thanks for the report!

However, please note that in general, Value cannot be round-tripped.

@saethlin
Copy link
Author

However, please note that in general, Value cannot be round-tripped.

Can you elaborate on this? I'm aware that textual formats like RON don't round-trip some float values correctly, is there something else specific to RON?

@torkleyy
Copy link
Contributor

However, please note that in general, Value cannot be round-tripped.

Can you elaborate on this? I'm aware that textual formats like RON don't round-trip some float values correctly, is there something else specific to RON?

Yes, sure. The serde data model is inspired by JSON and thus does not differentiate struct from map, lists from tuples, possibly more I don't recall right now. Since we use serde to serialize / deserialize Value, the information gets lost. This is not an issue when using regular Rust types with #[derive(Deserialize, Serialize)] because they provide the type information.

juntyr added a commit to juntyr/ron that referenced this issue Nov 19, 2021
torkleyy added a commit that referenced this issue Nov 19, 2021
torkleyy added a commit to torkleyy/ron that referenced this issue Jun 6, 2022
@torkleyy torkleyy mentioned this issue Jun 6, 2022
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants