Giter VIP home page Giter VIP logo

Comments (7)

mathstuf avatar mathstuf commented on May 26, 2024

(This may be a core serde issue rather than an XML-specific one, but the toggle of behavior based on the presence of a $value field is in this crate.)

from serde-xml-rs.

RReverser avatar RReverser commented on May 26, 2024

Hmm, yeah, I don't think this is possible right now without custom deserializer for that struct. I'll look into simplifying this use-case if possible.

from serde-xml-rs.

mathstuf avatar mathstuf commented on May 26, 2024

Thanks. I wonder if something like a #[serde(keys=["kind_a", "kind_b"])] attribute might make sense. Or to have some way to decorate the enum with a "tagged by key in the containing container".

from serde-xml-rs.

RReverser avatar RReverser commented on May 26, 2024

@mathstuf Unfortunately, attributes are handled by serde_derive, not something specific deserializers can add or control (easily at least).

from serde-xml-rs.

mathstuf avatar mathstuf commented on May 26, 2024

Agreed. The latter one probably makes more sense for such a level. The former sounds too XML-specific (since most other formats don't support multikey hashes).

from serde-xml-rs.

mathstuf avatar mathstuf commented on May 26, 2024

Pinging @dtolnay in case you have any input here (in case a serde_derive change is feasible).

from serde-xml-rs.

jonasbb avatar jonasbb commented on May 26, 2024

I found a way to make this example work. The code is based on this incomplete PR of me jonasbb/serde_with#375.
The additional deser_value_field function is necessary to 1) signal that the value of the tag should be parsed by using the rename to $value and 2) to parse the value from a String. Presumably, the flatten makes all values turn into a String (likely due to serde-rs/serde#1183), so the code needs to make the String to desired type transformation explicit.

use std::str::FromStr;
use std::fmt::Display;

fn deser_value_field<'de, D: Deserializer<'de>, T>(deserializer: D) -> Result<T, D::Error>
where
    T: FromStr,
    T::Err: Display,
{
    #[serde_as]
    #[derive(Deserialize)]
    struct Field<T> {
        #[serde_as(as = "DisplayFromStr")]
        #[serde(rename = "$value", bound = "T: FromStr, T::Err: Display")]
        field: T
    }
    
    Ok(Field::deserialize(deserializer)?.field)
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case")]
enum AnEnum {
    KindA(
        #[serde(deserialize_with = "deser_value_field")]
        String
    ),
    KindB(
        #[serde(deserialize_with = "deser_value_field")]
        i32
    ),
}

#[serde_as]
#[derive(Debug, Deserialize)]
struct MyStruct {
    attr: String,
    required_field: String,
    #[serde(flatten)]
    #[serde_as(as = "EnumMap")]
    kinds: Vec<AnEnum>,
}

from serde-xml-rs.

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.