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 #1023

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions askama/benches/to-json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ fn functions(c: &mut Criterion) {
fn escape_json(b: &mut criterion::Bencher<'_>) {
b.iter(|| {
for &s in STRINGS {
format!("{}", json(s).unwrap());
format!("{}", json(s, ()).unwrap());
}
});
}

fn escape_json_for_html(b: &mut criterion::Bencher<'_>) {
b.iter(|| {
for &s in STRINGS {
format!("{}", MarkupDisplay::new_unsafe(json(s).unwrap(), Html));
format!("{}", MarkupDisplay::new_unsafe(json(s, ()).unwrap(), Html));
}
});
}
Expand Down
151 changes: 137 additions & 14 deletions askama/src/filters/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::Infallible;
use std::{fmt, io, str};

use serde::Serialize;
use serde_json::to_writer_pretty;
use serde_json::ser::{to_writer, PrettyFormatter, Serializer};

/// Serialize to JSON (requires `json` feature)
///
Expand All @@ -19,17 +19,105 @@ use serde_json::to_writer_pretty;
/// or in apostrophes with the (optional) safe filter `'{{data|json|safe}}'`.
/// In HTML texts the output of e.g. `<pre>{{data|json|safe}}</pre>` is safe, too.
#[inline]
pub fn json<S: Serialize>(s: S) -> Result<impl fmt::Display, Infallible> {
Ok(ToJson(s))
pub fn json(value: impl Serialize, indent: impl AsIndent) -> Result<impl fmt::Display, Infallible> {
Ok(ToJson { value, indent })
Copy link
Owner

Choose a reason for hiding this comment

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

Is there reason not to store an Option<'static str> in ToJson::indent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the lifetimes wouldn't work.

}

#[derive(Debug, Clone)]
struct ToJson<S: Serialize>(S);
pub trait AsIndent {
fn as_indent(&self) -> Option<&str>;
}

impl AsIndent for str {
#[inline]
fn as_indent(&self) -> Option<&str> {
Some(self)
}
}

impl AsIndent for String {
#[inline]
fn as_indent(&self) -> Option<&str> {
Some(self)
}
}

impl AsIndent for isize {
fn as_indent(&self) -> Option<&str> {
const SPACES: &str = " ";
match *self < 0 {
true => None,
false => Some(&SPACES[..(*self as usize).min(SPACES.len())]),
}
}
}

impl AsIndent for () {
#[inline]
fn as_indent(&self) -> Option<&str> {
None
}
}

impl<T: AsIndent + ?Sized> AsIndent for &T {
#[inline]
fn as_indent(&self) -> Option<&str> {
T::as_indent(self)
}
}

impl<T: AsIndent> AsIndent for Option<T> {
#[inline]
fn as_indent(&self) -> Option<&str> {
self.as_ref().and_then(T::as_indent)
}
}

impl<T: AsIndent + ?Sized> AsIndent for Box<T> {
#[inline]
fn as_indent(&self) -> Option<&str> {
T::as_indent(self.as_ref())
}
}

impl<S: Serialize> fmt::Display for ToJson<S> {
impl<T: AsIndent + ToOwned + ?Sized> AsIndent for std::borrow::Cow<'_, T> {
#[inline]
fn as_indent(&self) -> Option<&str> {
T::as_indent(self.as_ref())
}
}

impl<T: AsIndent + ?Sized> AsIndent for std::rc::Rc<T> {
#[inline]
fn as_indent(&self) -> Option<&str> {
T::as_indent(self.as_ref())
}
}

impl<T: AsIndent + ?Sized> AsIndent for std::sync::Arc<T> {
#[inline]
fn as_indent(&self) -> Option<&str> {
T::as_indent(self.as_ref())
}
}

#[derive(Debug, Clone)]
struct ToJson<S, I> {
value: S,
indent: I,
}

impl<S: Serialize, I: AsIndent> fmt::Display for ToJson<S, I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
to_writer_pretty(JsonWriter(f), &self.0).map_err(|_| fmt::Error)
let f = JsonWriter(f);
if let Some(indent) = self.indent.as_indent() {
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: would prefer to match this, yielding out the self.indent value and doing an early return for the None case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No indentation is not the same as no beautification. A Some("") prefix still generates newlines, None does not. So both cases are entirely different. I could replace the if with a match, though.

let formatter = PrettyFormatter::with_indent(indent.as_bytes());
let mut serializer = Serializer::with_formatter(f, formatter);
self.value
.serialize(&mut serializer)
.map_err(|_| fmt::Error)
} else {
to_writer(f, &self.value).map_err(|_| fmt::Error)
}
}
}

Expand Down Expand Up @@ -77,20 +165,55 @@ mod tests {
use super::*;

#[test]
fn test_json() {
assert_eq!(json(true).unwrap().to_string(), "true");
assert_eq!(json("foo").unwrap().to_string(), r#""foo""#);
assert_eq!(json(true).unwrap().to_string(), "true");
assert_eq!(json("foo").unwrap().to_string(), r#""foo""#);
fn test_ugly() {
assert_eq!(json(true, ()).unwrap().to_string(), "true");
assert_eq!(json("foo", ()).unwrap().to_string(), r#""foo""#);
assert_eq!(json(true, ()).unwrap().to_string(), "true");
assert_eq!(json("foo", ()).unwrap().to_string(), r#""foo""#);
assert_eq!(
json("<script>").unwrap().to_string(),
json("<script>", ()).unwrap().to_string(),
r#""\u003cscript\u003e""#
);
assert_eq!(
json(vec!["foo", "bar"]).unwrap().to_string(),
json(vec!["foo", "bar"], ()).unwrap().to_string(),
r#"["foo","bar"]"#
);
assert_eq!(json(true, -1).unwrap().to_string(), "true");
assert_eq!(json(true, Some(())).unwrap().to_string(), "true");
assert_eq!(
json(true, &Some(None::<isize>)).unwrap().to_string(),
"true"
);
}

#[test]
fn test_pretty() {
assert_eq!(json(true, "").unwrap().to_string(), "true");
assert_eq!(
json("<script>", Some("")).unwrap().to_string(),
r#""\u003cscript\u003e""#
);
assert_eq!(
json(vec!["foo", "bar"], Some("")).unwrap().to_string(),
r#"[
"foo",
"bar"
]"#
);
assert_eq!(
json(vec!["foo", "bar"], 2).unwrap().to_string(),
r#"[
"foo",
"bar"
]"#
);
assert_eq!(
json(vec!["foo", "bar"], &Some(&"————"))
.unwrap()
.to_string(),
r#"[
————"foo",
————"bar"
]"#
);
}
Expand Down
2 changes: 1 addition & 1 deletion askama/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::fmt::{self, Write};
#[cfg(feature = "serde-json")]
mod json;
#[cfg(feature = "serde-json")]
pub use self::json::json;
pub use self::json::{json, AsIndent};

use askama_escape::{Escaper, MarkupDisplay};
#[cfg(feature = "humansize")]
Expand Down
8 changes: 5 additions & 3 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,12 +1272,14 @@ impl<'a> Generator<'a> {
return Err("the `json` filter requires the `serde-json` feature to be enabled".into());
}

if args.len() != 1 {
return Err("unexpected argument(s) in `json` filter".into());
}
buf.write(CRATE);
buf.write("::filters::json(");
self._visit_args(buf, args)?;
match args.len() {
1 => buf.write(", ()"),
2 => {}
_ => return Err("unexpected argument(s) in `json` filter".into()),
}
buf.write(")?");
Ok(DisplayWrap::Unwrapped)
}
Expand Down
12 changes: 12 additions & 0 deletions book/src/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ Ugly: <script>var data = "{{data|json}}";</script>
Ugly: <script>var data = '{{data|json|safe}}';</script>
```

By default, a compact representation of the data is generated, i.e. no whitespaces are generated
Kijewski marked this conversation as resolved.
Show resolved Hide resolved
between individual values. To generate a readable representation, you can either pass an integer
how many spaces to use as indentation, or you can pass a string that gets used as prefix:

```jinja2
Prefix with four spaces:
<textarea>{{data|tojson(4)}}</textarea>

Prefix with two &nbsp; characters:
<p>{{data|tojson("\u{a0}\u{a0}")}}</p>
```

## Custom Filters
[#custom-filters]: #custom-filters

Expand Down
10 changes: 5 additions & 5 deletions testing/templates/allow-whitespaces.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
{% let hash = &nested_1.nested_2.hash %}
#}

{{ array| json }}
{{ array[..]| json }}{{ array [ .. ]| json }}
{{ array[1..2]| json }}{{ array [ 1 .. 2 ]| json }}
{{ array[1..=2]| json }}{{ array [ 1 ..= 2 ]| json }}
{{ array[(0+1)..(3-1)]| json }}{{ array [ ( 0 + 1 ) .. ( 3 - 1 ) ]| json }}
{{ array| json(2) }}
{{ array[..]| json(2) }}{{ array [ .. ]| json(2) }}
{{ array[1..2]| json(2) }}{{ array [ 1 .. 2 ]| json(2) }}
{{ array[1..=2]| json(2) }}{{ array [ 1 ..= 2 ]| json(2) }}
{{ array[(0+1)..(3-1)]| json(2) }}{{ array [ ( 0 + 1 ) .. ( 3 - 1 ) ]| json(2) }}

{{-1}}{{ -1 }}{{ - 1 }}
{{1+2}}{{ 1+2 }}{{ 1 +2 }}{{ 1+ 2 }} {{ 1 + 2 }}
Expand Down
4 changes: 0 additions & 4 deletions testing/templates/json.html

This file was deleted.

72 changes: 71 additions & 1 deletion testing/tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ fn test_vec_join() {

#[cfg(feature = "serde-json")]
#[derive(Template)]
#[template(path = "json.html")]
#[template(
source = r#"{
"foo": "{{ foo }}",
"bar": {{ bar|json|safe }}
}"#,
ext = "txt"
)]
struct JsonTemplate<'a> {
foo: &'a str,
bar: &'a Value,
Expand All @@ -178,6 +184,37 @@ fn test_json() {
foo: "a",
bar: &val,
};
assert_eq!(
t.render().unwrap(),
r#"{
"foo": "a",
"bar": {"arr":["one",2,true,null]}
}"#
);
}

#[cfg(feature = "serde-json")]
#[derive(Template)]
#[template(
source = r#"{
"foo": "{{ foo }}",
"bar": {{ bar|json(2)|safe }}
}"#,
ext = "txt"
)]
struct PrettyJsonTemplate<'a> {
foo: &'a str,
bar: &'a Value,
}

#[cfg(feature = "serde-json")]
#[test]
fn test_pretty_json() {
let val = json!({"arr": [ "one", 2, true, null ]});
let t = PrettyJsonTemplate {
foo: "a",
bar: &val,
};
// Note: the json filter lacks a way to specify initial indentation
assert_eq!(
t.render().unwrap(),
Expand All @@ -195,6 +232,39 @@ fn test_json() {
);
}

#[cfg(feature = "serde-json")]
#[derive(Template)]
#[template(source = r#"{{ bar|json(indent)|safe }}"#, ext = "txt")]
struct DynamicJsonTemplate<'a> {
bar: &'a Value,
indent: Option<&'a str>,
}

#[cfg(feature = "serde-json")]
#[test]
fn test_dynamic_json() {
let val = json!({"arr": ["one", 2]});
let t = DynamicJsonTemplate {
bar: &val,
indent: Some("?"),
};
assert_eq!(
t.render().unwrap(),
r#"{
?"arr": [
??"one",
??2
?]
}"#
);

let t = DynamicJsonTemplate {
bar: &val,
indent: None,
};
assert_eq!(t.render().unwrap(), r#"{"arr":["one",2]}"#);
}

#[derive(Template)]
#[template(source = "{{ x|mytrim|safe }}", ext = "html")]
struct NestedFilterTemplate {
Expand Down