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