Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #98 from digitalbazaar/master

indent parameter for json_encode method
  • Loading branch information...
commit 8c22dd292af06d99b76ea2109b06c34f7aca12c6 2 parents 3c8ed7d + 7eba7e4
@paularmstrong paularmstrong authored
Showing with 9 additions and 4 deletions.
  1. +6 −2 docs/filters.md
  2. +2 −2 lib/filters.js
  3. +1 −0  lib/filters.test.js
View
8 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. <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>
View
4 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) {
View
1  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] }}, '{&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();
};
Please sign in to comment.
Something went wrong with that request. Please try again.