Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added indent parameter to json_encode filter to support pretty-printing.
  • Loading branch information
msporny committed Aug 2, 2012
1 parent 3c8ed7d commit 7eba7e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docs/filters.md
Expand Up @@ -376,9 +376,13 @@ If the value is an Array, you can join each value with a delimiter and return it


1. <var>**glue**</var> (_string_) Concatenation string to join each item in the array with. 1. <var>**glue**</var> (_string_) Concatenation string to join each item in the array with.


### json_encode <a name="json_encode" href="#json_encode">#</a> ### json_encode(indent) <a name="json_encode" href="#json_encode">#</a>


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. <var>**indent**</var> (_number_) _optional_ if greater than zero, the JSON will be pretty-printed using the number of spaces specified by the **indent** parameter.


### last <a name="last" href="#last">#</a> ### last <a name="last" href="#last">#</a>


Expand Down
4 changes: 2 additions & 2 deletions lib/filters.js
Expand Up @@ -130,8 +130,8 @@ exports.join = function (input, separator) {
return input; return input;
}; };


exports.json_encode = function (input) { exports.json_encode = function (input, indent) {
return JSON.stringify(input); return JSON.stringify(input, null, indent || 0);
}; };


exports.last = function (input) { exports.last = function (input) {
Expand Down
1 change: 1 addition & 0 deletions lib/filters.test.js
Expand Up @@ -210,6 +210,7 @@ exports.join = function (test) {


exports.json_encode = function (test) { exports.json_encode = function (test) {
testFilter(test, 'json_encode', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:[1,2,3]}'); testFilter(test, 'json_encode', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{&quot;foo&quot;:&quot;bar&quot;,&quot;baz&quot;:[1,2,3]}');
testFilter(test, 'json_encode(2)', { v: { foo: 'bar', baz: [1, 2, 3] }}, '{\n &quot;foo&quot;: &quot;bar&quot;,\n &quot;baz&quot;: [\n 1,\n 2,\n 3\n ]\n}');
test.done(); test.done();
}; };


Expand Down

0 comments on commit 7eba7e4

Please sign in to comment.