-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCore__JSON.resi
762 lines (614 loc) · 18.8 KB
/
Core__JSON.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/***
Functions for interacting with JSON.
*/
/**
A type representing a JSON object.
*/
@unboxed
type rec t = Js.Json.t =
| Boolean(bool)
| @as(null) Null
| String(string)
| Number(float)
| Object(Core__Dict.t<t>)
| Array(array<t>)
@unboxed
type replacer = Keys(array<string>) | Replacer((string, t) => t)
/**
`parseExn(string, ~reviver=?)`
Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid.
The reviver describes how the value should be transformed. It is a function which receives a key and a value.
It returns a JSON type.
## Examples
```rescript
try {
let _ = JSON.parseExn(`{"foo":"bar","hello":"world"}`)
// { foo: 'bar', hello: 'world' }
let _ = JSON.parseExn("")
// error
} catch {
| Error.Error(_) => Console.log("error")
}
let reviver = (_, value: JSON.t) =>
switch value {
| String(string) => string->String.toUpperCase->JSON.Encode.string
| Number(number) => (number *. 2.0)->JSON.Encode.float
| _ => value
}
let jsonString = `{"hello":"world","someNumber":21}`
try {
JSON.parseExn(jsonString, ~reviver)->Console.log
// { hello: 'WORLD', someNumber: 42 }
JSON.parseExn("", ~reviver)->Console.log
// error
} catch {
| Error.Error(_) => Console.log("error")
}
```
## Exceptions
- Raises a SyntaxError (Error.t) if the string isn't valid JSON.
*/
@raises(Error.t)
@val
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
/**
`parseExnWithReviver(string, reviver)`
Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid.
The reviver describes how the value should be transformed. It is a function which receives a key and a value.
It returns a JSON type.
## Examples
```rescript
let reviver = (_, value: JSON.t) =>
switch value {
| String(string) => string->String.toUpperCase->JSON.Encode.string
| Number(number) => (number *. 2.0)->JSON.Encode.float
| _ => value
}
let jsonString = `{"hello":"world","someNumber":21}`
try {
JSON.parseExnWithReviver(jsonString, reviver)->Console.log
// { hello: 'WORLD', someNumber: 42 }
JSON.parseExnWithReviver("", reviver)->Console.log
// error
} catch {
| Error.Error(_) => Console.log("error")
}
```
## Exceptions
- Raises a SyntaxError if the string isn't valid JSON.
*/
@deprecated("Use `parseExn` with optional parameter instead")
@raises(Error.t)
@val
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
/**
`stringify(json, ~replacer=?, ~space=?)`
Converts a JSON object to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value,
or an array of keys which should be included in the output.
If you want to stringify any type, use `JSON.stringifyAny` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringify(json)
// {"foo":"bar","hello":"world","someNumber":42}
JSON.stringify(json, ~space=2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
JSON.stringify(json, ~replacer=Keys(["foo", "someNumber"]))
// {"foo":"bar","someNumber":42}
let replacer = JSON.Replacer((_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
})
JSON.stringify(json, ~replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
```
*/
@val
external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"
/**
`stringifyWithIndent(json, indentation)`
Converts a JSON object to a JSON string. The output will be indented.
If you want to stringify any type, use `JSON.stringifyAnyWithIndent` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithIndent(json, 2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
```
*/
@deprecated("Use `stringify` with optional parameter instead")
@val
external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
/**
`stringifyWithReplacer(json, replacer)`
Converts a JSON object to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
If you want to stringify any type, use `JSON.stringifyAnyWithReplacer` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
let replacer = (_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyWithReplacer(json, replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
```
*/
@deprecated("Use `stringify` with optional parameter instead")
@val
external stringifyWithReplacer: (t, (string, t) => t) => string = "JSON.stringify"
/**
`stringifyWithReplacerAndIndent(json, replacer, indentation)`
Converts a JSON object to a JSON string. The output will be indented.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
If you want to stringify any type, use `JSON.stringifyAnyWithReplacerAndIndent` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
let replacer = (_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
// {
// "foo": "BAR",
// "hello": "WORLD",
// "someNumber": 42
// }
```
*/
@deprecated("Use `stringify` with optional parameters instead")
@val
external stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string = "JSON.stringify"
/**
`stringifyWithFilter(json, filter)`
Converts a JSON object to a JSON string.
The filter is an array of keys, which should be included in the output.
If you want to stringify any type, use `JSON.stringifyAnyWithFilter` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithFilter(json, ["foo", "someNumber"])
// {"foo":"bar","someNumber":42}
```
*/
@deprecated("Use `stringify` with optional parameter instead")
@val
external stringifyWithFilter: (t, array<string>) => string = "JSON.stringify"
/**
`stringifyWithFilterAndIndent(json, filter, indentation)`
Converts a JSON object to a JSON string. The output will be indented.
The filter is an array of keys, which should be included in the output.
If you want to stringify any type, use `JSON.stringifyAnyWithFilterAndIndent` instead.
## Examples
```rescript
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
// {
// "foo": "bar",
// "someNumber": 42
// }
```
*/
@deprecated("Use `stringify` with optional parameters instead")
@val
external stringifyWithFilterAndIndent: (t, array<string>, int) => string = "JSON.stringify"
/**
`stringifyAny(any, ~replacer=?, ~space=?)`
Converts any type to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringify` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAny(dict)
// {"foo":"bar","hello":"world","someNumber":42}
JSON.stringifyAny(dict, ~space=2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
JSON.stringifyAny(dict, ~replacer=Keys(["foo", "someNumber"]))
// {"foo":"bar","someNumber":42}
let replacer = JSON.Replacer((_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
})
JSON.stringifyAny(dict, ~replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@raises(Error.t)
@val
external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option<string> =
"JSON.stringify"
/**
`stringifyAnyWithIndent(any, indentation)`
Converts any type to a JSON string. The output will be indented.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringifyWithIndent` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithIndent(dict, 2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameter instead")
@raises(Error.t)
@val
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
/**
`stringifyAnyWithReplacer(json, replacer)`
Converts any type to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringifyWithReplacer` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
let replacer = (_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyAnyWithReplacer(dict, replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameter instead")
@raises
@val
external stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string> = "JSON.stringify"
/**
`stringifyAnyWithReplacerAndIndent(json, replacer, indentation)`
Converts any type to a JSON string. The output will be indented.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringifyWithReplacerAndIndent` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
let replacer = (_, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyAnyWithReplacerAndIndent(dict, replacer, 2)
// {
// "foo": "BAR",
// "hello": "WORLD",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameters instead")
@raises
@val
external stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string> =
"JSON.stringify"
/**
`stringifyAnyWithFilter(json, filter)`
Converts any type to a JSON string.
The filter is an array of keys, which should be included in the output.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringifyWithFilter` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithFilter(dict, ["foo", "someNumber"])
// {"foo": "bar","someNumber": 42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameter instead")
@raises
@val
external stringifyAnyWithFilter: ('a, array<string>) => string = "JSON.stringify"
/**
`stringifyAnyWithFilterAndIndent(json, filter, indentation)`
Converts any type to a JSON string. The output will be indented.
The filter is an array of keys, which should be included in the output.
Stringifying a function or `undefined` will return `None`.
If the value contains circular references or `BigInt`s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use `JSON.stringifyWithFilterAndIndent` instead.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithFilterAndIndent(dict, ["foo", "someNumber"], 2)
// {
// "foo": "bar",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
```
## Exceptions
- Raises a TypeError if the value contains circular references.
- Raises a TypeError if the value contains `BigInt`s.
*/
@deprecated("Use `stringifyAny` with optional parameters instead")
@raises
@val
external stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string = "JSON.stringify"
module Classify: {
/**
A type representing a JavaScript type.
*/
type t =
| Bool(bool)
| Null
| String(string)
| Number(float)
| Object(Core__Dict.t<t>)
| Array(array<t>)
/**
Returns the JSON type of any value.
## Examples
```rescript
JSON.Classify.classify("hello world")
// String("hello world")
JSON.Classify.classify(42)
// Number(42)
```
*/
let classify: 'a => t
}
module Encode: {
/**
Returns a boolean as a JSON object.
## Examples
```rescript
JSON.Encode.bool(true)
```
*/
external bool: bool => t = "%identity"
/**
Returns null as a JSON object.
## Examples
```rescript
JSON.Encode.null
```
*/
external null: t = "#null"
/**
Returns a string as a JSON object.
## Examples
```rescript
JSON.Encode.string("hello world")
```
*/
external string: string => t = "%identity"
/**
Returns an int as a JSON object.
## Examples
```rescript
JSON.Encode.int(42)
```
*/
external int: int => t = "%identity"
/**
Returns a float as a JSON object.
## Examples
```rescript
JSON.Encode.float(42.0)
```
*/
external float: float => t = "%identity"
/**
Returns a dict as a JSON object.
## Examples
```rescript
let dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
])
JSON.Encode.object(dict)
```
*/
external object: Core__Dict.t<t> => t = "%identity"
/**
Returns an array as a JSON object.
## Examples
```rescript
let array = [JSON.Encode.string("hello world"), JSON.Encode.int(42)]
JSON.Encode.array(array)
```
*/
external array: array<t> => t = "%identity"
}
module Decode: {
/**
Decodes a single JSON value. If the value is a bool, it will return `Some(bool)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`true`)->JSON.Decode.bool
// Some(true)
JSON.parseExn(`"hello world"`)->JSON.Decode.bool
// None
```
*/
let bool: t => option<bool>
/**
Decodes a single JSON value. If the value is null, it will return `Some(Null.t)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`null`)->JSON.Decode.null
// Some(null)
JSON.parseExn(`"hello world"`)->JSON.Decode.null
// None
```
*/
let null: t => option<Core__Null.t<'a>>
/**
Decodes a single JSON value. If the value is a string, it will return `Some(string)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`"hello world"`)->JSON.Decode.string
// Some("hello world")
JSON.parseExn(`42`)->JSON.Decode.string
// None
```
*/
let string: t => option<string>
/**
Decodes a single JSON value. If the value is a float, it will return `Some(float)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`42.0`)->JSON.Decode.float
// Some(42.0)
JSON.parseExn(`"hello world"`)->JSON.Decode.float
// None
```
*/
let float: t => option<float>
/**
Decodes a single JSON value. If the value is an object, it will return `Some(Dict.t)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`{"foo":"bar"}`)->JSON.Decode.object
// Some({ foo: 'bar' })
JSON.parseExn(`"hello world"`)->JSON.Decode.object
// None
```
*/
let object: t => option<Core__Dict.t<t>>
/**
Decodes a single JSON value. If the value is an array, it will return `Some(array)` - otherwise it will return `None`.
## Examples
```rescript
JSON.parseExn(`["foo", "bar"]`)->JSON.Decode.array
// Some([ 'foo', 'bar' ])
JSON.parseExn(`"hello world"`)->JSON.Decode.array
// None
```
*/
let array: t => option<array<t>>
}