12 个版本
0.1.11 | 2019年3月5日 |
---|---|
0.1.10 | 2019年3月5日 |
0.1.7 | 2019年2月17日 |
0.1.3 | 2019年1月30日 |
#1841 在 解析器实现
用于 hson_gen
66KB
2K SLoC
hson
HTML 类似于 JSON 的格式。
解析器可用于其他用途,但其主要目标是以 JSON 风格表示 HTML 结构。
允许在相同对象中多次使用相同的键。
与标准 JSON 的主要区别
- 允许在相同对象中多次使用相同的键
用法
解析
字符串化
搜索
插入
删除
迭代
调试
事件监听
节点操作
解析
use hson::{ Hson, Debug };
...
let data = r#"{
"div": {
"attrs": {
"class": [""],
"onClick": "doSomething"
},
"div": {
"p": {
"attrs": {},
"span": {
"text": "Hello"
}
},
"p": {}
},
"div": {
"component": "test",
"attrs": {},
"onClick": "componentDoSomething"
}
}
}"#;
let mut hson = Hson::new();
hson.parse(&data).unwrap();
hson.print_data(true);
字符串化
...
let s = hson.stringify();
println!("{}", &s);
搜索
搜索类似于 JavaScript 的 querySelectorAll
方法。
use hson::{ Hson, Query, Search };
...
// Standard recursive search
let results = hson.search("div p").unwrap();
println!("\n{:?}\n", results);
// Look for immediate childs, no recursive search
let results = hson.search("div>p").unwrap();
println!("\n{:?}\n", results);
// Look for multiple childs (OR search)
let results = hson.search("attrs id|rate|trusted").unwrap();
println!("\n{:?}\n", results);
// Look for a node with a specific value
let results = hson.search("attrs id='12'").unwrap();
println!("\n{:?}\n", results);
// All those features can be combined
let results = hson.search("div>p attrs id='12'|rate='3'|trusted").unwrap();
println!("\n{:?}\n", results);
// Look for childs in a specific node, no recursion
let results = hson.search_in(node_id, "div>p").unwrap();
println!("\n{:?}\n", results);
let results = hson.search_in(node_id, ">p").unwrap();
println!("\n{:?}\n", results);
插入
use hson::{ Hson, Query, Ops, Debug };
...
let results = hson.query("div p").unwrap();
let child = r#"{
"i": {
"class": [],
"text": "World"
},
"ul": {
"class": ["active","test"]
}
}"#;
hson.insert(results[0], 1, child).unwrap();
hson.print_data(true);
删除
use hson::{ Hson, Query, Ops, Debug };
...
let results = hson.query("p").unwrap();
hson.remove(results[0]).unwrap();
hson.print_data(true);
迭代
遍历节点标识符
...
for id in hson {
println!("{}", id);
}
// OR
loop {
let id = match hson.next() {
Some(s) => s,
None => break
};
match &hson.nodes.get(&id) {
Some(node) => {
println ! ("{} : {}", node.instance, node.id);
}
None => {
break
}
}
}
调试
use hson::{ Hson, Debug };
...
hson.print_process_time();
hson.print_nodes(true); // true for sorted printing
hson.print_data(true); // true for pretty printing
事件
当前支持的事件有 解析、插入、删除。
use hson::{ Hson, Ops, Event };
...
fn on_event (evt: Event, uid: u64) {
println!("\nEVENT : {:?} on {}\n", evt, uid);
}
let mut hson = Hson::new();
hson.subscribe(on_event);
...
操作
节点值可以使用具有更多属性的 Vertex
,即 Node
的克隆,转换为原始类型。
注意:Vertex 是节点克隆,而不是对底层节点的引用。操作 Vertex 的值不会反映在其匹配的节点上。
use hson::{ Hson, Query, Search, Cast };
...
let results = hson.search("div attrs class").unwrap();
let vertex = hson.get_vertex(results[0]).unwrap();
// Get vertex value as u64
println!("{}", vertex.value_as_f64());
// Get vertex value as a vector of String
println!("{:?}", vertex.value_as_array());
// Cast a string value to a different type
let s = "0.456";
println!("{}", vertex.as_f64(s));
Vertex 方法
fn key_as_string (&self) -> Option<String>
fn key_as_f64 (&self) -> Option<f64>
fn key_as_i64 (&self) -> Option<i64>
fn key_as_u64 (&self) -> Option<u64>
fn key_as_bool (&self) -> Option<bool>
fn value_as_string (&self) -> Option<String>
fn value_as_f64 (&self) -> Option<f64>
fn value_as_i64 (&self) -> Option<i64>
fn value_as_u64 (&self) -> Option<u64>
fn value_as_bool (&self) -> Option<bool>
fn value_as_array (&self) -> Option<Vec<String>>
fn as_f64 (&self, value: &str) -> Option<f64>
fn as_i64 (&self, value: &str) -> Option<i64>
fn as_u64 (&self, value: &str) -> Option<u64>
fn as_bool (&self, value: &str) -> Option<bool>