1extern crate serde_json;
2use super::ast::{Node::*, *};
3
4pub fn to_json(node: Node) -> String {
18 return match node {
19 Root(node) => serde_json::to_string(&node),
20 _ => serde_json::to_string(&node),
21 }
22 .unwrap();
23}
24
25#[cfg(test)]
26mod tests {
27 extern crate serde_json;
28 use super::super::parser::*;
29 use super::*;
30
31 const INPUT: &str = r#"
32<html>
33 <body>
34 <mt:Entries limit="10">
35 <mtEntryTitle encode_html='1'/>
36 </mt:Entries>
37 </body>
38</html>"#;
39
40 #[test]
41 fn test_serialize() {
42 let root = parse(INPUT).unwrap();
43 let json = to_json(root);
44 assert_eq!(
45 json,
46 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}]}"#
47 )
48 }
49}