Skip to content

Commit 84a779b

Browse files
author
Serhii Khoma
committed
feat: stringifyWithSpace and stringifyWith -> add method similar to elm encode
1 parent 0d98c56 commit 84a779b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Data/Argonaut/Core.js

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

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

src/Data/Argonaut/Core.purs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module Data.Argonaut.Core
3737
, jsonEmptyObject
3838
, jsonSingletonObject
3939
, stringify
40+
, stringifyWith
41+
, stringifyWithSpace
4042
) where
4143

4244
import Prelude
@@ -45,6 +47,7 @@ import Data.Function.Uncurried (Fn5, runFn5, Fn7, runFn7)
4547
import Data.Maybe (Maybe(..))
4648
import Foreign.Object (Object)
4749
import Foreign.Object as Obj
50+
import Unsafe.Coerce (unsafeCoerce)
4851

4952
-- | The type of JSON data. The underlying representation is the same as what
5053
-- | would be returned from JavaScript's `JSON.parse` function; that is,
@@ -178,13 +181,13 @@ toObject = toJsonType caseJsonObject
178181

179182
-- Encoding
180183

181-
-- | Construct `Json` from a `Boolean` value
184+
-- | Construct `Json` from a `Boolean` value
182185
foreign import fromBoolean :: Boolean -> Json
183186

184-
-- | Construct `Json` from a `Number` value
187+
-- | Construct `Json` from a `Number` value
185188
foreign import fromNumber :: Number -> Json
186189

187-
-- | Construct `Json` from a `String` value. If you would like to parse a string
190+
-- | Construct `Json` from a `String` value. If you would like to parse a string
188191
-- | of JSON into valid `Json`, see `jsonParser`.
189192
foreign import fromString :: String -> Json
190193

@@ -235,6 +238,17 @@ jsonSingletonObject key val = fromObject (Obj.singleton key val)
235238
-- | string value, see `fromString`.
236239
foreign import stringify :: Json -> String
237240

241+
-- | Converts a `Json` value to a JSON string.
242+
-- | The first `Int` argument specifies the amount of white space characters to use as indentation.
243+
-- | 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.
244+
stringifyWithSpace :: Int -> Json -> String
245+
stringifyWithSpace = unsafeCoerce stringifyWith
246+
247+
-- | Converts a `Json` value to a JSON string.
248+
-- | The first `String` argument is used as indentation.
249+
-- | If string is longer than 10 characters - only the first 10 characters are used.
250+
foreign import stringifyWith :: String -> Json -> String
251+
238252
foreign import _caseJson
239253
:: forall z
240254
. Fn7

0 commit comments

Comments
 (0)