@@ -14,10 +14,14 @@ type rec t = Js.Json.t =
14
14
| Object(Core__Dict.t<t>)
15
15
| Array(array<t>)
16
16
17
+ @unboxed
18
+ type replacer = Keys(array<string>) | Replacer((string, t) => t)
19
+
17
20
/**
18
- `parseExn(string)`
21
+ `parseExn(string, ~reviver=? )`
19
22
20
23
Parses 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.
21
25
It returns a JSON type.
22
26
23
27
## Examples
31
35
} catch {
32
36
| Exn.Error(_) => Console.log("error")
33
37
}
38
+
39
+ let reviver = (_, value) => {
40
+ let valueType = JSON.Classify.classify(value)
41
+
42
+ switch valueType {
43
+ | String(string) => string->String.toUpperCase->JSON.Encode.string
44
+ | Number(number) => (number *. 2.0)->JSON.Encode.float
45
+ | _ => value
46
+ }
47
+ }
48
+
49
+ let jsonString = `{"hello":"world","someNumber":21}`
50
+
51
+ try {
52
+ JSON.parseExn(jsonString, ~reviver)->Console.log
53
+ // { hello: 'WORLD', someNumber: 42 }
54
+
55
+ JSON.parseExn("", ~reviver)->Console.log
56
+ // error
57
+ } catch {
58
+ | Exn.Error(_) => Console.log("error")
59
+ }
34
60
```
35
61
36
62
## Exceptions
39
65
*/
40
66
@raises(Exn.t)
41
67
@val
42
- external parseExn: string => t = "JSON.parse"
68
+ external parseExn: ( string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
43
69
44
70
/**
45
71
`parseExnWithReviver(string, reviver)`
@@ -77,14 +103,17 @@ try {
77
103
78
104
- Raises a SyntaxError if the string isn't valid JSON.
79
105
*/
106
+ @deprecated("Use `parseExn` with optional parameter instead")
80
107
@raises(Exn.t)
81
108
@val
82
109
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
83
110
84
111
/**
85
- `stringify(json)`
112
+ `stringify(json, ~replacer=?, ~space=? )`
86
113
87
114
Converts a JSON object to a JSON string.
115
+ The replacer describes how the value should be transformed. It is a function which receives a key and a value,
116
+ or an array of keys which should be included in the output.
88
117
If you want to stringify any type, use `JSON.stringifyAny` instead.
89
118
90
119
## Examples
@@ -98,10 +127,32 @@ let json =
98
127
99
128
JSON.stringify(json)
100
129
// {"foo":"bar","hello":"world","someNumber":42}
130
+
131
+ JSON.stringify(json, ~space=2)
132
+ // {
133
+ // "foo": "bar",
134
+ // "hello": "world",
135
+ // "someNumber": 42
136
+ // }
137
+
138
+ JSON.stringify(json, ~replacer=Keys(["foo", "someNumber"]))
139
+ // {"foo":"bar","someNumber":42}
140
+
141
+ let replacer = JSON.Replacer((_, value) => {
142
+ let decodedValue = value->JSON.Decode.string
143
+
144
+ switch decodedValue {
145
+ | Some(string) => string->String.toUpperCase->JSON.Encode.string
146
+ | None => value
147
+ }
148
+ })
149
+
150
+ JSON.stringify(json, ~replacer)
151
+ // {"foo":"BAR","hello":"WORLD","someNumber":42}
101
152
```
102
153
*/
103
154
@val
104
- external stringify: t => string = "JSON.stringify"
155
+ external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"
105
156
106
157
/**
107
158
`stringifyWithIndent(json, indentation)`
@@ -126,6 +177,7 @@ JSON.stringifyWithIndent(json, 2)
126
177
// }
127
178
```
128
179
*/
180
+ @deprecated("Use `stringify` with optional parameter instead")
129
181
@val
130
182
external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
131
183
@@ -158,6 +210,7 @@ JSON.stringifyWithReplacer(json, replacer)
158
210
// {"foo":"BAR","hello":"WORLD","someNumber":42}
159
211
```
160
212
*/
213
+ @deprecated("Use `stringify` with optional parameter instead")
161
214
@val
162
215
external stringifyWithReplacer: (t, (string, t) => t) => string = "JSON.stringify"
163
216
@@ -194,6 +247,7 @@ JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
194
247
// }
195
248
```
196
249
*/
250
+ @deprecated("Use `stringify` with optional parameters instead")
197
251
@val
198
252
external stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string = "JSON.stringify"
199
253
@@ -217,6 +271,7 @@ JSON.stringifyWithFilter(json, ["foo", "someNumber"])
217
271
// {"foo":"bar","someNumber":42}
218
272
```
219
273
*/
274
+ @deprecated("Use `stringify` with optional parameter instead")
220
275
@val
221
276
external stringifyWithFilter: (t, array<string>) => string = "JSON.stringify"
222
277
@@ -243,13 +298,15 @@ JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
243
298
// }
244
299
```
245
300
*/
301
+ @deprecated("Use `stringify` with optional parameters instead")
246
302
@val
247
303
external stringifyWithFilterAndIndent: (t, array<string>, int) => string = "JSON.stringify"
248
304
249
305
/**
250
- `stringifyAny(any)`
306
+ `stringifyAny(any, ~replacer=?, ~space=? )`
251
307
252
308
Converts any type to a JSON string.
309
+ The replacer describes how the value should be transformed. It is a function which receives a key and a value.
253
310
Stringifying a function or `undefined` will return `None`.
254
311
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
255
312
If you want to stringify a JSON object, use `JSON.stringify` instead.
@@ -265,6 +322,28 @@ let dict = Dict.fromArray([
265
322
JSON.stringifyAny(dict)
266
323
// {"foo":"bar","hello":"world","someNumber":42}
267
324
325
+ JSON.stringifyAny(dict, ~space=2)
326
+ // {
327
+ // "foo": "bar",
328
+ // "hello": "world",
329
+ // "someNumber": 42
330
+ // }
331
+
332
+ JSON.stringifyAny(dict, ~replacer=Keys(["foo", "someNumber"]))
333
+ // {"foo":"bar","someNumber":42}
334
+
335
+ let replacer = JSON.Replacer((_, value) => {
336
+ let decodedValue = value->JSON.Decode.string
337
+
338
+ switch decodedValue {
339
+ | Some(string) => string->String.toUpperCase->JSON.Encode.string
340
+ | None => value
341
+ }
342
+ })
343
+
344
+ JSON.stringifyAny(dict, ~replacer)
345
+ // {"foo":"BAR","hello":"WORLD","someNumber":42}
346
+
268
347
JSON.stringifyAny(() => "hello world")
269
348
// None
270
349
@@ -279,7 +358,8 @@ BigInt.fromInt(0)->JSON.stringifyAny
279
358
*/
280
359
@raises(Exn.t)
281
360
@val
282
- external stringifyAny: 'a => option<string> = "JSON.stringify"
361
+ external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option<string> =
362
+ "JSON.stringify"
283
363
284
364
/**
285
365
`stringifyAnyWithIndent(any, indentation)`
@@ -316,6 +396,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
316
396
- Raises a TypeError if the value contains circular references.
317
397
- Raises a TypeError if the value contains `BigInt`s.
318
398
*/
399
+ @deprecated("Use `stringifyAny` with optional parameter instead")
319
400
@raises(Exn.t)
320
401
@val
321
402
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
@@ -361,6 +442,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
361
442
- Raises a TypeError if the value contains circular references.
362
443
- Raises a TypeError if the value contains `BigInt`s.
363
444
*/
445
+ @deprecated("Use `stringifyAny` with optional parameter instead")
364
446
@raises
365
447
@val
366
448
external stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string> = "JSON.stringify"
@@ -410,6 +492,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
410
492
- Raises a TypeError if the value contains circular references.
411
493
- Raises a TypeError if the value contains `BigInt`s.
412
494
*/
495
+ @deprecated("Use `stringifyAny` with optional parameters instead")
413
496
@raises
414
497
@val
415
498
external stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string> =
@@ -447,6 +530,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
447
530
- Raises a TypeError if the value contains circular references.
448
531
- Raises a TypeError if the value contains `BigInt`s.
449
532
*/
533
+ @deprecated("Use `stringifyAny` with optional parameter instead")
450
534
@raises
451
535
@val
452
536
external stringifyAnyWithFilter: ('a, array<string>) => string = "JSON.stringify"
@@ -486,6 +570,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
486
570
- Raises a TypeError if the value contains circular references.
487
571
- Raises a TypeError if the value contains `BigInt`s.
488
572
*/
573
+ @deprecated("Use `stringifyAny` with optional parameters instead")
489
574
@raises
490
575
@val
491
576
external stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string = "JSON.stringify"
0 commit comments