Crates.io | parson |
lib.rs | parson |
version | 1.1.0 |
source | src |
created_at | 2022-02-20 15:07:03.729071 |
updated_at | 2022-09-13 18:00:45.268474 |
description | A crate for parsing JSON into Rust types |
homepage | |
repository | https://www.github.com/zS1L3NT/rs-parson |
max_upload_size | |
id | 535765 |
size | 90,284 |
Parson is a Rust crate that parses a JSON string so that the JSON data can be used in another Rust project
My interest in Rust is growing rapidly due to my boredom of TypeScript and Web Development. Making a Parson in Rust can possibly teach me a lot more about Rust than I currently know. Learning how to make a parser will also teach me about the steps to make a Lexer and a Parser. This is a stepping stone to making my own JavaScript interpreter, my Rust dream project.
Add this to your Rust project's Cargo.toml
file
[dependencies]
...
parson = "<version number>"
Since the JSONValue
struct implements FromStr
, you can parse it from a string directly into a JSONValue
.
If an error occurs in the parsing, you will get back a JSONError
use parson::JSONValue;
fn main() {
let json = r#"{ "key": "value" }"#;
let json_value = json.parse::<JSONValue>().expect("JSON parse failed");
println!("{}", json_value);
}
JSONValue
structThe JSONValue
struct holds data contianing either of the following
JSONValue
has methods to get the value of each type:
get_string(): Result<String,
JSONError
>
get_number(): Result<f64,
JSONError
>
get_boolean(): Result<bool,
JSONError
>
get_null(): Result<&
JSONNull
,
JSONError
>
get_array(): Result<&
JSONArray
,
JSONError
>
get_object(): Result<&
JSONObject
,
JSONError
>
JSONValue
also has methods to check if the value is of each type
is_string(): bool
is_number(): bool
is_boolean(): bool
is_null(): bool
is_array(): bool
is_object(): bool
JSONValue
has a method which returns an enum of the type of value it holds
let json_value = /* result of parsing a string */
match json_value.get_type() {
JSONType::String(json_string) => println!("{}", json_string.get_string()),
JSONType::Number(json_number) => println!("{}", json_number.get_number()),
JSONType::Boolean(json_boolean) => println!("{}", json_boolean.get_boolean()),
JSONType::Null(json_null) => println!("Null"),
JSONType::Array(json_array) => println!("{} items", json_array.len()),
JSONType::Object(json_object) => println!("{} pairs", json_object.len()),
}
JSONString
structThe JSONString
struct holds data about a string in your input json as a String
.
The JSONString
implementation has 1 method to get the data within it.
get_string(): String
JSONNumber
structThe JSONNumber
struct holds data about a number in your input json as an f64
.
The JSONNumber
implementation has 1 method to get the data within it.
get_number(): f64
JSONBoolean
structThe JSONBoolean
struct holds data about a boolean in your input json as a bool
.
The JSONBoolean
implementation has 1 method to get the data within it.
get_boolean(): bool
JSONNull
structThe JSONNull
struct is a representation of a null value in your input json.
The JSONNull
implementation has no methods to get the data within it since Rust has no way to represent Null values.
JSONArray
structThe JSONArray
struct holds data about an array in your input json as a Vec<
JSONValue
>
.
The JSONArray
implementation has 3 methods to get the data within it.
JSONObject
structThe JSONObject
struct holds data about an object in your input json as a IndexMap<String,
JSONValue
>
, which is a HashMap that maintains the order data was added to it.
The JSONObject
implementation has 3 methods to get the data within it.
JSONError
structThe JSONError
struct holds the message of an error that happened when parsing your input json.
JSONError
has 1 method to get the error message within it.
get_message(): String
I also learnt how to make a basic JSON Parser from here. I understood and improvised the way the author wrote the Lexer and Parsers to make Parson more bug free.