mitsuhiko / deser

Experimental rust serialization library
https://docs.rs/deser
Apache License 2.0
287 stars 8 forks source link

Add support for `#[deser(flatten)]` #9

Closed jonasbb closed 2 years ago

jonasbb commented 2 years ago

Hi, I am not sure if that is the correct way for feedback. I could not find if this is a feature being addressed or if there is a plan for providing something similar in deser. At least, I want to contribute it as a point to be aware of.

serde has a long-standing issue about internal buffering which interacts with a lot features and tends to break deserialization unexpectedly: https://github.com/serde-rs/serde/issues/1183 (Example) Internal buffering is used to implement the flatten attribute, untagged and adjacently tagged enums.

The problem in the example is that serde_json "knows" how to deserialize a HashMap<i64, i64> (by treating string keys as numbers), but due to buffering it turns into a Map<String, i64> which then cannot be deserialized. The problem is also that the Inner type works well on its own, but by being included in the Outer type it starts failing.

mitsuhiko commented 2 years ago

So far it doesn't but this issue is one I want to try to address. Generally internal buffering will absolutely be necessary to address this issue but internal buffering is tricky. My general goal is to have an extensible internal data model but I'm not sure yet what the best approach here is.

Both https://github.com/serde-rs/serde/issues/1183 and https://github.com/serde-rs/serde/issues/1463 are issues I hit with serde and are tricky to address in its current design but I want to try to address here.

mitsuhiko commented 2 years ago

Basic support for this landed now but I am not entirely convinced yet that the approach here is the best one. That said, I did chose to implement flattening by "reaching" into the inner deserializer without buffering, which should be less error prone.

mitsuhiko commented 2 years ago

I'm closing this as resolved for now, but I am sure that there will be multiple iterations about this before this feature can be considered implemented.