diff --git a/docs/filters.md b/docs/filters.md index 5fd8643e..34620e04 100644 --- a/docs/filters.md +++ b/docs/filters.md @@ -376,9 +376,13 @@ If the value is an Array, you can join each value with a delimiter and return it 1. **glue** (_string_) Concatenation string to join each item in the array with. -### json_encode # +### json_encode(indent) # -Return a JSON string of the variable. +Return a JSON string of the variable, optionally pretty-printing the string if **indent** is specified. + +#### Arguments + +1. **indent** (_number_) _optional_ if greater than zero, the JSON will be pretty-printed using the number of spaces specified by the **indent** parameter. ### last # diff --git a/lib/filters.js b/lib/filters.js index 6e84b0f4..677882df 100644 --- a/lib/filters.js +++ b/lib/filters.js @@ -130,8 +130,8 @@ exports.join = function (input, separator) { return input; }; -exports.json_encode = function (input) { - return JSON.stringify(input); +exports.json_encode = function (input, indent) { + return JSON.stringify(input, null, indent || 0); }; exports.last = function (input) { diff --git a/lib/filters.test.js b/lib/filters.test.js index b7df0db2..fa8e495e 100644 --- a/lib/filters.test.js +++ b/lib/filters.test.js @@ -210,6 +210,7 @@ exports.join = function (test) { exports.json_encode = function (test) { testFilter(test, 'json_encode', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{"foo":"bar","baz":[1,2,3]}'); + testFilter(test, 'json_encode(2)', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{\n "foo": "bar",\n "baz": [\n 1,\n 2,\n 3\n ]\n}'); test.done(); };