ron: Struct flattening not working
I’m trying to use Serde’s struct flattening functionality with RON, using the Rust code below:
#[derive(Deserialize, Serialize)]
struct Main {
#[serde(flatten)]
required: Required,
#[serde(flatten)]
optional: Optional,
some_other_field: u32,
}
#[derive(Deserialize, Serialize)]
struct Required {
first: u32,
second: u32,
}
#[derive(Deserialize, Serialize)]
struct Optional {
third: Option<u32>,
}
This is the RON that I am attempting to deserialize:
Main(
first: 1,
second: 2,
third: Some(3),
some_other_field: 1337,
)
When I try to deserialize the target struct from RON, I get the following error:
thread 'example::test' panicked at 'called `Result::unwrap()` on an `Err` value: Parser(ExpectedMap, Position { col: 9, line: 2 })', libcore/result.rs:945:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
On the other hand, deserializing from the following TOML string works as intended:
first = 1
second = 2
third = 3
some_other_field = 4
Any indication on what might be going wrong?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (15 by maintainers)
Commits related to this issue
- Fixes #115 struct flattening — committed to juntyr/ron by juntyr 4 years ago
- Fixes #115 struct flattening — committed to juntyr/ron by juntyr 4 years ago
I did a bit more digging and flatten is even more broken than I had realised the last time. #403 would only sort-of-fix deserialising from a RON struct into a flattened struct, but it does not solve serialising at all. At serialisation time, serde uses
Serializer::serialize_mapto output the flattened struct - so far so good? If it forces RON to output a map and RON could then read in that map, all would be good even if serde would force ugly map syntax on RON users. Unfortunately, no.Serialising has no notion of identifiers but deserialising does. So while serialising a flattened struct outputs a map with stringified keys, at deserialisation time serde then asks for a map which contains identifiers as keys. In RON identifiers are typed to not be strings. So while serialising outputs
{"field": value}, deserialising expects{field: value}. If RON now starts to accept strings where an identifier should be, it is forced to repeat all of JSON’s problems just because serde was designed for JSON and, as it seems, no other format in mind. And this just does not seem like a good path - RON’s codebase cannot consist of 50% special cases to handle serde’s weird data model to the point where we can no longer understand this code.I think there are two ways out of this and I’m sorry that they both sound bad right now. I would be delighted if someone comes up with something better.
serialize_mapcall as if it were a struct - serde would need to fix this with new API and the willingness for that does not seem to be there). This would split RON in two, a strict mode and a serde mode, and would complicate the code base.serde2that works with existingserdeimpls to ease adoption but cleans up both the serialisation and deserialisation APIs to mirror each other, be as strongly typed as possible, and exposes serde’s internal buffering API in a way that formats can overwrite it and avoid the problems with, e.g. untagged enums.This bug makes it impossible to use
palettecrate’s types with ron, as it uses struct flattening everywhere.