@@ -14,10 +14,14 @@ type rec t = Js.Json.t =
1414 | Object (Core__Dict .t <t >)
1515 | Array (array <t >)
1616
17+ @unboxed
18+ type replacer = Keys (array <string >) | Replacer ((string , t ) => t )
19+
1720/**
18- `parseExn(string)`
21+ `parseExn(string, ~reviver=? )`
1922
2023Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid.
24+ The reviver describes how the value should be transformed. It is a function which receives a key and a value.
2125It returns a JSON type.
2226
2327## Examples
3135} catch {
3236| Exn.Error(_) => Console.log("error")
3337}
38+
39+ let reviver = (_, value: JSON.t) =>
40+ switch value {
41+ | String(string) => string->String.toUpperCase->JSON.Encode.string
42+ | Number(number) => (number *. 2.0)->JSON.Encode.float
43+ | _ => value
44+ }
45+
46+ let jsonString = `{"hello":"world","someNumber":21}`
47+
48+ try {
49+ JSON.parseExn(jsonString, ~reviver)->Console.log
50+ // { hello: 'WORLD', someNumber: 42 }
51+
52+ JSON.parseExn("", ~reviver)->Console.log
53+ // error
54+ } catch {
55+ | Exn.Error(_) => Console.log("error")
56+ }
3457```
3558
3659## Exceptions
3962*/
4063@raises (Exn .t )
4164@val
42- external parseExn : string => t = "JSON.parse"
65+ external parseExn : ( string , ~ reviver : ( string , t ) => t = ?) => t = "JSON.parse"
4366
4467/**
4568`parseExnWithReviver(string, reviver)`
@@ -50,15 +73,12 @@ It returns a JSON type.
5073
5174## Examples
5275```rescript
53- let reviver = (_, value) => {
54- let valueType = JSON.Classify.classify(value)
55-
56- switch valueType {
76+ let reviver = (_, value: JSON.t) =>
77+ switch value {
5778 | String(string) => string->String.toUpperCase->JSON.Encode.string
5879 | Number(number) => (number *. 2.0)->JSON.Encode.float
5980 | _ => value
6081 }
61- }
6282
6383let jsonString = `{"hello":"world","someNumber":21}`
6484
@@ -77,14 +97,17 @@ try {
7797
7898- Raises a SyntaxError if the string isn't valid JSON.
7999*/
100+ @deprecated ("Use `parseExn` with optional parameter instead" )
80101@raises (Exn .t )
81102@val
82103external parseExnWithReviver : (string , (string , t ) => t ) => t = "JSON.parse"
83104
84105/**
85- `stringify(json)`
106+ `stringify(json, ~replacer=?, ~space=? )`
86107
87108Converts a JSON object to a JSON string.
109+ The replacer describes how the value should be transformed. It is a function which receives a key and a value,
110+ or an array of keys which should be included in the output.
88111If you want to stringify any type, use `JSON.stringifyAny` instead.
89112
90113## Examples
@@ -98,10 +121,32 @@ let json =
98121
99122JSON.stringify(json)
100123// {"foo":"bar","hello":"world","someNumber":42}
124+
125+ JSON.stringify(json, ~space=2)
126+ // {
127+ // "foo": "bar",
128+ // "hello": "world",
129+ // "someNumber": 42
130+ // }
131+
132+ JSON.stringify(json, ~replacer=Keys(["foo", "someNumber"]))
133+ // {"foo":"bar","someNumber":42}
134+
135+ let replacer = JSON.Replacer((_, value) => {
136+ let decodedValue = value->JSON.Decode.string
137+
138+ switch decodedValue {
139+ | Some(string) => string->String.toUpperCase->JSON.Encode.string
140+ | None => value
141+ }
142+ })
143+
144+ JSON.stringify(json, ~replacer)
145+ // {"foo":"BAR","hello":"WORLD","someNumber":42}
101146```
102147*/
103148@val
104- external stringify : t => string = "JSON.stringify"
149+ external stringify : ( t , ~ replacer : replacer = ?, ~ space : int = ?) => string = "JSON.stringify"
105150
106151/**
107152`stringifyWithIndent(json, indentation)`
@@ -126,6 +171,7 @@ JSON.stringifyWithIndent(json, 2)
126171// }
127172```
128173*/
174+ @deprecated ("Use `stringify` with optional parameter instead" )
129175@val
130176external stringifyWithIndent : (t , @as (json ` null` ) _ , int ) => string = "JSON.stringify"
131177
@@ -158,6 +204,7 @@ JSON.stringifyWithReplacer(json, replacer)
158204// {"foo":"BAR","hello":"WORLD","someNumber":42}
159205```
160206*/
207+ @deprecated ("Use `stringify` with optional parameter instead" )
161208@val
162209external stringifyWithReplacer : (t , (string , t ) => t ) => string = "JSON.stringify"
163210
@@ -194,6 +241,7 @@ JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
194241// }
195242```
196243*/
244+ @deprecated ("Use `stringify` with optional parameters instead" )
197245@val
198246external stringifyWithReplacerAndIndent : (t , (string , t ) => t , int ) => string = "JSON.stringify"
199247
@@ -217,6 +265,7 @@ JSON.stringifyWithFilter(json, ["foo", "someNumber"])
217265// {"foo":"bar","someNumber":42}
218266```
219267*/
268+ @deprecated ("Use `stringify` with optional parameter instead" )
220269@val
221270external stringifyWithFilter : (t , array <string >) => string = "JSON.stringify"
222271
@@ -243,13 +292,15 @@ JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
243292// }
244293```
245294*/
295+ @deprecated ("Use `stringify` with optional parameters instead" )
246296@val
247297external stringifyWithFilterAndIndent : (t , array <string >, int ) => string = "JSON.stringify"
248298
249299/**
250- `stringifyAny(any)`
300+ `stringifyAny(any, ~replacer=?, ~space=? )`
251301
252302Converts any type to a JSON string.
303+ The replacer describes how the value should be transformed. It is a function which receives a key and a value.
253304Stringifying a function or `undefined` will return `None`.
254305If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
255306If you want to stringify a JSON object, use `JSON.stringify` instead.
@@ -265,6 +316,28 @@ let dict = Dict.fromArray([
265316JSON.stringifyAny(dict)
266317// {"foo":"bar","hello":"world","someNumber":42}
267318
319+ JSON.stringifyAny(dict, ~space=2)
320+ // {
321+ // "foo": "bar",
322+ // "hello": "world",
323+ // "someNumber": 42
324+ // }
325+
326+ JSON.stringifyAny(dict, ~replacer=Keys(["foo", "someNumber"]))
327+ // {"foo":"bar","someNumber":42}
328+
329+ let replacer = JSON.Replacer((_, value) => {
330+ let decodedValue = value->JSON.Decode.string
331+
332+ switch decodedValue {
333+ | Some(string) => string->String.toUpperCase->JSON.Encode.string
334+ | None => value
335+ }
336+ })
337+
338+ JSON.stringifyAny(dict, ~replacer)
339+ // {"foo":"BAR","hello":"WORLD","someNumber":42}
340+
268341JSON.stringifyAny(() => "hello world")
269342// None
270343
@@ -279,7 +352,8 @@ BigInt.fromInt(0)->JSON.stringifyAny
279352*/
280353@raises (Exn .t )
281354@val
282- external stringifyAny : 'a => option <string > = "JSON.stringify"
355+ external stringifyAny : ('a , ~replacer : replacer = ?, ~space : int = ?) => option <string > =
356+ "JSON.stringify"
283357
284358/**
285359`stringifyAnyWithIndent(any, indentation)`
@@ -316,6 +390,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
316390- Raises a TypeError if the value contains circular references.
317391- Raises a TypeError if the value contains `BigInt`s.
318392*/
393+ @deprecated ("Use `stringifyAny` with optional parameter instead" )
319394@raises (Exn .t )
320395@val
321396external stringifyAnyWithIndent : ('a , @as (json ` null` ) _ , int ) => option <string > = "JSON.stringify"
@@ -361,6 +436,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
361436- Raises a TypeError if the value contains circular references.
362437- Raises a TypeError if the value contains `BigInt`s.
363438*/
439+ @deprecated ("Use `stringifyAny` with optional parameter instead" )
364440@raises
365441@val
366442external stringifyAnyWithReplacer : ('a , (string , t ) => t ) => option <string > = "JSON.stringify"
@@ -410,6 +486,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
410486- Raises a TypeError if the value contains circular references.
411487- Raises a TypeError if the value contains `BigInt`s.
412488*/
489+ @deprecated ("Use `stringifyAny` with optional parameters instead" )
413490@raises
414491@val
415492external stringifyAnyWithReplacerAndIndent : ('a , (string , t ) => t , int ) => option <string > =
@@ -447,6 +524,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
447524- Raises a TypeError if the value contains circular references.
448525- Raises a TypeError if the value contains `BigInt`s.
449526*/
527+ @deprecated ("Use `stringifyAny` with optional parameter instead" )
450528@raises
451529@val
452530external stringifyAnyWithFilter : ('a , array <string >) => string = "JSON.stringify"
@@ -486,6 +564,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
486564- Raises a TypeError if the value contains circular references.
487565- Raises a TypeError if the value contains `BigInt`s.
488566*/
567+ @deprecated ("Use `stringifyAny` with optional parameters instead" )
489568@raises
490569@val
491570external stringifyAnyWithFilterAndIndent : ('a , array <string >, int ) => string = "JSON.stringify"
0 commit comments