-
Notifications
You must be signed in to change notification settings - Fork 17
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
Conversation
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
rinja_derive/src/generator.rs
Outdated
buf.write(CRATE); | ||
buf.write("::filters::json("); | ||
self._visit_args(ctx, buf, args)?; | ||
match args.len() { | ||
1 => buf.write(", ()"), |
There was a problem hiding this comment.
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.
Looks much simpler now, thanks! Please feel free to merge once CI pass. |
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'sjson.dumps()
:None
, then compact JSON data is generated.&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.