Comments (7)
(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.
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.
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.
@mathstuf Unfortunately, attributes are handled by serde_derive, not something specific deserializers can add or control (easily at least).
from serde-xml-rs.
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.
Pinging @dtolnay in case you have any input here (in case a serde_derive change is feasible).
from serde-xml-rs.
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)
- Wrong serialization of Vec<Enum> HOT 2
- Custom { field: "missing field `soap:Body`" } when colon used
- LastElementNameNotAvailable error on writing HOT 11
- Decoding 0 elements in to a Vec gives "custom: missing field `$value`" error HOT 2
- Deserialization with custom deserializer does not work according to serde spec HOT 1
- Is there something like "from_slice"
- custom: missing field `id`
- Expose EmitterConfig/Make document declaration optional
- readme.md example broken out of the box
- readme.md example broken out of the box
- Can't serialize a struct with error 'LastElementNotAvailable'
- Error `DocumentStartAlreadyEmitted` while trying to serialize simple structure in 0.6.0
- Hoot serialize into attributes HOT 2
- Cannot add attribute id
- duplicate field `$value`'
- Does anyone have an improved fork or does any better alternative exist?
- You've found a bug in xml-rs, caused by calls to push_pos() in states that don't end up emitting events
- Regression : Fix xml-rs depencency version in Cargo.toml
- error order when serde a enum field
- Parse error whlist parsing hackernoon xml feed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serde-xml-rs.