conf_json 0.1.4

A human editable configure file in JSON parser
Documentation
  • Coverage
  • 38.24%
    13 out of 34 items documented1 out of 21 items with examples
  • Size
  • Source code size: 24.83 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 7.24 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • TedQue

A JSON compatable configure file parser

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:

  • Comments begin with '#' which can take a whole line or trailing part of a line
  • Both single and double quotation marks strings are allowed
  • For JSON Arry and Object, trailing comma is allowed

Examples

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"