Skip to content

How to pretty-print with serde? #361

@troelsarvin

Description

@troelsarvin

Hello,

I have the code below; when it runs, it results in the following output:

seralized:
<osm><node visible="true" timestamp="2022-02-17T13:55:16.184553291Z"><tag k="key1" v="value1"/><tag k="key2" v="value2"/></node></osm>

I would like to print it in a prettyfied way, i.e. with indention and line breaks following the XML structure. How can that be done?

use serde::Serialize;
use quick_xml::se;
use chrono::{ DateTime, Utc };

#[derive(Serialize, Clone, Debug)]
#[serde(rename = "tag")]
#[serde(rename_all = "lowercase")]
struct OsmTag {
    k: String,
    v: String,
}

#[derive(Serialize, Clone, Debug)]
#[serde(rename = "node")]
#[serde(rename_all = "lowercase")]
struct OsmNode {
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<u64>,
    visible: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    version: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    changeset: Option<u64>,
    timestamp: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    uid: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    lat: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    lon: Option<f64>,
    tag: Vec<OsmTag>,
}

impl Default for OsmNode {
    fn default() -> OsmNode {
        OsmNode {
            id: None,
            visible: true,
            version: None,
            changeset: None,
            timestamp: Utc::now(),
            user: None,
            uid: None,
            lat: None,
            lon: None,
            tag: Vec::new()
        }
    }
}

#[derive(Serialize, Debug)]
#[serde(rename = "osm")]
#[serde(rename_all = "lowercase")]
struct OsmNodeContainer {
    node: OsmNode,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut on = OsmNode { ..Default::default() };
    let t1 = OsmTag { k: String::from("key1"), v: String::from("value1") };
    let t2 = OsmTag { k: String::from("key2"), v: String::from("value2") };
    on.tag.push(t1.clone());
    on.tag.push(t2.clone());
    let onc = OsmNodeContainer { node: on.clone(), };
    let ser = se::to_string(&onc)?;
    println!("seralized:");
    println!("{}", ser);

    Ok(())
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions