Crates.io | conf_json |
lib.rs | conf_json |
version | 0.1.4 |
source | src |
created_at | 2023-11-08 01:39:14.401211 |
updated_at | 2023-11-10 11:45:13.552398 |
description | A human editable configure file in JSON parser |
homepage | |
repository | |
max_upload_size | |
id | 1028631 |
size | 25,170 |
This crate is mainly for read
not write
from a human editable configure file in JSON.
Most of the configure key accessing methods are immutable.
Besides standard JSON, following features are supported:
Parse from str
use conf_json::value;
let s = r##"
# this is a json conf file
#
{
# comment line
"null_key": null,
"i64_key": 1,
"i64_sci_key": 123E+4,
"i64_0x_key": 0xAf,
"f64_key": 3.14,
"f64_sci_key": 3.14e-2,
"true_key": true,
"false_key": false, # comment in line
'single_quote_key': 'a single quote value',
"object_key": {
"inner_key1": "abc",
"inner_key2": 3.14E3,
},
"array_key": [1, "abc", ],
}
"##;
println!("data source:");
println!("{}", s);
let v = s.parse::<value::Value>().unwrap();
// basic accessing
println!("\nbasic accessing assertion begin ...");
assert!(v.is_object());
assert!(!v.is_null());
assert!(v.has("i64_key"));
assert!(!v["null_key"].as_bool());
assert!(v["i64_key"].as_bool());
assert_eq!(v["i64_key"].as_i64(), 1);
assert_eq!(v["f64_key"].as_f64(), 3.14);
assert_eq!(v["object_key"]["inner_key1"].as_str(), "abc");
assert_eq!(v["array_key"][1].as_str(), "abc");
println!("basic accessing assertion pass");
// iter over JSON Array
println!("\niter over JSON Array:");
v["array_key"].iter_array().for_each(|v| {println!("{:?}", v); });
// iter over JSON Object
println!("\niter over JSON Object:");
v.iter_object().for_each(|(k, v)| { println!("{}: {:?}", k, v); });
Load from str
use conf_json;
let v: conf_json::value::Value = "\"abc\"".as_bytes().into();
assert_eq!(v.as_str(), "abc");
Load from file
use conf_json;
let v = conf_json::load_from_file("sample.conf").unwrap();
assert!(v.is_object());
assert_eq!(v["f64_key"].as_f64(), 3.14);
Load from any type T t that implements std::io::Read trait
use conf_json;
let t = b"123";
let v: conf_json::value::Value = t.as_slice().into();
assert_eq!(v.as_i64(), 123);
In Cargo.toml
[dependencies]
conf_json = "0.1.4"