json_easy2use 0.2.9

Work with json more easily like python's dict and javascript json. includes save, load, set, append, get, delete, and more... :)
Documentation

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

  1. 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!(mydict, "key" => "value");
      
    • Returns: String (path to the parent object) or None if not found.
  2. 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!(mydict, "destination_ip" => destination_ip, "packet_type" => packet_type);
      
    • Returns: String (path to the parent object) or None if not found.
  3. 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!(mydict, "value");
      
    • Returns: String (path to the value) or None if not found.
  4. exists!

    • Description: Checks if a specific key or value exists in a JSON-like structure.
    • Usage:
      exists!(mydict, "key");
      
    • Returns: bool (whether the key or value exists).
  5. get!

    • Description: Retrieves a value from a JSON-like structure by its path.
    • Usage:
      let value = get!(mydict, "key.subkey");
      
    • Returns: serde_json::Value (the retrieved value).

Helper

  1. print_pretty!

    • Description: Pretty-prints a JSON-like structure in a formatted way.
    • Usage:
      print_pretty!(mydict);
      
    • Returns: This macro does not return a value; it prints the formatted JSON structure to the console.
  2. root_append!

    • Description: Appends a key-value pair to the root of a JSON-like structure.
    • Usage:
      root_append!(mydict, "new_key" => "new_value");
      
    • Returns: This macro does not return a value; it appends the key-value pair to the root object.
  3. set!

    • Description: Sets a value in a JSON-like structure by its path.
    • Usage:
      set!(mydict, "key.subkey", "new_value");
      
    • Returns: This macro does not return a value; it sets the value at the specified path.
  4. append!

    • Description: Appends a value to an array within a JSON-like structure.
    • Usage:
      append!(mydict, "key.array", "new_value");
      
    • Returns: This macro does not return a value; it appends the value to the specified array.
  5. delete!

    • Description: Deletes a key or value from a JSON-like structure by its path.
    • Usage:
      delete!(mydict, "key.subkey");
      
    • Returns: This macro does not return a value; it deletes the key or value at the specified path.

File System

  1. 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!("test.db", serde_json::json!({"key": "value"}));
      
    • Returns: This macro does not return a value; it ensures the file and schema exist.
  2. ensure_exist!

    • Description: Ensures that a file exists by creating it if it doesn't.
    • Usage:
      ensure_exist!("data.db");
      
    • Returns: This macro does not return a value; it creates the file if it doesn't exist.
  3. load!

    • Description: Loads a JSON-like structure from a file.
    • Usage:
      let mydict = load!("data.json");
      
    • Returns: serde_json::Value (the loaded JSON structure).
  4. save!

    • Description: Saves a JSON-like structure to a file.
    • Usage:
      save!(mydict, "data.json");
      
    • 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.