hson
JSON like format for HTML.
The parser can be used for other purposes, but its main goal is to represent a HTML structure in a JSON style.
Allow to query the data the same way the DOM is queried client-side through QuerySelectorAll
method.
Main differences with standard json
- Allow same key multiple times in same object
- Does not allow
null
andundefined
- Root element must be an object
Usage
Parsing
Stringifying
Searching
Inserting
Removing
Iterating
Debugging
Events listening
Nodes manipulation
Parsing
use ;
...
let data = r#"{
"div": {
"attrs": {
"class": [""],
"onClick": "doSomething"
},
"div": {
"p": {
"attrs": {},
"span": {
"text": "Hello"
}
},
"p": {}
},
"div": {
"component": "test",
"attrs": {},
"onClick": "componentDoSomething"
}
}
}"#;
let mut hson = new;
hson.parse.unwrap;
hson.print_data;
Stringifying
...
let s = hson.stringify;
println!;
Searching
Search is similar to the javascript querySelectorAll
method.
use ;
...
// Standard recursive search
let results = hson.search.unwrap;
println!;
// Look for immediate childs, no recursive search
let results = hson.search.unwrap;
println!;
// Look for multiple childs (OR search)
let results = hson.search.unwrap;
println!;
// Look for a node with a specific value
let results = hson.search.unwrap;
println!;
// All those features can be combined
let results = hson.search.unwrap;
println!;
// Look for childs in a specific node, no recursion
let results = hson.search_in.unwrap;
println!;
let results = hson.search_in.unwrap;
println!;
Inserting
use ;
...
let results = hson.query.unwrap;
let child = r#"{
"i": {
"class": [],
"text": "World"
},
"ul": {
"class": ["active","test"]
}
}"#;
hson.insert.unwrap;
hson.print_data;
Removing
use ;
...
let results = hson.query.unwrap;
hson.remove.unwrap;
hson.print_data;
Iterating
Iterate over the nodes identifiers
...
for id in hson
// OR
loop
Debugging
use ;
...
hson.print_process_time;
hson.print_nodes; // true for sorted printing
hson.print_data; // true for pretty printing
Events
Current supported events are Parse, Insert, Remove.
use ;
...
let mut hson = new;
hson.subscribe;
...
Manipulation
Nodes values can be casted to primitive types using Vertex
, a Node
clone with more attributes.
Note : Vertex are Nodes clones and not references to the underlying Nodes. Manipulating Vertex's values will not be reflected on their matching Nodes.
use ;
...
let results = hson.search.unwrap;
let vertex = hson.get_vertex.unwrap;
// Get vertex value as u64
println!;
// Get vertex value as a vector of String
println!;
// Cast a string value to a different type
let s = "0.456";
println!;
Vertex methods
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>