Add README and examples
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
use wson::{Config, Node};
|
||||
|
||||
fn main() -> wson::Result<()> {
|
||||
let cfg = Config {
|
||||
serialize_references: true,
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
let graph = wson::text::parse_str("(0){\"child\":$0,\"id\":25}", &cfg)?;
|
||||
let root = graph.root();
|
||||
let child = match graph.node(root) {
|
||||
Node::Object(entries) => *entries.get("child").expect("child entry"),
|
||||
other => panic!("expected object, got {other:?}"),
|
||||
};
|
||||
assert_eq!(root, child);
|
||||
println!("root and child are the same node: {root:?}");
|
||||
|
||||
let text = wson::text::dump_graph(&graph, &cfg)?;
|
||||
println!("text: {text}");
|
||||
|
||||
let jbon = wson::binary::dump_graph(&graph, &cfg)?;
|
||||
println!("jbon length: {} bytes", jbon.len());
|
||||
|
||||
let reparsed = wson::binary::parse_slice(&jbon, &cfg)?;
|
||||
let reparsed_root = reparsed.root();
|
||||
let reparsed_child = match reparsed.node(reparsed_root) {
|
||||
Node::Object(entries) => *entries.get("child").expect("child entry"),
|
||||
other => panic!("expected object, got {other:?}"),
|
||||
};
|
||||
assert_eq!(reparsed_root, reparsed_child);
|
||||
println!("binary round trip ok");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct ServiceConfig {
|
||||
name: String,
|
||||
enabled: bool,
|
||||
retries: i64,
|
||||
endpoints: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() -> wson::Result<()> {
|
||||
let value = ServiceConfig {
|
||||
name: "worker".to_string(),
|
||||
enabled: true,
|
||||
retries: 3,
|
||||
endpoints: vec![
|
||||
"https://a.example".to_string(),
|
||||
"https://b.example".to_string(),
|
||||
],
|
||||
};
|
||||
|
||||
let bytes = wson::to_vec(&value)?;
|
||||
println!("jbon length: {} bytes", bytes.len());
|
||||
println!("jbon prefix: {:02x?}", &bytes[..bytes.len().min(16)]);
|
||||
|
||||
let decoded: ServiceConfig = wson::from_slice(&bytes)?;
|
||||
assert_eq!(decoded, value);
|
||||
println!("round trip ok");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct ServiceConfig {
|
||||
name: String,
|
||||
enabled: bool,
|
||||
retries: i64,
|
||||
endpoints: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() -> wson::Result<()> {
|
||||
let value = ServiceConfig {
|
||||
name: "worker".to_string(),
|
||||
enabled: true,
|
||||
retries: 3,
|
||||
endpoints: vec![
|
||||
"https://a.example".to_string(),
|
||||
"https://b.example".to_string(),
|
||||
],
|
||||
};
|
||||
|
||||
let text = wson::to_string(&value)?;
|
||||
println!("text: {text}");
|
||||
|
||||
let decoded: ServiceConfig = wson::from_str(&text)?;
|
||||
assert_eq!(decoded, value);
|
||||
println!("round trip ok");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user