Skip to content

Loading…

indent parameter for json_encode method #98

Merged
merged 1 commit into from

2 participants

@msporny

Changes:

  • Added an indent parameter to json_encode filter to support pretty-printing.
  • Added unit test to test new functionality.
  • Updated documentation to reflect new parameter for json_encode
@paularmstrong

Looks good. Thanks!

@paularmstrong paularmstrong merged commit 8c22dd2 into paularmstrong:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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();
};
Something went wrong with that request. Please try again.