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

Box<RawValue> and #[serde(flatten)] don't work together for deserialization #1099

Closed
tbu- opened this issue Jan 3, 2024 · 3 comments
Closed

Comments

@tbu-
Copy link

tbu- commented Jan 3, 2024

See #883, which was mistakenly closed as being a duplicate of #1051.

@dtolnay
Copy link
Member

dtolnay commented Jan 3, 2024

As far as I can tell, this is a duplicate of #1051. That issue is about Box<RawValue> and #[serde(flatten)] not working together for deserialization when Box<RawValue> appears somewhere inside the flattened data structure. Could you please explain what aspect of it is not duplicated?

@tbu-
Copy link
Author

tbu- commented Jan 3, 2024

In the example you gave,

#[derive(Deserialize, Debug)]
struct Repro {
    id: String,
    #[serde(flatten)]
    value: Box<RawValue>,
}

I find it clear that the RawValue can't work.

If there's an indirection (another struct) between #[serde(flatten)] and the Box<RawValue> though, it should be possible to make it work, e.g. in the example of #883, we have the following:

#![allow(dead_code)]

use serde::Deserialize;
use std::error::Error;

#[derive(Deserialize)]
struct Abc {
    #[serde(flatten)]
    pub def: Def,
}

#[derive(Deserialize)]
struct Def {
    pub ghi: Box<serde_json::value::RawValue>,
}

fn main() -> Result<(), Box<dyn Error>> {
    let _: Def = serde_json::from_str(r#"{"ghi": {}}"#).unwrap();
    let _: Abc = serde_json::from_str(r#"{"ghi": {}}"#).unwrap();
    Ok(())
}

As we can see, the "raw json string" of the "ghi" key will always be contiguous, so it should™ be possible to create the RawValue.

Upon re-reading #1051, I can now see how the issues are closer than I thought, so I see why it was marked as duplicate. I feel the reasoning given there doesn't apply here though. It makes sense that a raw value doesn't work if directly marked as #[serde(flatten)].

@dtolnay
Copy link
Member

dtolnay commented Jan 3, 2024

I think the indirection is incidental to the reason this isn't working, so they are duplicates. It is possible to do it differently but that is tracked by serde-rs/serde#1183.

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

No branches or pull requests

2 participants