#json-xml #json #xml #conversion

xml2json-rs

A library for converting to and from XML/JSON

2 stable releases

1.0.3 Jul 14, 2022
1.0.1 Aug 11, 2022

#1007 in Encoding

Download history 874/week @ 2024-06-19 390/week @ 2024-06-26 752/week @ 2024-07-03 1196/week @ 2024-07-10 866/week @ 2024-07-17 769/week @ 2024-07-24 856/week @ 2024-07-31 607/week @ 2024-08-07 643/week @ 2024-08-14 622/week @ 2024-08-21 449/week @ 2024-08-28 459/week @ 2024-09-04 515/week @ 2024-09-11 558/week @ 2024-09-18 595/week @ 2024-09-25 358/week @ 2024-10-02

2,072 downloads per month
Used in 4 crates (via br-xml)

MIT license

49KB
970 lines

XML2JSON

Tests License License

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

Install

λ › 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

use xml2json::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

use xml2json::XmlConfig;
use xml2json::{ 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)))
    .attr_key("^")
    .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

use xml2json::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

use xml2json::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(())
}

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

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

Running

λ › cargo test

Dependencies

~4.5–6MB
~106K SLoC