| Crates.io | json_easy2use |
| lib.rs | json_easy2use |
| version | 0.2.9 |
| created_at | 2024-08-26 05:11:46.289207+00 |
| updated_at | 2024-09-06 18:55:54.109449+00 |
| description | Work with json more easily like python's dict and javascript json. includes save, load, set, append, get, delete, and more... :) |
| homepage | https://github.com/RetributionByRevenue/json_easy2use |
| repository | https://github.com/RetributionByRevenue/json_easy2use |
| max_upload_size | |
| id | 1351783 |
| size | 38,768 |

As seen on Crates.io: https://crates.io/crates/json_easy2use
Use Rust's JSON more easily like python's dict and javascript JSON. includes set, append, get, delete, and more.
I made a series of custom macro's for Rust's Serde JSON crate. This will make it easier to work with JSON if you are familiar with Python or JavaScript.
I added a macro exist_same_level!, for determining if multiple key-pairs exist on the same level.
let result = exist_same_level!(mydict,
"destination_ip" => destination_ip,
"packet_type" => packet_type,
"source_ip" => source_ip
);
Changed Macros to return None instead of a string "None" or "Null". I want to avoid unnecessarily working with strings as a return unless it makes sense too.
| Querying | Helper | File System |
|---|---|---|
query_key_pair!Finds the path to a key-value pair in a JSON-like structure. Example: query_key_pair!(mydict, "key" => "value"); |
print_pretty!Pretty-prints a JSON-like structure in a formatted way. Example: print_pretty!(mydict); |
ensure_exist_with_schema!Checks if a file exists and its schema; creates it with the specified JSON if not. Example: ensure_exist_with_schema!("test.db", serde_json::json!({"key": "value"})); |
exist_same_level!Checks if all provided key-value pairs exist at the same level in a JSON-like structure. Example: exist_same_level!(mydict, "destination_ip" => destination_ip, "packet_type" => packet_type); |
root_append!Appends a key-value pair to the root of a JSON-like structure. Example: root_append!(mydict, "new_key" => "new_value"); |
ensure_exist!Ensures that a file exists by creating it if it doesn't. Example: ensure_exist!("data.db"); |
query_value!Searches for a specific value in a JSON-like structure and returns its path. Example: query_value!(mydict, "value"); |
set!Sets a value in a JSON-like structure by its path. Example: set!(mydict, "key.subkey", "new_value"); |
load!Loads a JSON-like structure from a file. Example: let mydict = load!("data.json"); |
exists!Checks if a specific key or value exists in a JSON-like structure. Example: exists!(mydict, "key"); |
append!Appends a value to an array within a JSON-like structure. Example: append!(mydict, "key.array", "new_value"); |
save!Saves a JSON-like structure to a file. Example: save!(mydict, "data.json"); |
get!Retrieves a value from a JSON-like structure by its path. Example: let value = get!(mydict, "key.subkey"); |
delete!Deletes a key or value from a JSON-like structure by its path. Example: delete!(mydict, "key.subkey"); |
query_key_pair!
query_key_pair!(mydict, "key" => "value");
String (path to the parent object) or None if not found.exist_same_level!
exist_same_level!(mydict, "destination_ip" => destination_ip, "packet_type" => packet_type);
String (path to the parent object) or None if not found.query_value!
query_value!(mydict, "value");
String (path to the value) or None if not found.exists!
exists!(mydict, "key");
bool (whether the key or value exists).get!
let value = get!(mydict, "key.subkey");
serde_json::Value (the retrieved value).print_pretty!
print_pretty!(mydict);
root_append!
root_append!(mydict, "new_key" => "new_value");
set!
set!(mydict, "key.subkey", "new_value");
append!
append!(mydict, "key.array", "new_value");
delete!
delete!(mydict, "key.subkey");
ensure_exist_with_schema!
ensure_exist_with_schema!("test.db", serde_json::json!({"key": "value"}));
ensure_exist!
ensure_exist!("data.db");
load!
let mydict = load!("data.json");
serde_json::Value (the loaded JSON structure).save!
save!(mydict, "data.json");
in a new rust project,
enter commands cargo add serde_json and cargo add json_easy2use
add the following to your main.rs file:
#[macro_use]
extern crate json_easy2use;
fn main() {
let mut mydict = serde_json::json!({
"level1": {
"level2": {
"level3a": "value_a",
"level3b": "value_b",
"level3c": "value_c"
}
}
});
// Using the `get` macro to retrieve a value
if let Some(value) = get!(mydict, "level1.level2") {
println!("Found: {}", value);
} else {
println!("Not found");
}
// Using the `root_append` macro to add a new key-value pair at the root level
root_append!(mydict, json!({"new_root_key": "new_root_value"}));
// Using the `set` macro to set a value at a specific path
set!(mydict, "level1.level2.level4" => [1, 2, 3]);
// Using the `append` macro to add a new key-value pair at a specific path
append!(mydict, "level1.level2" => json!({"level5": "value_d"}));
// Using the `delete` macro to remove a key-value pair at a specific path
delete!(mydict, "level1.level2.level3b");
if exists!(mydict, "level1.level2.level3b") {
println!("Key exists!");
} else {
println!("Key does not exist.");
}
// Print the final JSON structure
println!("Output");
println!("{}", mydict);
//Saving the JSON to file
save!(mydict, "./test.db");
}
the output from this code is the following:
Found: {
"level3a": "value_a",
"level3b": "value_b",
"level3c": "value_c"
}
Key does not exist.
Output:
{
"level1": {
"level2": {
"level3a": "value_a",
"level3c": "value_c",
"level4": [
1,
2,
3
],
"level5": "value_d"
}
},
"new_root_key": "new_root_value"
}
and test.db created in the current directory.