1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/*
* This library helps convert an XML String into a serde_json::Value which can be
* used to generate JSON
*/
#[cfg(test)]
#[macro_use]
extern crate serde_json;
use log::*;
use quick_xml::events::Event;
use quick_xml::Reader;
use serde_json::{Map, Value};
#[derive(Debug)]
pub struct Error {}
fn read(reader: &mut Reader<&[u8]>, depth: u64) -> Value {
let mut buf = Vec::new();
let mut values = Vec::new();
let mut node = Map::new();
debug!("Parsing at depth: {}", depth);
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) => {
if let Ok(name) = String::from_utf8(e.name().into_inner().to_vec()) {
let mut child = read(reader, depth + 1);
let mut attrs = Map::new();
debug!("{} children: {:?}", name, child);
let _ = e
.attributes()
.map(|a| {
if let Ok(attr) = a {
let key = String::from_utf8(attr.key.into_inner().to_vec());
let value = String::from_utf8(attr.value.to_vec());
// Only bother adding the attribute if both key and value are valid utf8
if let (Ok(key), Ok(value)) = (key, value) {
let key = format!("@{}", key);
let value = Value::String(value);
// If the child is already an object, that's where the insert
// should happen
if child.is_object() {
child.as_object_mut().unwrap().insert(key, value);
} else {
attrs.insert(key, value);
}
}
}
})
.collect::<Vec<_>>();
/*
* nodes with attributes need to be handled special
*/
if !attrs.is_empty() {
if child.is_string() {
attrs.insert("#text".to_string(), child);
}
if let Ok(attrs) = serde_json::to_value(attrs) {
node.insert(name, attrs);
}
} else if node.contains_key(&name) {
debug!("Node contains `{}` already, need to convert to array", name);
let (_, mut existing) = node.remove_entry(&name).unwrap();
let mut entries: Vec<Value> = vec![];
if existing.is_array() {
let existing = existing.as_array_mut().unwrap();
while !existing.is_empty() {
entries.push(existing.remove(0));
}
} else {
entries.push(existing);
}
entries.push(child);
node.insert(name, Value::Array(entries));
} else {
node.insert(name, child);
}
}
}
Ok(Event::Text(ref e)) => {
if let Ok(decoded) = e.unescape() {
values.push(Value::String(decoded.to_string()));
}
}
Ok(Event::CData(ref e)) => {
if let Ok(decoded) = e.clone().escape() {
if let Ok(decoded_bt) = decoded.unescape() {
node.insert("#cdata".to_string(), Value::String(decoded_bt.to_string()));
}
}
}
Ok(Event::End(ref _e)) => break,
Ok(Event::Eof) => break,
_ => (),
}
}
debug!("values to return: {:?}", values);
if !node.is_empty() {
// If we had collected some text along the way, that needs to be inserted
// so we don't lose it
let mut index = 0;
let mut has_text = false;
for value in values.iter() {
if value.is_string() {
has_text = true;
break;
}
index += 1;
}
if has_text {
node.insert("#text".to_string(), values.remove(index));
}
debug!("returning node instead: {:?}", node);
return serde_json::to_value(&node).expect("Failed to #to_value() a node!");
}
match values.len() {
0 => Value::Null,
1 => values.pop().unwrap(),
_ => Value::Array(values),
}
}
/**
* to_json() will take an input string and attempt to convert it into a form
* of JSON
*/
pub fn to_json(xml: &str) -> Result<Value, Error> {
let mut reader = Reader::from_str(xml);
reader.expand_empty_elements(true);
reader.trim_text(true);
Ok(read(&mut reader, 0))
}
#[cfg(test)]
mod tests {
use super::*;
fn json_eq(left: Value, right: Result<Value, Error>) {
assert!(right.is_ok());
assert_eq!(left, right.unwrap());
}
#[test]
fn single_node() {
json_eq(json!({ "e": null }), to_json("<e></e>"));
}
#[test]
fn node_with_text() {
json_eq(json!({"e" : "foo"}), to_json("<e>foo</e>"));
}
#[test]
fn node_with_attr() {
json_eq(
json!({"e" : {"@name":"value"}}),
to_json("<e name=\"value\"></e>"),
);
}
#[test]
fn node_with_attr_and_text() {
json_eq(
json!({"e": {"@name":"value", "#text" : "text"}}),
to_json(r#"<e name="value">text</e>"#),
);
}
#[test]
fn node_with_children() {
json_eq(
json!(
{
"e":{
"a":"text1",
"b":"text2"
}
}),
to_json(r#"<e> <a>text1</a> <b>text2</b> </e>"#),
);
}
#[test]
fn node_with_multiple_identical_children() {
json_eq(
json!({
"e":{"a":[
"text",
"text"
]}
}),
to_json(r#"<e><a>text</a><a>text</a></e>"#),
);
}
#[test]
fn node_with_n_identical_children() {
json_eq(
json!({
"e":{"a":[
"text1",
"text2",
"text3"
]}
}),
to_json(r#"<e><a>text1</a><a>text2</a><a>text3</a></e>"#),
);
}
#[test]
fn node_with_text_and_child() {
json_eq(
json!(
{
"e":{
"#text":"lol",
"a":"text"
}
}),
to_json(r#"<e> lol <a>text</a></e>"#),
);
}
#[test]
fn node_with_just_text() {
json_eq(
json!(
{
"a":"hello"
}),
to_json(r#"<a>hello</a>"#),
);
}
#[test]
fn node_with_attrs_and_text() {
json_eq(
json!(
{
"a":{
"@x":"y",
"#text":"hello"
}
}),
to_json(r#"<a x="y">hello</a>"#),
);
}
#[test]
fn nested_nodes_with_attrs() {
json_eq(
json!(
{
"a":{
"@id":"a",
"b":{
"@id":"b",
"#text":"hey!"
}
}
}),
to_json(r#"<a id="a"><b id="b">hey!</b></a>"#),
);
}
#[test]
fn node_with_nested_test() {
/*
todo!("this syntax makes no sense to me");
json_eq(json!(
{
"a":"x<c/>y"
}),
to_json(r#"<a>x<c/>y</a>"#)
);
*/
}
#[test]
fn node_with_empty_attrs() {
json_eq(
json!(
{
"x":{"@u":""}
}),
to_json(r#"<x u=""/>"#),
);
}
#[test]
fn some_basic_html() {
json_eq(
json!(
{
"html":{
"head":{
"title":"Xml/Json",
"meta":{
"@name":"x",
"@content":"y"
}
},
"body":null
}
}),
to_json(
r#"<html><head><title>Xml/Json</title><meta name="x" content="y"/></head><body/></html>"#,
),
);
}
#[test]
fn more_complex_html() {
json_eq(
json!(
{
"ol":{
"@class":"xoxo",
"li":[
{
"#text":"Subject 1",
"ol":{"li":[
"subpoint a",
"subpoint b"
]}
},
{
"span":"Subject 2",
"ol":{
"@compact":"compact",
"li":[
"subpoint c",
"subpoint d"
]
}
}
]
}
}),
to_json(
r#"<ol class="xoxo"><li>Subject 1 <ol><li>subpoint a</li><li>subpoint b</li></ol></li><li><span>Subject 2</span><ol compact="compact"><li>subpoint c</li><li>subpoint d</li></ol></li></ol>"#,
),
);
}
#[test]
fn node_with_cdata() {
json_eq(
json!(
{
"e":{"#cdata":" .. some data .. "}
}),
to_json(r#"<e><![CDATA[ .. some data .. ]]></e>"#),
);
}
#[test]
fn node_with_cdata_and_siblings() {
json_eq(
json!(
{
"e":{
"a":null,
"#cdata":" .. some data .. ",
"b":null
}
}),
to_json(r#"<e><a/><![CDATA[ .. some data .. ]]><b/></e>"#),
);
}
#[test]
fn node_with_cdata_inside_text() {
/*
* TODO
json_eq(json!(
{
"e":"\n some text\n <![CDATA[ .. some data .. ]]>\n more text\n"
}),
to_json(r#"<e> some text <![CDATA[ .. some data .. ]]> more text</e>"#)
);
*/
}
#[test]
fn node_with_child_cdata_and_text() {
json_eq(
json!(
{
"e":{
"#text":"some text",
"#cdata":" .. some data .. ",
"a":null
}
}),
to_json(r#"<e> some text <![CDATA[ .. some data .. ]]><a/></e>"#),
);
}
#[test]
fn node_with_duplicate_cdata() {
/*
* TODO: unsure about this approach to handling cdata
json_eq(json!(
{
"e":"<![CDATA[ .. some data .. ]]><![CDATA[ .. more data .. ]]>"
}
),
to_json(r#"<e><![CDATA[ .. some data .. ]]><![CDATA[ .. more data .. ]]></e>"#)
);
*/
}
}