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.
How this works?
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.
Changelog 0.2.9
I added a macro exist_same_level!, for determining if multiple key-pairs exist on the same level.
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.
Macro Collection
| 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"); |
Detailed Descriptions
Querying
-
query_key_pair!- Description: Finds the path to a key-value pair in a JSON-like structure. The key is a string, and the value can be either a string or a complex JSON value.
- Usage:
query_key_pair!; - Returns:
String(path to the parent object) orNoneif not found.
-
exist_same_level!- Description: Checks if all provided key-value pairs exist at the same level in a JSON-like structure.
- Usage:
exist_same_level!; - Returns:
String(path to the parent object) orNoneif not found.
-
query_value!- Description: Searches for a specific value in a JSON-like structure and returns its path. The value can be a string or a complex JSON value.
- Usage:
query_value!; - Returns:
String(path to the value) orNoneif not found.
-
exists!- Description: Checks if a specific key or value exists in a JSON-like structure.
- Usage:
exists!; - Returns:
bool(whether the key or value exists).
-
get!- Description: Retrieves a value from a JSON-like structure by its path.
- Usage:
let value = get!; - Returns:
serde_json::Value(the retrieved value).
Helper
-
print_pretty!- Description: Pretty-prints a JSON-like structure in a formatted way.
- Usage:
print_pretty!; - Returns: This macro does not return a value; it prints the formatted JSON structure to the console.
-
root_append!- Description: Appends a key-value pair to the root of a JSON-like structure.
- Usage:
root_append!; - Returns: This macro does not return a value; it appends the key-value pair to the root object.
-
set!- Description: Sets a value in a JSON-like structure by its path.
- Usage:
set!; - Returns: This macro does not return a value; it sets the value at the specified path.
-
append!- Description: Appends a value to an array within a JSON-like structure.
- Usage:
append!; - Returns: This macro does not return a value; it appends the value to the specified array.
-
delete!- Description: Deletes a key or value from a JSON-like structure by its path.
- Usage:
delete!; - Returns: This macro does not return a value; it deletes the key or value at the specified path.
File System
-
ensure_exist_with_schema!- Description: Checks if a file exists, and if it does, checks if the schema exists inside it. If it does not exist, it will create the file with the specified JSON.
- Usage:
ensure_exist_with_schema!; - Returns: This macro does not return a value; it ensures the file and schema exist.
-
ensure_exist!- Description: Ensures that a file exists by creating it if it doesn't.
- Usage:
ensure_exist!; - Returns: This macro does not return a value; it creates the file if it doesn't exist.
-
load!- Description: Loads a JSON-like structure from a file.
- Usage:
let mydict = load!; - Returns:
serde_json::Value(the loaded JSON structure).
-
save!- Description: Saves a JSON-like structure to a file.
- Usage:
save!; - Returns: This macro does not return a value; it saves the JSON structure to a file.
Example Usage
in a new rust project,
enter commands cargo add serde_json and cargo add json_easy2use
add the following to your main.rs file:
the output from this code is the following:
and test.db created in the current directory.