A json builder used to create JSON structures using a simple DSL (based on Jbuilder). It is a simple and easy to use tool to create JSON structures in Rust.
notes @notes |note| do content @note.content author do name @note.author.name age @note.author.age end comments @note.comments |comment| do content @comment.content author do name @comment.author.name age @comment.author.age end end end Built with the above DSL, the following JSON structure will be created:
{ "notes": [ { "content": "Note content", "author": { "name": "Author name", "age": 20 }, "comments": [ { "content": "Comment content", "author": { "name": "Comment author name", "age": 25 } } ] } ] }Add the following to your Cargo.toml file:
[dependencies] jsonbuilder = "0.1.0"use jsonbuilder::{JsonBuilder, context_map}; #[derive(Serialize)] struct Note { content: String, } fn main() { let context = context_map!({ "note" => Note::new(), }) let json = JsonBuilder::render("data", context) }JsonBuilder searches for a file that ends with json.jb in templates directory.
or you can render with raw string:
use jsonbuilder::JsonBuilder; #[derive(Serialize)] struct Note { content: String, } fn main() { let context = context_map!({ "note" => Note::new(), }) let input = " note @note.content ... " let json = JsonBuilder::render_raw(input, context) }you can change the settings by creating a jsonbuilder.toml file in the root of your project.
template_path = "another/path/to/templates"The syntax is based on Jbuilder and is very similar to it. The following is a list of the supported syntax:
name @variable names @names |item| do name @item end person do name @person.name age @person.age _ @person.abilities # _ is a special character that allows you to access the entire object end - Basic DSL
- Array mapping
- Object mapping
- Extend with another templates
- Conditional statements
- Merge objects
Contributions are welcome! Feel free to open an issue or submit a pull request.
See the contribution guidelines for more information.
This project is licensed under the MIT License - see the LICENSE file for details.