@@ -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: 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
+ }
34
57
```
35
58
36
59
## Exceptions
39
62
*/
40
63
@raises(Exn.t)
41
64
@val
42
- external parseExn: string => t = "JSON.parse"
65
+ external parseExn: ( string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
43
66
44
67
/**
45
68
`parseExnWithReviver(string, reviver)`
@@ -50,15 +73,12 @@ It returns a JSON type.
50
73
51
74
## Examples
52
75
```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 {
57
78
| String(string) => string->String.toUpperCase->JSON.Encode.string
58
79
| Number(number) => (number *. 2.0)->JSON.Encode.float
59
80
| _ => value
60
81
}
61
- }
62
82
63
83
let jsonString = `{"hello":"world","someNumber":21}`
64
84
@@ -77,14 +97,17 @@ try {
77
97
78
98
- Raises a SyntaxError if the string isn't valid JSON.
79
99
*/
100
+ @deprecated("Use `parseExn` with optional parameter instead")
80
101
@raises(Exn.t)
81
102
@val
82
103
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
83
104
84
105
/**
85
- `stringify(json)`
106
+ `stringify(json, ~replacer=?, ~space=? )`
86
107
87
108
Converts 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.
88
111
If you want to stringify any type, use `JSON.stringifyAny` instead.
89
112
90
113
## Examples
@@ -98,10 +121,32 @@ let json =
98
121
99
122
JSON.stringify(json)
100
123
// {"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}
101
146
```
102
147
*/
103
148
@val
104
- external stringify: t => string = "JSON.stringify"
149
+ external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"
105
150
106
151
/**
107
152
`stringifyWithIndent(json, indentation)`
@@ -126,6 +171,7 @@ JSON.stringifyWithIndent(json, 2)
126
171
// }
127
172
```
128
173
*/
174
+ @deprecated("Use `stringify` with optional parameter instead")
129
175
@val
130
176
external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
131
177
@@ -158,6 +204,7 @@ JSON.stringifyWithReplacer(json, replacer)
158
204
// {"foo":"BAR","hello":"WORLD","someNumber":42}
159
205
```
160
206
*/
207
+ @deprecated("Use `stringify` with optional parameter instead")
161
208
@val
162
209
external stringifyWithReplacer: (t, (string, t) => t) => string = "JSON.stringify"
163
210
@@ -194,6 +241,7 @@ JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
194
241
// }
195
242
```
196
243
*/
244
+ @deprecated("Use `stringify` with optional parameters instead")
197
245
@val
198
246
external stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string = "JSON.stringify"
199
247
@@ -217,6 +265,7 @@ JSON.stringifyWithFilter(json, ["foo", "someNumber"])
217
265
// {"foo":"bar","someNumber":42}
218
266
```
219
267
*/
268
+ @deprecated("Use `stringify` with optional parameter instead")
220
269
@val
221
270
external stringifyWithFilter: (t, array<string>) => string = "JSON.stringify"
222
271
@@ -243,13 +292,15 @@ JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
243
292
// }
244
293
```
245
294
*/
295
+ @deprecated("Use `stringify` with optional parameters instead")
246
296
@val
247
297
external stringifyWithFilterAndIndent: (t, array<string>, int) => string = "JSON.stringify"
248
298
249
299
/**
250
- `stringifyAny(any)`
300
+ `stringifyAny(any, ~replacer=?, ~space=? )`
251
301
252
302
Converts 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.
253
304
Stringifying a function or `undefined` will return `None`.
254
305
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
255
306
If you want to stringify a JSON object, use `JSON.stringify` instead.
@@ -265,6 +316,28 @@ let dict = Dict.fromArray([
265
316
JSON.stringifyAny(dict)
266
317
// {"foo":"bar","hello":"world","someNumber":42}
267
318
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
+
268
341
JSON.stringifyAny(() => "hello world")
269
342
// None
270
343
@@ -279,7 +352,8 @@ BigInt.fromInt(0)->JSON.stringifyAny
279
352
*/
280
353
@raises(Exn.t)
281
354
@val
282
- external stringifyAny: 'a => option<string> = "JSON.stringify"
355
+ external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option<string> =
356
+ "JSON.stringify"
283
357
284
358
/**
285
359
`stringifyAnyWithIndent(any, indentation)`
@@ -316,6 +390,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
316
390
- Raises a TypeError if the value contains circular references.
317
391
- Raises a TypeError if the value contains `BigInt`s.
318
392
*/
393
+ @deprecated("Use `stringifyAny` with optional parameter instead")
319
394
@raises(Exn.t)
320
395
@val
321
396
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
@@ -361,6 +436,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
361
436
- Raises a TypeError if the value contains circular references.
362
437
- Raises a TypeError if the value contains `BigInt`s.
363
438
*/
439
+ @deprecated("Use `stringifyAny` with optional parameter instead")
364
440
@raises
365
441
@val
366
442
external stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string> = "JSON.stringify"
@@ -410,6 +486,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
410
486
- Raises a TypeError if the value contains circular references.
411
487
- Raises a TypeError if the value contains `BigInt`s.
412
488
*/
489
+ @deprecated("Use `stringifyAny` with optional parameters instead")
413
490
@raises
414
491
@val
415
492
external stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string> =
@@ -447,6 +524,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
447
524
- Raises a TypeError if the value contains circular references.
448
525
- Raises a TypeError if the value contains `BigInt`s.
449
526
*/
527
+ @deprecated("Use `stringifyAny` with optional parameter instead")
450
528
@raises
451
529
@val
452
530
external stringifyAnyWithFilter: ('a, array<string>) => string = "JSON.stringify"
@@ -486,6 +564,7 @@ BigInt.fromInt(0)->JSON.stringifyAny
486
564
- Raises a TypeError if the value contains circular references.
487
565
- Raises a TypeError if the value contains `BigInt`s.
488
566
*/
567
+ @deprecated("Use `stringifyAny` with optional parameters instead")
489
568
@raises
490
569
@val
491
570
external stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string = "JSON.stringify"
0 commit comments