Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pretty-print with serde? #361

Closed
troelsarvin opened this issue Feb 17, 2022 · 6 comments · Fixed by #490
Closed

How to pretty-print with serde? #361

troelsarvin opened this issue Feb 17, 2022 · 6 comments · Fixed by #490
Assignees
Labels
enhancement help wanted serde Issues related to mapping from Rust types to XML

Comments

@troelsarvin
Copy link

troelsarvin commented Feb 17, 2022

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(())
}
@Mingun
Copy link
Collaborator

Mingun commented May 21, 2022

Pretty-printing not supported yet, PR is welcome. Be noted, however, that I plan a big rework of a serializer in the coming months

@Mingun Mingun added enhancement help wanted serde Issues related to mapping from Rust types to XML labels May 21, 2022
@Mingun Mingun self-assigned this Sep 7, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252 - ok
Fixes tafia#280 - ok
Fixes tafia#287 - ok
Fixes tafia#343 - ok
Fixes tafia#346 - not ok
Fixes tafia#361 - ok
Partially addresses tafia#368
Fixes tafia#429 - ok
Fixes tafia#430 - ok
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430
@Mingun
Copy link
Collaborator

Mingun commented Oct 2, 2022

When #490 will be merged, pretty-print will work

Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 24, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 24, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 25, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 25, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 26, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
@Mingun Mingun closed this as completed in bd0b792 Oct 26, 2022
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
@gdesmott
Copy link

When #490 will be merged, pretty-print will work

Can you please explain how it's meant to work? I didn't find any API for pretty-print serialization.

thanks!

@Mingun
Copy link
Collaborator

Mingun commented Apr 12, 2023

Create Serializer and setup indent

@noguxun
Copy link

noguxun commented Apr 15, 2023

For anyone who needs a quick example of adding indentation when do serializing

    use serde::Serialize;
    use quick_xml::se::Serializer;

    #[derive(Debug, PartialEq, Serialize)]
    struct Struct {
        question: String,
        answer: u32,
    }

    let data = Struct {
        question: "The Ultimate Question of Life, the Universe, and Everything".into(),
        answer: 42,
    };

    let mut buffer = String::new();
    let mut ser = Serializer::new(&mut buffer);
    ser.indent(' ', 2);

    data.serialize(ser).unwrap();

    println!("{buffer}");

@Mingun
Copy link
Collaborator

Mingun commented Apr 15, 2023

@noguxun, feel free to make a PR that improves documentation (example for indent and maybe a mention in Serializer doc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants