Giter Site home page Giter Site logo

Comments (3)

dtolnay avatar dtolnay commented on May 20, 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?

from json.

tbu- avatar tbu- commented on May 20, 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)].

from json.

dtolnay avatar dtolnay commented on May 20, 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.

from json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.