This is a simple library that parses json files or text into usable rust objects, its still in development and not ready for production use.
- currently the when serializing back to strings the order of key value pairs changes due to how rust maps work.
- Parses JSON strings into a
JsonValue
enum. - Supports JSON data types:
null
,boolean
,number
,string
,array
, andobject
. - Includes error handling for invalid JSON strings.
- Serializes
JsonValue
enum back into JSON strings.
use your_json_parser_lib::parse_json;
fn main() {
let json_str = r#"
{
"name": "Gojo Saturo",
"age": 29,
"alive": "its complicated"
}
"#;
match parse_json(json_str) {
Ok(json_value) => println!("{:?}", json_value),
Err(e) => eprintln!("Error: {}", e),
}
}
JsonValue
The JsonValue
enum represents a JSON value and can be one of the following variants:
- Null
- Bool(bool)
- Number(f64)
- String(String)
- Array(Vec)
- Object(HashMap<String, JsonValue>)
parse_json
The parse_json
function takes a JSON string slice and returns a Result<JsonValue, String>
:
Copy code
fn parse_json(json: &str) -> Result<JsonValue, String>;
For now to parse a JSON file, you can use the following example code:
Copy code
use std::fs::File;
use std::io::Read;
use your_json_parser_lib::parse_json;
fn main() {
let mut file_content = String::new();
let mut file = File::open("examples/sample.json").expect("Error opening file");
file.read_to_string(&mut file_content).expect("Error reading file");
match parse_json(&file_content) {
Ok(json_value) => println!("{:?}", json_value),
Err(e) => eprintln!("Error: {}", e),
}
}
The parser will return an Err(String) if it encounters an invalid JSON string. The error message will provide details about the issue.
Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License.