-
Notifications
You must be signed in to change notification settings - Fork 97
attributes serialization #49
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
Comments
Yea attributes are a weird thing. I'm not sure how to handle them. We could rename fields to something like |
Yep I think like in every library who manage XML ... It's still the same problem of naming. |
I agree, also was thinking about this approach to ensure roundtrips. Could avoid most annotations by using something like |
You mean to require adding |
@oli-obk I mean to avoid any annotations and instead use prefixes for struct fields (which is essentially the same, just perhaps more convenient). |
hmm.. I don't like that, because it exposes xml stuff to users of the struct who don't even care about xml |
Maybe... although author can still add annotations to rename it if it's a public structure / fields and not just for internal usage. |
Probably you already know of it, but here there's a nice overview of some of the most common mapping conventions for attributes (and namespaces). The main problem is, there's all kind of conventions. Maybe a plugin system to allow choosing one's preferred method would be best? |
I think an annotation would be a good choice. Maybe |
Serde has no support for such features. |
We'll also need to enforce that attribute-fields come before regular fields, because the serializer cannot look ahead. This is probably only doable with a runtime error though. |
So, may be like $value, but more complex |
@anton-dutov Wouldn't that also affect json serializaton because it's |
@Boscop Just for case where only xml (seri/deseri)lization used, on other cases modification of serde required
|
it's interesting @anton-dutov, in this formulation |
Continue discussion in #62 |
Hello guys, is this feature supported now? |
For anyone here in the future, this is implemented at least I am on
Anything renamed with the @ will map to an attribute, otherwise they will be treated as a child component. |
Note that the use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_str, to_string};
#[derive(Debug, Serialize, Deserialize)]
pub struct Control {
name: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Layout {
#[serde(rename(serialize = "@height"))]
pub height: String,
#[serde(rename(serialize = "@width"))]
pub width: String,
#[serde(rename = "control")]
pub control: Control,
}
fn main() {
let xml = r#"
<Layout height="3" width="5">
<control>
<name>Hello World!</name>
</control>
</Layout>
"#;
let layout: Layout = from_str(xml).unwrap();
println!("{layout:#?}");
println!("{}", to_string(&layout).unwrap());
} will output:
|
Is there a way to serialize a HashMap to a series of Attributes? I can get the attributes deserialized into a HashMap.
But, no clue how to serialize that. Here's what I have in code:
Any ideas? I'm guessing it is actually
Kinda curious why it is trying to handle a PlainStringSerializer. |
I'm also facing similar issue as well. After getting such error, I attempted to narrow down the reason by simplify my testing code, and then I got another error that is identical to #186. By reading the comment in that issue (#186 (comment)), I found out If we go with this workaround, all we need to do is:
HTH, and hope this issue can be addressed as well so we don't need yet another crate to workaround this issue. |
@BLumia, version 0.6.0 of this crate added better serialization. In order to specify that a field should be serialized as an attribute, you have to rename the field so that it starts with For instance #[derive(Serialize, Deserialize)]
struct Document {
#[serde(rename = "@a")]
a: String,
#[serde(rename = "@b")]
b: i32,
#[serde(rename = "@c")]
c: (),
}
let value = Document {
a: "abc".to_string(),
b: 123,
c: (),
}; The value serializes as <?xml version="1.0" encoding="UTF-8"?><Document a="abc" b="123" c="" /> |
I'm pretty confused by this reply. My local project is created at yesterday and I was already using 0.6.0 (or 0.7.0, can't recall what's the actual version). I am aware this can be done with a I didn't open an new issue for the bug I'm facing because of...
Because of it, I don't think it's worth to create a new issue before #186 gets addressed. |
You probably forget I was replying a person that have an identical issue that I was facing. Search engine brought me here so I am replying here to help that person and other people might having the same issue as I did (and might also end up here because of the search engine). I also linked to #186 because of at least #186 is the most similar issue out there.
I won't unless #186 gets addressed, again, because:
Thus, unless #186 gets addressed, it worth nothing to open another issue. I think I get my point clear. If you still don't know why I originally replied to #49 (comment), I simply give up and will stop explaining the same thing once and once again here, since the source cause is off-topic to this main issue. |
How to serialise attributes on XML items ?
I d'ont see anything for that. Any suggestion ?
The text was updated successfully, but these errors were encountered: