How to 'flatten' a vector to produce multiple XML elements when using serde?

1k views Asked by At

I have the following structs:

struct Artist {
    name: String,
    image: String,
}

struct Album {
    title: String,
    artists: Vec<Artist>,
}

I need to produce XML that looks like:

<album>
  <title>Some title</title>
  <artist>
    <name>Bonnie</name>
    <image>http://example.com/bonnie.jpg</image>
  </artist>
  <artist>
    <name>Cher</name>
    <image>http://example.com/cher.jpg</image>
  </artist>
</album>

How can I use serde to serialise/deserialise to the above XML format, specifically regarding flattening the artists vector to produce multiple artist elements? This is a third-party format I can't change :-(

1

There are 1 answers

1
dtolnay On

The serde-xml-rs crate is the replacement for the deprecated serde_xml crate and supports serializing data structures to XML in the representation you want.

extern crate serde;
extern crate serde_xml_rs;

use serde::ser::{Serialize, Serializer, SerializeMap, SerializeStruct};

struct Artist {
    name: String,
    image: String,
}

impl Serialize for Artist {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        let mut map = serializer.serialize_map(Some(2))?;
        map.serialize_entry("name", &self.name)?;
        map.serialize_entry("image", &self.image)?;
        map.end()
    }
}

struct Album {
    title: String,
    artists: Vec<Artist>,
}

impl Serialize for Album {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        let len = 1 + self.artists.len();
        let mut map = serializer.serialize_struct("album", len)?;
        map.serialize_field("title", &self.title)?;
        for artist in &self.artists {
            map.serialize_field("artist", artist)?;
        }
        map.end()
    }
}

fn main() {
    let album = Album {
        title: "Some title".to_owned(),
        artists: vec![
            Artist {
                name: "Bonnie".to_owned(),
                image: "http://example.com/bonnie.jpg".to_owned(),
            },
            Artist {
                name: "Cher".to_owned(),
                image: "http://example.com/cher.jpg".to_owned(),
            },
        ],
    };

    let mut buffer = Vec::new();
    serde_xml_rs::serialize(&album, &mut buffer).unwrap();
    let serialized = String::from_utf8(buffer).unwrap();
    println!("{}", serialized);
}

The output is:

<album>
  <title>Some title</title>
  <artist>
    <name>Bonnie</name>
    <image>http://example.com/bonnie.jpg</image>
  </artist>
  <artist>
    <name>Cher</name>
    <image>http://example.com/cher.jpg</image>
  </artist>
</album>