#json-format #json-xml #xml #json #xml2json #xml-to-json #xml-to-string

rsxml2json

A Rust library for converting XML to JSON format

2 releases

Uses old Rust 2015

0.1.1 Jul 9, 2024
0.1.0 Jun 2, 2024

#11 in #json-xml

46 downloads per month

MIT license

10KB
149 lines

rsxml2json

A Rust library for converting XML to JSON format

Dependencies:

use rsxml2json::{Convert, ConvertConfig};

Usage

Example Code 1: Returning String

use rsxml2json::{Convert, ConvertConfig};

fn main() {
    //init
    let convert = Convert::new(ConvertConfig::default());
    //xml data
    let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>"#.to_string();
    
    let data_option = convert.execute(xml_str);
    let json_str = match data_option {
        Ok(value) => value,
        Err(_) => return,
    };
    
    println!("json_str = {}",json_str);
}

Example Code 2: Returning serde_json::Value

use rsxml2json::{Convert, ConvertConfig};
use serde_json;

fn main() {
    // Initialize
    let convert = Convert::new(ConvertConfig::default());
    // XML data
    let xml_str = r#"<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>"#.to_string();
    
    let data_option = convert.execute_json(xml_str);
    let json_value = match data_option {
        Ok(value) => value,
        Err(err) => {
            println!("Error: {:?}", err);
            return;
        }
    };
    
    let pretty_json = serde_json::to_string_pretty(&json_value).expect("Unable to convert to pretty JSON");
    println!("pretty_json = {}", pretty_json);
}

Input

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<foo>bar</foo>
</osm>

Output

{
  "osm": {
    "-generator": "CGImap 0.0.2",
    "-version": "0.6",
    "bounds": {
      "-maxlat": "54.0913900",
      "-maxlon": "12.2524800",
      "-minlat": "54.0889580",
      "-minlon": "12.2487570"
    },
    "foo": "bar"
  }
}

Contributing

Feel free to contribute to this project if you want to fix/extend/improve it.

Dependencies

~0.7–1.3MB
~26K SLoC