#json-file #deserialize-json #serde-json #database #applications

struct-to-json-db

A minimalist file-based database designed for straightforward software applications. It leverages the Serde crate to seamlessly serialize and deserialize a HashMap of structured data to and from JSON files, ensuring simplicity and ease of use.

21 releases

0.1.20 Aug 29, 2024
0.1.19 Aug 29, 2024

#1569 in Parser implementations

Download history 180/week @ 2024-08-04 254/week @ 2024-08-11 14/week @ 2024-08-18 1443/week @ 2024-08-25 116/week @ 2024-09-01 30/week @ 2024-09-15 174/week @ 2024-09-29 4/week @ 2024-10-06 16/week @ 2024-10-13

194 downloads per month

Apache-2.0

12KB
198 lines

struct-to-json-db is a minimalist file-based database designed for straightforward software applications. It leverages the Serde crate to seamlessly serialize and deserialize a HashMap of structured data to and from JSON files, ensuring simplicity and ease of use.

Example code

For a practical example, check out the example code repository.

Get started

To get started, add the crate to your Cargo.toml file:

[dependencies]
struct-to-json-db = {  path = "https://github.com/acscoder/struct-to-json-db.git" } 

or

[dependencies]
struct-to-json-db = "x.x.x" 

Check the latest version on https://crates.io/search?q=struct-to-json-db

Configuration

To configure struct-to-json-db, use the auto_json_db_config! macro to specify the directory where your JSON files will be stored:

struct_to_json_db::auto_json_db_config!("./db/");

Note: Ensure that the path ends with a slash (/). For example, "./db/" is correct, but "./db" is not.

Adding the Macro to Your Struct

Use the #[auto_json_db] macro for your struct to automatically add a unique ID and additional methods:

#[auto_json_db]
struct Post {
    title: String,
    description: String,
    categories: Vec<String>
}

This macro will add the following:

A unique id (idx: u64) to the Post struct.

A new method for creating new instances.

A get_all method to retrieve all saved posts from the JSON file as a HashMap with idx as the key and Post object as the value.

A save method to save a single Post to the JSON file.

A save_vec method to save a vector of Post objects to the JSON file.

A get_by_id method to retrieve a Post by its idx.

A remove_by_id method to remove a Post by its idx.

A clear method to remove all Post objects.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the APACHE License.

Dependencies

~0.5–1MB
~22K SLoC