quick-xml-to-json
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:
[]
= "0.1.0"
Usage
Basic Conversion
use 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 = Vecnew;
xml_to_json?;
// output now contains the JSON bytes
let json_string = Stringfrom_utf8?;
println!;
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
#tkey - Child elements are stored under the
#ckey as an array
Example XML:
Hello World
Becomes:
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:
Test Suite
Run the test suite with:
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.