Skip to content

Commit

Permalink
Implement indent parameter for tojson (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Jul 30, 2024
1 parent dddd5eb commit a9a3ea2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to MiniJinja are documented here.

## 2.1.1

- Added `indent` parameter to `tojson` filter. #546

## 2.1.0

- minijinja-cli now supports `.ini` files. #532
Expand Down
24 changes: 21 additions & 3 deletions minijinja/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,27 @@ mod builtins {
/// ```
#[cfg_attr(docsrs, doc(cfg(all(feature = "builtins", feature = "json"))))]
#[cfg(feature = "json")]
pub fn tojson(value: Value, pretty: Option<bool>) -> Result<Value, Error> {
if pretty.unwrap_or(false) {
serde_json::to_string_pretty(&value)
pub fn tojson(value: Value, indent: Option<Value>, args: Kwargs) -> Result<Value, Error> {
let indent = match indent {
Some(indent) => Some(indent),
None => ok!(args.get("indent")),
};
let indent = match indent {
None => None,
Some(ref val) => match bool::try_from(val.clone()).ok() {
Some(true) => Some(2),
Some(false) => None,
None => Some(ok!(usize::try_from(val.clone()))),
},
};
args.assert_all_used()?;
if let Some(indent) = indent {
let mut out = Vec::<u8>::new();
let indentation = " ".repeat(indent);
let formatter = serde_json::ser::PrettyFormatter::with_indent(indentation.as_bytes());
let mut s = serde_json::Serializer::with_formatter(&mut out, formatter);
serde::Serialize::serialize(&value, &mut s)
.map(|_| unsafe { String::from_utf8_unchecked(out) })
} else {
serde_json::to_string(&value)
}
Expand Down
1 change: 0 additions & 1 deletion minijinja/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::get_first)]
#![allow(clippy::default_constructed_unit_structs)]
#![allow(where_clauses_object_safety)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(missing_docs)]
#![doc(html_logo_url = "https://github.com/mitsuhiko/minijinja/raw/main/artwork/logo-square.png")]
Expand Down
7 changes: 7 additions & 0 deletions minijinja/tests/inputs/tojson.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{}
---
{{ [1, 2, 3]|tojson }}
{{ [1, 2, 3]|tojson(true) }}
{{ [1, 2, 3]|tojson(indent=4) }}
{{ [1, 2, 3]|tojson(1) }}
{{ [1, 2, 3]|tojson(8) }}
27 changes: 27 additions & 0 deletions minijinja/tests/snapshots/test_templates__vm@tojson.txt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: minijinja/tests/test_templates.rs
description: "{{ [1, 2, 3]|tojson }}\n{{ [1, 2, 3]|tojson(true) }}\n{{ [1, 2, 3]|tojson(indent=4) }}\n{{ [1, 2, 3]|tojson(1) }}\n{{ [1, 2, 3]|tojson(8) }}"
info: {}
input_file: minijinja/tests/inputs/tojson.txt
---
[1,2,3]
[
1,
2,
3
]
[
1,
2,
3
]
[
1,
2,
3
]
[
1,
2,
3
]

0 comments on commit a9a3ea2

Please sign in to comment.