XML2JSON

Tests License License

A rust library for converting to and from XML and JSON.

Install

bash λ › cargo add xml2json-rs

Usage

JSON to XML

XmlBuilder

XmlBuilder builds XML from JSON. - build_from_json builds an XML String from a serde_json::Value. - build_from_json_string builds an XML String from a serialized JSON String.

Example

```rust use xml2json::XmlBuilder; use std::error::Error;

fn main() -> Result<(), Box> { let mut xmlbuilder = XmlBuilder::default(); let xml= xmlbuilder.buildfromjsonstring(r#"{"book":{"$":{"category":"fantasy"},"title":{"":"The Name of the Wind","$":{"lang":"en"}},"author":"Patrick Rothfuss","year":"2007"}}"#)?; assert_eq!(xml, r#"The Name of the WindPatrick Rothfuss2007"#); Ok(()) } ```

XmlConfig

XmlConfig Uses the Builder pattern to set configuration options and then finalize to build an XmlBuilder.

Example

```rust use xml2json::XmlConfig; use xml2json::{ Indentation, Declaration, Version, Encoding }; use std::error::Error; use indoc::indoc;

fn main() -> Result<(), Box> { let mut xmlbuilder = XmlConfig::new() .rendering(Indentation::new(b' ', 2)) .decl(Declaration::new(Version::XML10, Some(Encoding::UTF8), Some(true))) .attrkey("^") .root_name("store") .finalize();

let xml = xmlbuilder.buildfromjsonstring(r#"{"book":{"^":{"category":"fantasy"},"title":{"":"The Name of the Wind","^":{"lang":"en"}},"author":"Patrick Rothfuss","year":"2007"}}"#)?; asserteq!(xml, indoc!(r#" The Name of the Wind Patrick Rothfuss 2007 "#)); Ok(()) }

```

XML to JSON

JsonBuilder

JsonBuilder builds JSON from XML. - build_from_xml build a serde_json::Value from an XML String. - build_string_from_xml build a JSON serialized String from an XML String. - build_pretty_string_from_xml build a pretty-printed JSON serialized String from an XML String.

Example

```rust use xml2json::JsonBuilder; use std::error::Error;

fn main() -> Result<(), Box> { let jsonbuilder = JsonBuilder::default(); let json = jsonbuilder.buildstringfromxml(r#"The Name of the WindPatrick Rothfuss2007"#)?; asserteq!(json, r#"{"book":{"$":{"category":"fantasy"},"title":{"$":{"lang":"en"},"_":"The Name of the Wind"},"author":"Patrick Rothfuss","year":"2007"}}"#); Ok(()) } ```

JsonConfig

JsonConfig Uses the Builder pattern to set configuration options and then finalize to build an JsonBuilder.

Example

```rust use xml2json::JsonConfig; use std::error::Error;

fn main() -> Result<(), Box> { let jsonbuilder = JsonConfig::new() .ignoreattrs(true) .explicitarray(false) .finalize(); let json = jsonbuilder.buildstringfromxml(r#"The Name of the WindPatrick Rothfuss2007"#)?; asserteq!(json, r#"{"book":{"title":"The Name of the Wind","author":"Patrick Rothfuss","year":"2007"}}"#); Ok(()) } ```

Objective

This library was inspired by node-xml2json and has a primary objective of maintaining parity with its 0.4.20 version.

Tests

Integration tests are generated via scripts using node-xml2json in order to verify parity.

Generating

bash λ › cd tests/generator λ › yarn λ › ./generate

Running

bash λ › cargo test