Open mhalle opened 4 years ago
Yes I think that makes sense. I added json
to the template context in Dogsheep Beta just a few days ago because I needed that: https://github.com/dogsheep/dogsheep-beta/blob/bed9df2b3ef68189e2e445427721a28f4e9b4887/dogsheep_beta/__init__.py#L176
I'm not a huge fan of from_json
as the name for this. Some other options:
json
directly so templates can do json.loads()
and json.dumps()
- this allows for outputting JSON too, which is useful. But is there anything else on the json
module that shouldn't be exposed in templates?json_dumps()
and json_loads()
template functions. I quite like that.I think json_dumps()
and json_loads()
as aliases for json.dumps()
and json.loads()
is the way to go here.
I agree on the descriptive and python-consistent naming. There is already a tojson, but frankly i find the "to" and "from" confusing in a text templating language where what's a string and what's data isn't 100% transparent.
When JSON fields are rendered in a jinja2 template, it is handy to be able to manipulate them as data (e.g., iterate over an array of values).
Ansible has a "from_json" function, which just called json.loads. It's a trivial as a datasette plugin, but it seems generally useful. Does it makes sense to add it directly into the app?