Jsonlib is a json parsing tool written in Rust! 🦀
The parser returns a Result
:
Result<Value, Box<dyn Error>>
A strong recursive type that represents any possible json value
pub enum Value {
Object(HashMap<String, Value>),
Array(Vec<Value>),
String(String),
Number(f64),
Bool(bool),
Null,
}
To use the value extract it using match case:
let res = jsonlib::parse(some_data).unwrap();
if let Value::Number(num) = &res {
//Do stuff with the num value
}
Or you can extract the value with a method for each type:
let num = jsonlib::parse(some_data).unwrap().get_num().unwrap();
//Do stuff with num
Available methods for Value are:
pub fn get_num(self) -> Result<f64, Box<dyn Error>>
pub fn get_obj(self) -> Result<HashMap<String, Value>, Box<dyn Error>>
pub fn get_arr(self) -> Result<Vec<Value>, Box<dyn Error>>
pub fn get_str(self) -> Result<String, Box<dyn Error>>
pub fn get_bool(self) -> Result<bool, Box<dyn Error>>
pub fn is_null(&self) -> Result<bool, Box<dyn Error>>
To use the parser include the library and the Value type into scope and you are good to go :
use jsonlib;
use jsonlib::value::Value;
let json_data = "{\"parse\":\"me\"}";
let result = jsonlib::parse(json_data).expect("Should parse no problem");
if let Value::Object(obj) = &result {
assert_eq!(obj["parse"], Value::String("me".to_string()));
}
As of right now there is a working parser, it is a work in progress and needs to be tested more...