diff --git a/docs/index.md b/docs/index.md index 6fe977f..8671ee6 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,5 +1,5 @@

- Supercharge YAML configs with Jinja templates, typing and custom functions. + Supercharge configs with Jinja templates, typing and custom functions.

diff --git a/docs/json.md b/docs/json.md new file mode 100644 index 0000000..662d8b7 --- /dev/null +++ b/docs/json.md @@ -0,0 +1,45 @@ +## Rendering JSON Files + +`levy` supports JSON file as well as YAML. All the features remain the same, however, +we need to make sure that the JSON file we write is a correctly formatted JSON +after the render phase, which might be a bit tricky at times. + +> OBS: Note that the added difficulty is only for Jinja templating, as we need to + take care about putting all the quotes and commas. + +Let's revisit the first example we saw, and how it would look like as a JSON file: + +```json +{ + "title": "Lévy the cat", + "colors": ["black", "white"], + "hobby": { + "eating": { + "what": "anything" + } + }, + "friends": [ + {% set friends = [ "cartman", "lima" ] %} + {% for friend in friends %} + { + "name": "${ friend }", + "type": "cat" + } + {% if loop.index0 < friends|length - 1%} + , + {% endif %} + {% endfor %} + ] +} +``` + +As you can see, most of it is the same. However, in the `friends` list, we need +to add specific logic to add commas `,` if we have not reached the end of the loop. + +Afterwards, the API remains: + +```python +from levy.config import Config + +cfg = Config.read_file("test.json") +``` diff --git a/mkdocs.yml b/mkdocs.yml index 8228fa5..cb10ce6 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -34,6 +34,7 @@ nav: - Nested Config Lists: lists.md - Render Custom Functions: functions.md - Schema Validation: schema.md + - JSON Files: json.md - Contributing: contributing.md - References: references.md