Skip to content

Commit

Permalink
Add JSON docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmbrull committed Oct 24, 2021
1 parent c5a1a50 commit ab71058
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<em>Supercharge YAML configs with Jinja templates, typing and custom functions.</em>
<em>Supercharge configs with Jinja templates, typing and custom functions.</em>
</p>
<p align="center">
<a href="https://pypi.org/project/levy/" target="_blank">
Expand Down
45 changes: 45 additions & 0 deletions docs/json.md
Original file line number Diff line number Diff line change
@@ -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")
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ab71058

Please sign in to comment.