JSON Parser
This Rust crate provides a robust and efficient solution for parsing JSON data into Rust's strongly-typed data structures. It's designed to simplify the process of converting JSON text into Rust types and allows you to validate your JSON data with schema-based validation, making it easier to work with JSON data in Rust applications.
Features
- Strong Typing: Leverages Rust's type system to ensure that JSON data is parsed into the correct Rust data structures.
- Error Handling: Provides comprehensive error handling to catch and manage parsing errors, making your application more robust.
- Schema-Based Validation: This ensures that the JSON data adheres to a predefined schema, reducing runtime errors, enhancing data integrity and allows you to transform JSON values before validation.
Getting Started
To use this crate in your project, add it to your Cargo.toml
:
[]
= "0.2.1"
Then, import it in your Rust file:
use *;
Basic Usage
Parsing JSON to Rust
To parse a JSON string into a Rust JSONValue
, use the from
function provided by the crate:
use JSONParser;
let input = r#"
{
"name": "John Doe",
"age": 30,
"is_student": false
}
"#;
match from
Accessing Data
Once parsed, access the data using the .get()
method or the indexing syntax for both objects and arrays:
// Using `.get()` method
if let Some = json.get.and_then
// Using indexing syntax
let age = &json;
if let Some = age.as_f64
Serialization
To serialize a Rust data structure into a JSON string, use the serialize
method:
use ;
let json: JSONValue = from.unwrap;
let serialized = json.serialize;
assert_eq!;
Validation
To validate a JSONValue, use the validate
method:
use ;
let json: JSONValue = from.unwrap;
let schema = new;
assert!;
Contribution
Contributions are welcome! If you have suggestions for improvements or find any issues, please open an issue or submit a pull request on GitHub.
License
This project is licensed under the Apache-2.0 license.