xml2json_rs/lib.rs
1//! # XML2JSON
2//!
3//! A library for converting to and from XML and JSON.
4//!
5//! # JSON to XML
6//!
7//! ## XmlBuilder
8//! [`XmlBuilder`] builds a XML from JSON.
9//! - [`build_from_json`] builds an XML `String` from a [`serde_json::Value`]
10//! - [`build_from_json_string`] builds an XML `String` from a serialized JSON `String`.
11//!
12//! ### Example
13//! ```rust
14//! use xml2json_rs::XmlBuilder;
15//! use std::error::Error;
16//!
17//! fn main() -> Result<(), Box<dyn Error>> {
18//! let mut xml_builder = XmlBuilder::default();
19//! let xml= xml_builder.build_from_json_string(r#"{"book":{"$":{"category":"fantasy"},"title":[{"_":"The Name of the Wind","$":{"lang":"en"}}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#)?;
20//! assert_eq!(xml, r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#);
21//! Ok(())
22//! }
23//! ```
24//!
25//! ## XmlConfig
26//! [`XmlConfig`] Uses the [Builder] pattern to set configuration options and then `finalize` to
27//! build an [`XmlBuilder`]
28//!
29//! ### Example
30//!
31//! ```rust
32//! use xml2json_rs::XmlConfig;
33//! use xml2json_rs::{ Indentation, Declaration, Version, Encoding };
34//! use std::error::Error;
35//! use indoc::indoc;
36//!
37//! fn main() -> Result<(), Box<dyn Error>> {
38//! let mut xml_builder = XmlConfig::new()
39//! .rendering(Indentation::new(b' ', 2))
40//! .decl(Declaration::new(Version::XML10, Some(Encoding::UTF8), Some(true)))
41//! .attrkey("^")
42//! .root_name("store")
43//! .finalize();
44//!
45//! let xml = xml_builder.build_from_json_string(r#"{"book":{"^":{"category":"fantasy"},"title":[{"_":"The Name of the Wind","^":{"lang":"en"}}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#)?;
46//! assert_eq!(xml, indoc!(r#"
47//! <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
48//! <store>
49//! <book category="fantasy">
50//! <title lang="en">The Name of the Wind</title>
51//! <author>Patrick Rothfuss</author>
52//! <year>2007</year>
53//! </book>
54//! </store>"#));
55//! Ok(())
56//! }
57//! ```
58//! ## XML to JSON
59//!
60//! ### JsonBuilder
61//! [`JsonBuilder`] builds JSON from XML.
62//! - [`build_from_xml`] build a [`serde_json::Value`] from an XML `String`.
63//! - [`build_string_from_xml`] build a JSON serialized `String` from an XML `String`.
64//! - [`build_pretty_string_from_xml`] build a pretty-printed JSON serialized `String` from an XML
65//! `String`
66//!
67//! ## Example
68//!
69//! ```rust
70//! use xml2json_rs::JsonBuilder;
71//! use std::error::Error;
72//!
73//! fn main() -> Result<(), Box<dyn Error>> {
74//! let json_builder = JsonBuilder::default();
75//! let json = json_builder.build_string_from_xml(r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#)?;
76//! assert_eq!(json, r#"{"book":{"$":{"category":"fantasy"},"title":[{"$":{"lang":"en"},"_":"The Name of the Wind"}],"author":["Patrick Rothfuss"],"year":["2007"]}}"#);
77//! Ok(())
78//! }
79//! ```
80//!
81//! ### JsonConfig
82//! [`JsonConfig`] Uses the [Builder] pattern to set configuration options and then `finalize` to
83//! build an [`JsonBuilder`]
84//!
85//! ## Example
86//!
87//! ```rust
88//! use xml2json_rs::JsonConfig;
89//! use std::error::Error;
90//!
91//! fn main() -> Result<(), Box<dyn Error>> {
92//! let json_builder = JsonConfig::new()
93//! .ignore_attrs(true)
94//! .explicit_array(false)
95//! .finalize();
96//! let json = json_builder.build_string_from_xml(r#"<?xml version="1.0"?><book category="fantasy"><title lang="en">The Name of the Wind</title><author>Patrick Rothfuss</author><year>2007</year></book>"#)?;
97//! assert_eq!(json, r#"{"book":{"title":"The Name of the Wind","author":"Patrick Rothfuss","year":"2007"}}"#);
98//! Ok(())
99//! }
100//! ```
101//!
102//! [`XmlBuilder`]: struct.XmlBuilder.html
103//! [`XmlConfig`]: struct.XmlConfig.html
104//! [`serde_json::Value`]: https://docs.serde.rs/serde_json/value/enum.Value.html
105//! [`build_pretty_string_from_xml`]: struct.JsonBuilder.html#method.build_pretty_string_from_xml
106//! [`build_string_from_xml`]: struct.JsonBuilder.html#method.build_string_from_xml
107//! [`build_from_xml`]: struct.JsonBuilder.html#method.build_from_xml
108//! [`build_from_json_string`]: struct.XmlBuilder.html#method.build_from_json_string
109//! [`build_from_json`]: struct.XmlBuilder.html#method.build_from_json
110//! [`JsonConfig`]: struct.JsonConfig.html
111//! [`JsonBuilder`]: struct.JsonBuilder.html
112//! [Builder]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
113
114#![deny(missing_docs)]
115
116extern crate quick_xml;
117extern crate regex;
118
119#[macro_use]
120extern crate lazy_static;
121
122mod json;
123mod xml;
124
125pub use json::{JsonBuilder, JsonConfig};
126
127pub use xml::{Declaration, Encoding, Indentation, Version, XmlBuilder, XmlConfig};
128
129pub use error::Error as X2JError;
130
131mod error;
132mod utils;