extern crate serde_json;
use super::ast::{Node::*, *};
pub fn to_json(node: Node) -> String {
return match node {
Root(node) => serde_json::to_string(&node),
_ => serde_json::to_string(&node),
}
.unwrap();
}
#[cfg(test)]
mod tests {
extern crate serde_json;
use super::super::parser::*;
use super::*;
const INPUT: &str = r#"
<html>
<body>
<mt:Entries limit="10">
<mtEntryTitle encode_html='1'/>
</mt:Entries>
</body>
</html>"#;
#[test]
fn test_serialize() {
let root = parse(INPUT).unwrap();
let json = to_json(root);
assert_eq!(
json,
r#"{"children":[{"type":"Text","value":"\n<html>\n <body>\n ","line":1,"column":1,"offset":0},{"type":"BlockTag","name":"Entries","attributes":[{"name":"limit","values":[{"value":"10","line":4,"column":26,"offset":42}],"line":4,"column":20,"offset":36}],"children":[{"type":"Text","value":"\n ","line":4,"column":31,"offset":47},{"type":"FunctionTag","name":"EntryTitle","attributes":[{"name":"encode_html","values":[{"value":"1","line":5,"column":33,"offset":80}],"line":5,"column":21,"offset":68}],"line":5,"column":7,"offset":54},{"type":"Text","value":"\n ","line":5,"column":38,"offset":85}],"line":4,"column":5,"offset":21},{"type":"Text","value":"\n </body>\n</html>","line":6,"column":18,"offset":103}]}"#
)
}
}