Skip to content

Commit 6b17010

Browse files
author
Serhii Khoma
authored
Add stringifyWithIndent (#41)
1 parent 5ad5251 commit 6b17010

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Data/Argonaut/Core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ exports.stringify = function (j) {
1717
return JSON.stringify(j);
1818
};
1919

20+
exports.stringifyWithIndent = function (i) {
21+
return function (j) {
22+
return JSON.stringify(j, null, i);
23+
};
24+
};
25+
2026
function isArray(a) {
2127
return Object.prototype.toString.call(a) === "[object Array]";
2228
}

src/Data/Argonaut/Core.purs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module Data.Argonaut.Core
3737
, jsonEmptyObject
3838
, jsonSingletonObject
3939
, stringify
40+
, stringifyWithIndent
4041
) where
4142

4243
import Prelude
@@ -178,13 +179,13 @@ toObject = toJsonType caseJsonObject
178179

179180
-- Encoding
180181

181-
-- | Construct `Json` from a `Boolean` value
182+
-- | Construct `Json` from a `Boolean` value
182183
foreign import fromBoolean :: Boolean -> Json
183184

184-
-- | Construct `Json` from a `Number` value
185+
-- | Construct `Json` from a `Number` value
185186
foreign import fromNumber :: Number -> Json
186187

187-
-- | Construct `Json` from a `String` value. If you would like to parse a string
188+
-- | Construct `Json` from a `String` value. If you would like to parse a string
188189
-- | of JSON into valid `Json`, see `jsonParser`.
189190
foreign import fromString :: String -> Json
190191

@@ -235,6 +236,11 @@ jsonSingletonObject key val = fromObject (Obj.singleton key val)
235236
-- | string value, see `fromString`.
236237
foreign import stringify :: Json -> String
237238

239+
-- | Converts a `Json` value to a JSON string.
240+
-- | The first `Int` argument specifies the amount of white space characters to use as indentation.
241+
-- | This number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used.
242+
foreign import stringifyWithIndent :: Int -> Json -> String
243+
238244
foreign import _caseJson
239245
:: forall z
240246
. Fn7

0 commit comments

Comments
 (0)