Skip to content

Jsonlib is a json parsing tool written completely in Rust! 🦀

License

Notifications You must be signed in to change notification settings

mihna123/jsonlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonlib

Jsonlib is a json parsing tool written in Rust! 🦀

Usage

The parser returns a Result :

Result<Value, Box<dyn Error>>

Value type

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()));
}

Current development

As of right now there is a working parser, it is a work in progress and needs to be tested more...

About

Jsonlib is a json parsing tool written completely in Rust! 🦀

Resources

License

Stars

Watchers

Forks

Languages