2个稳定版本
| 1.0.3 |
|
|---|---|
| 1.0.1 | 2022年8月11日 |
#1021 in 编码
2,755 每月下载量
在 6 个crate中使用(通过 br-xml)
49KB
970 行
XML2JSON
一个用于XML和JSON之间转换的Rust库。
安装
λ › cargo add xml2json-rs
用法
JSON转XML
XmlBuilder
XmlBuilder 从JSON构建XML。
build_from_json从一个serde_json::Value构建一个XMLString。build_from_json_string从一个序列化的JSONString构建一个XMLString。
示例
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 使用Builder模式设置配置选项,然后通过finalize构建一个XmlBuilder。
示例
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转JSON
JsonBuilder
JsonBuilder 从XML构建JSON。
build_from_xml从一个XMLString构建一个serde_json::Value。build_string_from_xml从一个XMLString构建一个JSON序列化的String。build_pretty_string_from_xml从一个XMLString构建一个格式化的JSON序列化的String。
示例
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 使用Builder模式设置配置选项,然后通过finalize构建一个JsonBuilder。
示例
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(())
}
目标
这个库受node-xml2json的启发,主要目标是与其0.4.20版本保持兼容。
测试
通过使用node-xml2json生成的脚本进行集成测试,以验证兼容性。
生成
λ › cd tests/generator
λ › yarn
λ › ./generate
运行
λ › cargo test
依赖
~4.5-6MB
~106K SLoC