Code Monkey home page Code Monkey logo

Comments (3)

RReverser avatar RReverser commented on May 28, 2024

Yeah I'm afraid you'll have to manually capture extra fields like here: https://serde.rs/attr-flatten.html#capture-additional-fields

The reason is that, unlike flatten, deny_unknown_fields doesn't switch deserialization from structs to maps, and we have no way of knowing whether Serde will want those extra fields or not.

Being able to pick only known properties from JS object instead of iterating over its all key-value pairs as a map is a significant perf optimisation, and I wouldn't want to remove it for the relatively rare attribute.

Ideally Serde upstream should switch to deserialize_map when deny_unknown_fields is used, but I'm not sure if they'd be open to do so.

from serde-wasm-bindgen.

RReverser avatar RReverser commented on May 28, 2024

The easiest solution for now might be to use a helper like this:

fn deserialize_deny_unknown_fields<'de, T: Deserialize<'de>>(value: JsValue) -> Result<T, serde_wasm_bindgen::Error> {
    #[derive(Deserialize)]
    struct Wrap<T> {
        #[serde(flatten)]
        result: T,
        #[serde(flatten)]
        extra_fields: HashMap<String, serde::de::IgnoredAny>,
    }

    let Wrap {
        result,
        extra_fields,
    } = serde_wasm_bindgen::from_value(value)?;
    if !extra_fields.is_empty() {
        return Err(serde::de::Error::custom(format_args!(
            "Unexpected fields: {:?}",
            extra_fields.keys().collect::<Vec<_>>()
        )));
    }
    Ok(result)
}

from serde-wasm-bindgen.

sergeyboyko0791 avatar sergeyboyko0791 commented on May 28, 2024

@RReverser thank you for the answer! Will follow your advice.

from serde-wasm-bindgen.

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.