Create attribute without using @ for compatibility with json serialization

Open
#582 7 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
backend

Research direction

Read the serde serialization and deserialization code in quick-xml to find how XML attributes are distinguished from element fields, then check the existing tests for XML and JSON interoperability. The issue asks for an attribute marker that preserves the field name in JSON; done means the same struct serializes with the desired XML attribute and the unmodified JSON field name.

Written by the indexing model from the issue text.

Description

question serde

I have a data structure with an attribute:

#[derive(Debug, Serialize, Deserialize)]
pub struct Foo {
  #[serde(rename = "@bar")]
  pub bar: String,
}

Which (de)serializes to:

<foo bar="blabla"/>

For this to work I had to add @. However, I need to (de)serialize in XML and json. So now, in json, the field appear as @bar:

{
  "foo": {
    "@bar": "blabla",
  },
}

And that's a problem for me. Is there a way to mark a field as attribute without modifying its name?

Dominant language
Rust
Stars
1.6k
Forks
299
Avg merge
2d 4h
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tafia/quick-xml

All issues in tafia/quick-xml

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.