Deserializing tag with attribute values into Map

Open
#383 3 comments 2 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

Start by reading the deserialization handling for internally tagged enums and $value fields, then trace how maps and sequences are populated from XML elements. The issue provides a reproducer with AttrDefChoice, AttrDef, and MultiMap; check whether the map can accept element values alongside the name attribute and add a regression case for that input. Done means the example deserializes successfully into a map, with existing behavior preserved.

Written by the indexing model from the issue text.

Description

help wanted serde

Assume we have below xml structure:

<container>
  <attr>
    <name>Name</name>
    <desc>Description</desc>
  </attr>
</container>

when <attr> tag doesn't have attribute values, quick-xml parses successfully into Map (HashMap, MultiMap)
Container ( "attr" : { Name(Name), Desc(Description) } )

#[derive(Deserialize, PartialEq)]
pub enum AttrDefChoice {
    Name(String),
    Desc(String),
}

#[derive(Deserialize, PartialEq)]
pub enum ContainerDef {
    Container(MultiMap<String, AttrDefChoice>),
}

but when <attr> have name value I need some wrapper struct with rename = "$value". In that case it works only with attr: Vec<AttrDefChoice>.

<container>
  <attr name="AttrName">
    <name>Name</name>
    <desc>Description</desc>
  </attr>
</container>
...

#[derive(Deserialize, PartialEq)]
pub struct AttrDef {
    name: String,

    #[serde(rename = "$value")]
    attr: MultiMap<String, AttrDefChoice>,
}

invalid type: string "Name", expected internally tagged enum AttrDefChoice

Is there any way to achieve parsing of tags with values into Map?

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.