-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
Internal buffering disrupts format-specific deserialization features #1183
Comments
There are some other JSON-specific assumptions baked into I suspect we will need a format-specific optional API on We should start by identifying all the ways |
This is complicated by associated type defaults being unstable rust-lang/rust#29661. I thought we might want a format-specific buffer type as a Deserializer associated type with the default being |
Here is a backdoor way to add an associated type. struct Content;
trait Deserializer {
// Suppose we want something like this but associated type defaults are unstable.
type Buffer = Content;
} trait Deserializer {
// Instead write it as an associated function that forwards to a generic callback.
fn deserialize_with_buffering<F>(callback: F) -> F::Value
where
F: DeserializeWithBuffer,
{
callback.run::<Content>()
}
}
trait DeserializeWithBuffer {
type Value;
fn run<Buffer>(self) -> Self::Value;
} |
Is there any update on this? |
Customizing the buffer type by Deserializer could provide a powerful alternative to #1327. The key constraint being satisfied in that approach (as compared to just stashing state in some thread local) was to accurately expose state during deserialization of untagged, internally tagged, and flattened members. If a Deserializer can replace the buffer type, a library could provide a stateful Deserializer built on TLS by wrapping any existing Deserializer and wrapping that Deserializer's buffer type to correctly track the state. |
I implemented a proof of concept in #1354. |
Let's give this another attempt as associated type defaults (rust-lang/rust#29661) approaches stabilization. I believe the workaround without associated type defaults in #1354 could have been made to work but would have been too complicated to maintain. |
Is it possible that this issue explains the behavior described in nox/serde_urlencoded#66? In that case, the author is trying to deserialize the key-value pair |
Yeah that looks like a consequence of this issue. |
@dtolnay Since return impl trait in traits will now be stabilised soon, do you think that using it to solve this issue would be an avenue worth pursuing? |
Any update on this matter? Anything we can do to assist? |
I'll repeat the last comment:
|
Was not sure if I should file this against
serde_json
or here. As far as I can tell the bug is more likely in serde itself. While implementing #1179 I noticed that non string keys are not supported for flattening. However the same behavior happens if buffering happens for internally tagged enums:Example that shows the problem:
This currently fails with this error:
However a
HashMap<u32, ()>
is otherwise entirely permissible byserde_json
.The text was updated successfully, but these errors were encountered: