Skip to content

struct-based list can be serde-serialized as child but not as attribute #906

@Kakadus

Description

@Kakadus

Hi, cool project!

Consider these two types using serde (de)serialize:

use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FooType {
    #[serde(rename = "a-list")]
    pub a_list: ListType,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct ListType{
    #[serde(default, rename = "$text")]
    pub content: Vec<String>
}

This creates xml such as <test><a-list>A B</a-list></test>.

Now consider this slight adaptation:

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BarType {
    #[serde(default, rename = "@a-list")]
    pub a_list: ListType,
}

I expect this to create xml such as <test a-list="A B"/>, but it fails with

Unsupported("cannot serialize struct ListType as an attribute or text content value")

I know that if I change ListType to simply

type ListType = Vec<String>

the second case works. But then, the first case does not serialize correctly: <test><a-list>A</a-list><a-list>B</a-list></test>

Is there a way to define a single ListType in such a way that both

  • <test><a-list>A B</a-list></test>
  • <test a-list="A B"/>

can be serialized?
Thanks a lot!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysIssues related to mapping XML content onto arrays using serdeserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions