https://github.com/youssef-shehata/json-parser
json parser in rust
https://github.com/youssef-shehata/json-parser
rust
Last synced: 3 months ago
JSON representation
json parser in rust
- Host: GitHub
- URL: https://github.com/youssef-shehata/json-parser
- Owner: Youssef-Shehata
- Created: 2024-10-27T21:07:51.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-12-30T17:22:37.000Z (5 months ago)
- Last Synced: 2025-01-23T20:32:34.789Z (5 months ago)
- Topics: rust
- Language: Rust
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Json-Parser
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.
## bugs
- currently the when serializing back to strings the order of key value pairs changes due to how rust maps work.
## Features
- Parses JSON strings into a `JsonValue` enum.
- Supports JSON data types: `null`, `boolean`, `number`, `string`, `array`, and `object`.
- Includes error handling for invalid JSON strings.
- Serializes `JsonValue` enum back into JSON strings.## Usage
```rust
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),
}
}
```
## API`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)`parse_json`
The `parse_json` function takes a JSON string slice and returns a `Result`:```rust
Copy code
fn parse_json(json: &str) -> Result;
```## Example
For now to parse a JSON file, you can use the following example code:
```rust
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),
}
}
```
## Error Handling
The parser will return an Err(String) if it encounters an invalid JSON string. The error message will provide details about the issue.## Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.## License
This project is licensed under the MIT License.