Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JSON prettifying optional #32

Merged
merged 4 commits into from
Jun 27, 2024
Merged

Conversation

Kijewski
Copy link
Collaborator

This PR adds an optional argument to the |tojson filter, which controls if the serialized JSON data gets prettified or not. The arguments works the same as flask's |tojson filter, which passes the argument to python's json.dumps():

  • Omitting the argument, providing a negative integer, or None, then compact JSON data is generated.
  • Providing a non-negative integer, then this amount of ASCII spaces is used to indent the data. (Capped to 16 characters.)
  • Providing a string, then this string is used as prefix. I attempts are made to ensure that the prefix actually consists of whitespaces, because chances are, that if you provide e.g. &nsbp;, then you are doing it intentionally.

This is a breaking change, because it changes the default behavior to not prettify the data. This is done intentionally, because this is how it works in flask.

Kijewski added 2 commits June 25, 2024 00:12
This PR adds an optional argument to the `|tojson` filter, which
controls if the serialized JSON data gets prettified or not. The
arguments works the same as flask's [`|tojson`][flask] filter, which
passes the argument to python's [`json.dumps()`][python]:

* Omitting the argument, providing a negative integer, or `None`, then
  compact JSON data is generated.
* Providing a non-negative integer, then this amount of ASCII spaces is
  used to indent the data. (Capped to 16 characters.)
* Providing a string, then this string is used as prefix. I attempts are
  made to ensure that the prefix actually consists of whitespaces,
  because chances are, that if you provide e.g. `&nsbp;`, then you are
  doing it intentionally.

This is a breaking change, because it changes the default behavior to
not prettify the data. This is done intentionally, because this is how
it works in flask.

[flask]: https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.tojson
[python]: https://docs.python.org/3/library/json.html#json.dump
buf.write(CRATE);
buf.write("::filters::json(");
self._visit_args(ctx, buf, args)?;
match args.len() {
1 => buf.write(", ()"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a trait to handle integer/void, why not write a specialized json filter called json_empty?

Second question: why not forcing the type of indent to be a usize so we don't have to handle types and other things? I find it a bit strange to allow users to pass a negative number of even None as arguments.

GuillaumeGomez
GuillaumeGomez previously approved these changes Jun 26, 2024
@GuillaumeGomez
Copy link
Collaborator

Looks much simpler now, thanks! Please feel free to merge once CI pass.

@Kijewski Kijewski merged commit 214c445 into askama-rs:master Jun 27, 2024
17 checks passed
@Kijewski Kijewski deleted the pr-uglier-json branch June 27, 2024 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants