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

Deserializing a RawValue inside an untagged enum always fails #497

Closed
untitaker opened this issue Oct 9, 2018 · 3 comments
Closed

Deserializing a RawValue inside an untagged enum always fails #497

untitaker opened this issue Oct 9, 2018 · 3 comments
Labels

Comments

@untitaker
Copy link

cargo script compatible example:

//! ```cargo
//! [dependencies]
//! serde = "1.0.79"
//! serde_json = { version = "1.0.32", features = ["raw_value"] }
//! serde_derive = "1.0.79"
//! ```

#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate serde_json;

#[derive(Debug, Deserialize, Serialize)]
struct FooNewtype<T>(T);

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum FooEnum<T> {
    Single(T)
}

fn main() {
    // works
    let val: FooNewtype<Box<serde_json::value::RawValue>> = serde_json::from_str("42").unwrap();
    println!("{:?}", val);

    // thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("data did not match any variant of untagged enum FooEnum", line: 0, column: 0)', libcore/result.rs:945:5
    let val: FooEnum<Box<serde_json::value::RawValue>> = serde_json::from_str("42").unwrap();
    println!("{:?}", val);
}

I haven't figured out what's causing this yet.

@untitaker
Copy link
Author

Ok, I think I get it now. RawValue assumes the Deserializer from serde-json but untagged enums use their own deserializer.

@dtolnay Do you have opinions on putting deserialize_raw onto the Deserialize trait (with a default impl that errors)? It's the first fix that comes to mind

@dtolnay
Copy link
Member

dtolnay commented Oct 11, 2018

Right, untagged enums are deserialized from a data structure that has been buffered into heap memory, not from the original JSON deserializer.

I don't think a deserialize_raw method is the right fix. The underlying limitation is the same as serde-rs/serde#1183. Both issues would be better resolved by supporting a customizable buffer type per deserializer (proof of concept in serde-rs/serde#1354).

@dtolnay
Copy link
Member

dtolnay commented Aug 20, 2021

Closing in favor of serde-rs/serde#1183 since this issue is a direct consequence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants