quick-xml-to-json

Crates.ioquick-xml-to-json
lib.rsquick-xml-to-json
version0.1.0
created_at2025-08-11 02:45:16.801331+00
updated_at2025-08-11 02:45:16.801331+00
descriptionHigh-performance conversion of XML to JSON
homepage
repositoryhttps://github.com/joshuaclayton/quick-xml-to-json
max_upload_size
id1789492
size42,193
Josh Clayton (joshuaclayton)

documentation

README

quick-xml-to-json

Crates.io Documentation

High-performance XML to JSON converter built on top of quick-xml.

This crate provides a fast, memory-efficient way to convert XML documents to JSON format, leveraging quick-xml's high-performance XML parsing capabilities.

Features

  • High Performance: Built on quick-xml for maximum parsing speed
  • Memory Efficient: Minimal memory allocations with configurable buffer sizes
  • Streaming: Processes XML as a stream without loading entire documents into memory
  • Attribute Support: Preserves XML attributes in JSON output
  • Text Node Handling: Properly handles text content within elements
  • Error Handling: Comprehensive error types with thiserror

Installation

Add this to your Cargo.toml:

[dependencies]
quick-xml-to-json = "0.1.0"

Usage

Basic Conversion

use quick_xml_to_json::xml_to_json;

let xml = r#"<users count="3">
  <user age="40">Jane Doe</user>
  <user age="42">John Doe</user>
</users>"#;

let mut output = Vec::new();
xml_to_json(xml.as_bytes(), &mut output)?;

// output now contains the JSON bytes
let json_string = String::from_utf8(output)?;
println!("{}", json_string);

JSON Output Format

The crate converts XML to JSON using a specific format that preserves structure:

  • Attributes are prefixed with @ (e.g., @id="value")
  • Text content is stored under the #t key
  • Child elements are stored under the #c key as an array

Example XML:

<root id="main">
  <child>Hello World</child>
  <empty attr="value" />
</root>

Becomes:

{
  "root": {
    "@id": "main",
    "#c": [
      {
        "child": {
          "#t": "Hello World"
        }
      },
      {
        "empty": {
          "@attr": "value"
        }
      }
    ]
  }
}

Performance

This crate is designed for high-performance conversion of XML to JSON.

Benchmark Results

XML fixture files have been sourced from https://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/www/repository.html.

On a 12-core MacBook Pro M4, throughput benchmarks vary but sit between 215 MiB/s and 340 MiB/s with minimal RAM usage (~3 MiB).

Run benchmarks with:

cargo bench

Test Suite

Run the test suite with:

cargo test

Dependencies

  • quick-xml: High-performance XML parsing
  • serde: JSON serialization support
  • serde_json: JSON formatting
  • thiserror: Custom error types

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

Commit count: 0

cargo fmt