Skip to content

Commit e52e6e9

Browse files
authored
Merge pull request #2737 from arnarthor/master
Add stringifyWithSpace to Js_json
2 parents 0777052 + 4e1302b commit e52e6e9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

jscomp/others/js_json.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ external booleanArray : Js.boolean array -> t = "%identity"
127127
external objectArray : t Js_dict.t array -> t = "%identity"
128128
external stringify: t -> string = "stringify"
129129
[@@bs.val] [@@bs.scope "JSON"]
130+
external stringifyWithSpace: t -> (_ [@bs.as {json|null|json}]) -> int -> string = "stringify"
131+
[@@bs.val] [@@bs.scope "JSON"]

jscomp/others/js_json.mli

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ Js.log \@\@ Js.Json.stringify (Js.Json.object_ dict)
201201
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify> MDN
202202
*)
203203

204+
external stringifyWithSpace: t -> (_ [@bs.as {json|null|json}]) -> int -> string = "stringify"
205+
[@@bs.val] [@@bs.scope "JSON"]
206+
(** [stringify json] formats the JSON data structure as a string
207+
208+
{b Returns} the string representation of a given JSON data structure
209+
210+
@example {[
211+
(* Creates and stringifies a simple JS object with spacing *)
212+
213+
let dict = Js.Dict.empty () in
214+
Js.Dict.set dict "name" (Js.Json.string "John Doe");
215+
Js.Dict.set dict "age" (Js.Json.numberOfInt 30);
216+
Js.Dict.set dict "likes"
217+
(Js.Json.stringArray [|"bucklescript";"ocaml";"js"|]);
218+
219+
Js.log \@\@ Js.Json.stringify (Js.Json.object_ dict) 2
220+
]}
221+
222+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify> MDN
223+
*)
224+
225+
204226
external stringifyAny : 'a -> string option = "stringify"
205227
[@@bs.val] [@@bs.return undefined_to_opt] [@@bs.scope "JSON"]
206228
(** [stringifyAny value] formats any [value] into a JSON string

0 commit comments

Comments
 (0)