-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.ts
1885 lines (1834 loc) · 51.8 KB
/
json.ts
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
* Utilities for handling Deno KV entries, keys, and values as structures
* which can be serialized and deserialized to JSON.
*
* This is useful when communicating entries and values outside of the runtime
* environment.
*
* @example Converting to a maybe entry to JSON
*
* ```ts
* import { entryMaybeToJSON } from "@deno/kv-utils/json";
*
* const db = await Deno.openKv();
* const entryMaybe = await db.get(["a"]);
*
* // `json` is now an object which can be safely converted to a JSON string
* const json = entryMaybeToJSON(entryMaybe);
* db.close();
* ```
*
* @example Converting a JSON value to a Deno KV value
*
* ```ts
* import { toValue } from "@deno/kv-utils/json";
*
* // `json` represents a `Uint8Array` with the bytes of [1, 2, 3]
* const json = { type: "Uint8Array", value: "AQID", byteLength: 3 } as const;
*
* const db = await Deno.openKv();
* await db.set(["a"], toValue(json));
* db.close();
* ```
*
* @module
*/
import { decodeBase64Url, encodeBase64Url } from "@std/encoding/base64url";
// Deno KV Key types
/**
* A JSON representation of a {@linkcode bigint} Deno KV key part. The value
* is a string representation of the integer, for example `100n` would be:
*
* ```json
* { "type": "bigint", "value": "100" }
* ```
*/
export interface KvBigIntJSON {
/**
* The type of the key part, which is always `"bigint"`.
*/
type: "bigint";
/**
* The string representation of the bigint value.
*/
value: string;
}
/**
* A JSON representation of a {@linkcode boolean} Deno KV key part. The value
* is the boolean value, for example `true` would be:
*
* ```json
* { "type": "boolean", "value": true }
* ```
*/
export interface KvBooleanJSON {
/**
* The type of the key part, which is always `"boolean"`.
*/
type: "boolean";
/**
* The boolean value.
*/
value: boolean;
}
/**
* A JSON representation of a {@linkcode number} Deno KV key part. The value
* is the number value, for example `100` would be:
*
* ```json
* { "type": "number", "value": 100 }
* ```
*
* For special numbers, the value is a string representation of the number, for
* example `Infinity` would be:
*
* ```json
* { "type": "number", "value": "Infinity" }
* ```
*/
export interface KvNumberJSON {
/**
* The type of the key part, which is always `"number"`.
*/
type: "number";
/**
* The number value.
*/
value: number | "Infinity" | "-Infinity" | "NaN";
}
/**
* A JSON representation of a {@linkcode string} Deno KV key part. The value is
* the string value, for example `"value"` would be:
*
* ```json
* { "type": "string", "value": "value" }
* ```
*/
export interface KvStringJSON {
/**
* The type of the key part, which is always `"string"`.
*/
type: "string";
/**
* The string value.
*/
value: string;
}
/**
* A JSON representation of a {@linkcode Uint8Array} Deno KV key part. The value
* is a URL safe base64 encoded value, for example an array with the values of
* `[ 1, 2, 3 ]` would be:
*
* ```json
* { "type": "Uint8Array", "value": "AQID" }
* ```
*
* While Deno KV accepts anything that is array view like as a key part, when
* the value is read as part of an entry, it is always represented as a
* `Uint8Array`.
*/
export interface KvUint8ArrayJSON {
/**
* The type of the key part, which is always `"Uint8Array"`.
*/
type: "Uint8Array";
/**
* The URL safe base64 encoded value of the array.
*/
value: string;
/**
* The byte length of the byte array.
*/
byteLength: number;
}
/**
* JSON representations of {@linkcode Deno.KvKeyPart}. This represents each key
* part type that is supported by Deno KV.
*/
export type KvKeyPartJSON =
| KvBigIntJSON
| KvBooleanJSON
| KvNumberJSON
| KvStringJSON
| KvUint8ArrayJSON;
/**
* A JSON representation of a {@linkcode Deno.KvKey}, which is an array of
* {@linkcode KvKeyPartJSON} items.
*/
export type KvKeyJSON = readonly KvKeyPartJSON[];
// Deno KV Value types
/**
* A representation of an {@linkcode ArrayBuffer} Deno KV value. The value is
* the bytes of the array buffer encoded as a URL safe base64 string, for
* example an array buffer with the byte values of `[ 1, 2, 3 ]` would be:
*
* ```json
* { "type": "ArrayBuffer", "value": "AQID" }
* ```
*/
export interface KvArrayBufferJSON {
/**
* The type of the value, which is always `"ArrayBuffer"`.
*/
type: "ArrayBuffer";
/**
* The URL safe base64 encoded value of the array buffer.
*/
value: string;
/**
* The byte length of the array buffer.
*/
byteLength: number;
}
/**
* A representation of an {@linkcode Array} Deno KV value. The value is the
* JSON serialized version of the elements of the array.
*/
export interface KvArrayJSON {
/**
* The type of the value, which is always `"Array"`.
*/
type: "Array";
/**
* The JSON serialized version of the array.
*/
value: readonly KvValueJSON[];
}
/**
* A representation of an {@linkcode DataView} Deno KV value. The value is
* the bytes of the buffer encoded as a URL safe base64 string, for example a
* data view with the byte values of `[ 1, 2, 3 ]` would be:
*
* ```json
* { "type": "DataView", "value": "AQID" }
* ```
*/
export interface KvDataViewJSON {
/**
* The type of the value, which is always `"DataView"`.
*/
type: "DataView";
/**
* The URL safe base64 encoded value of the data view.
*/
value: string;
/**
* The byte length of the data view.
*/
byteLength: number;
}
/**
* A representation of a {@linkcode Date} Deno KV value. The value is the
* [ISO string representation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)
* of the date.
*/
export interface KvDateJSON {
/**
* The type of the value, which is always `"Date"`.
*/
type: "Date";
/**
* The ISO string representation of the date.
*/
value: string;
}
/**
* Error instances which are
* [cloneable](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#error_types)
* and therefore can be stored in a Deno KV store.
*
* This type is used to allow type inference when deserializing from JSON.
*/
export interface CloneableErrors {
/** {@linkcode Error} which is cloneable. */
Error: Error;
/** {@linkcode EvalError} which is cloneable. */
EvalError: EvalError;
/** {@linkcode RangeError} which is cloneable. */
RangeError: RangeError;
/** {@linkcode ReferenceError} which is cloneable. */
ReferenceError: ReferenceError;
/** {@linkcode SyntaxError} which is cloneable. */
SyntaxError: SyntaxError;
/** {@linkcode TypeError} which is cloneable. */
TypeError: TypeError;
/** {@linkcode URIError} which is cloneable. */
URIError: URIError;
}
/**
* The keys of {@linkcode CloneableErrors} which is used for type inference
* when deserializing from JSON.
*/
export type CloneableErrorTypes = keyof CloneableErrors;
/**
* A representation of {@linkcode Error}s that can be stored as Deno KV values.
* The value is set to a serialized version of the value. Instances that are
* not one of the specified types, but inherit from `Error` will be serialized
* as `Error`.
*/
export interface KvErrorJSON<
ErrorType extends CloneableErrorTypes = CloneableErrorTypes,
> {
/**
* The type of the error, which is one of the cloneable error types.
*/
type: ErrorType;
/**
* The value of the error, which is a JSON serialized version of the error.
*/
value: {
message: string;
cause?: KvValueJSON | undefined;
stack?: string | undefined;
};
}
/**
* A representation of a {@linkcode Deno.KvU64} value. The value is the string
* representation of the unsigned integer.
*/
export interface KvKvU64JSON {
/**
* The type of the value, which is always `"KvU64"`.
*/
type: "KvU64";
/**
* The string representation of the unsigned integer value.
*/
value: string;
}
/**
* A representation of a {@linkcode Map} Deno KV value. The value is an array
* of map entries where is map entry is a tuple of a JSON serialized key and
* value.
*/
export interface KvMapJSON {
/**
* The type of the value, which is always `"Map"`.
*/
type: "Map";
/**
* The JSON serialized version of the map entries.
*/
value: readonly [key: KvValueJSON, value: KvValueJSON][];
}
/**
* A representation of a {@linkcode null} Deno KV value. The value is `null`.
*/
export interface KvNullJSON {
/**
* The type of the value, which is always `"null"`.
*/
type: "null";
/**
* The value of the value, which is always `null`.
*/
value: null;
}
/**
* A representation of a {@linkcode object} Deno KV value. The value is a JSON
* serialized version of the value.
*/
export interface KvObjectJSON {
/**
* The type of the value, which is always `"object"`.
*/
type: "object";
/**
* The JSON serialized version of the object.
*/
value: { [key: string]: KvValueJSON };
}
/**
* A representation of a {@linkcode RegExp} Deno KV value. The value is a string
* representation of the regular expression value.
*/
export interface KvRegExpJSON {
/**
* The type of the value, which is always `"RegExp"`.
*/
type: "RegExp";
/**
* The string representation of the regular expression value.
*/
value: string;
}
/**
* A representation of a {@linkcode Set} Deno KV value. The value is an array
* of the JSON serialized values of the set.
*/
export interface KvSetJSON {
/**
* The type of the value, which is always `"Set"`.
*/
type: "Set";
/**
* The JSON serialized version of the set values.
*/
value: readonly KvValueJSON[];
}
/** Used internally to identify a typed array. */
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
/**
* Used internally to be able to map the name of the typed array to its instance
* type.
*/
interface TypedArrayMap {
/** {@linkcode Int8Array} which is a typed array. */
Int8Array: Int8Array;
/** {@linkcode Uint8Array} which is a typed array. */
Uint8Array: Uint8Array;
/** {@linkcode Uint8ClampedArray} which is a typed array. */
Uint8ClampedArray: Uint8ClampedArray;
/** {@linkcode Int16Array} which is a typed array. */
Int16Array: Int16Array;
/** {@linkcode Uint16Array} which is a typed array. */
Uint16Array: Uint16Array;
/** {@linkcode Int32Array} which is a typed array. */
Int32Array: Int32Array;
/** {@linkcode Uint32Array} which is a typed array. */
Uint32Array: Uint32Array;
/** {@linkcode Float32Array} which is a typed array. */
Float32Array: Float32Array;
/** {@linkcode Float64Array} which is a typed array. */
Float64Array: Float64Array;
/** {@linkcode BigInt64Array} which is a typed array. */
BigInt64Array: BigInt64Array;
/** {@linkcode BigUint64Array} which is a typed array. */
BigUint64Array: BigUint64Array;
}
/** Used internally. The string literal types of the names of the type. */
type TypedArrayTypes = keyof TypedArrayMap;
/**
* A representation of a typed array Deno KV value. The value is a URL safe
* base64 encoded string which represents the individual bytes of the array.
*/
export interface KvTypedArrayJSON<
ArrayType extends TypedArrayTypes = TypedArrayTypes,
> {
/**
* The type of the value.
*/
type: ArrayType;
/**
* The URL safe base64 encoded value of the typed array.
*/
value: string;
/**
* The byte length of the typed array.
*/
byteLength: number;
}
/**
* A representation of a {@linkcode undefined} Deno KV value. The value is
* undefined, and therefore elided when serialized. Therefore there is only one
* form of this entity:
*
* ```json
* { "type": "undefined" }
* ```
*/
export interface KvUndefinedJSON {
/**
* The type of the value, which is always `"undefined"`.
*/
type: "undefined";
}
/**
* JSON representations of {@linkcode Deno.Kv} values, where the value types are
* exhaustive of what Deno KV supports and are allowed via
* [structured cloning](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
*/
export type KvValueJSON =
| KvArrayBufferJSON
| KvArrayJSON
| KvBigIntJSON
| KvBooleanJSON
| KvDataViewJSON
| KvDateJSON
| KvErrorJSON
| KvKvU64JSON
| KvMapJSON
| KvNullJSON
| KvNumberJSON
| KvObjectJSON
| KvRegExpJSON
| KvSetJSON
| KvStringJSON
| KvTypedArrayJSON
| KvUndefinedJSON;
// Deno KV Entry types
/**
* A representation of a {@linkcode Deno.KvEntry} where the key and value are
* encoded in a JSON serializable format.
*/
export interface KvEntryJSON {
/**
* The key of the entry.
*/
key: KvKeyJSON;
/**
* The value of the entry.
*/
value: KvValueJSON;
/**
* The versionstamp of the entry.
*/
versionstamp: string;
}
/**
* A representation of a {@linkcode Deno.KvEntryMaybe} where the key and value
* are encoded in a JSON serializable format.
*/
export type KvEntryMaybeJSON = KvEntryJSON | {
/**
* The key of the entry.
*/
key: KvKeyJSON;
/**
* The value of the entry.
*/
value: null;
/**
* The versionstamp of the entry.
*/
versionstamp: null;
};
// Serializing to JSON
/**
* Internal function to serialize various classes of errors to JSON.
*
* @param error The error to serialize.
* @returns The JSON representation of the error.
*
* @private
*/
function errorToJSON(error: Error): KvErrorJSON {
const { message, stack, cause } = error;
const value: KvErrorJSON["value"] = { message };
if (cause) {
value.cause = valueToJSON(cause);
}
if (stack) {
value.stack = stack;
}
if (error instanceof EvalError) {
return { type: "EvalError", value };
}
if (error instanceof RangeError) {
return { type: "RangeError", value };
}
if (error instanceof ReferenceError) {
return { type: "ReferenceError", value };
}
if (error instanceof SyntaxError) {
return { type: "SyntaxError", value };
}
if (error instanceof TypeError) {
return { type: "TypeError", value };
}
if (error instanceof URIError) {
return { type: "URIError", value };
}
return { type: "Error", value };
}
/**
* Internal function to serialize various typed arrays to JSON.
*
* @param typedArray The typed array to serialize.
* @returns The JSON representation of the typed array.
*
* @private
*/
function typedArrayToJSON(typedArray: ArrayBufferView): KvTypedArrayJSON {
const value = encodeBase64Url(typedArray.buffer);
const byteLength = typedArray.byteLength;
if (typedArray instanceof Int8Array) {
return { type: "Int8Array", value, byteLength };
}
if (typedArray instanceof Uint8Array) {
return { type: "Uint8Array", value, byteLength };
}
if (typedArray instanceof Uint8ClampedArray) {
return { type: "Uint8ClampedArray", value, byteLength };
}
if (typedArray instanceof Int16Array) {
return { type: "Int16Array", value, byteLength };
}
if (typedArray instanceof Uint16Array) {
return { type: "Uint16Array", value, byteLength };
}
if (typedArray instanceof Int32Array) {
return { type: "Int32Array", value, byteLength };
}
if (typedArray instanceof Uint32Array) {
return { type: "Uint32Array", value, byteLength };
}
if (typedArray instanceof Float32Array) {
return { type: "Float32Array", value, byteLength };
}
if (typedArray instanceof Float64Array) {
return { type: "Float64Array", value, byteLength };
}
if (typedArray instanceof BigInt64Array) {
return { type: "BigInt64Array", value, byteLength };
}
if (typedArray instanceof BigUint64Array) {
return { type: "BigUint64Array", value, byteLength };
}
throw TypeError("Unexpected typed array type, could not serialize.");
}
/**
* Internal function to encode an object.
*
* @param object The object to encode.
* @returns The encoded object.
*
* @private
*/
function encodeObject(object: object): { [key: string]: KvValueJSON } {
const result: { [key: string]: KvValueJSON } = {};
for (const [key, value] of Object.entries(object)) {
result[key] = valueToJSON(value);
}
return result;
}
/**
* Internal function to decode an object.
*
* @param json The JSON object to decode.
* @returns The decoded object.
*
* @private
*/
function decodeObject(json: { [key: string]: KvValueJSON }): object {
const result: { [key: string]: unknown } = {};
for (const [key, value] of Object.entries(json)) {
result[key] = toValue(value);
}
return result;
}
/**
* Serialize a {@linkcode Deno.KvKeyPart} to JSON.
*
* @param value The key part to serialize.
* @returns The JSON representation of the key part.
* @example Serialize a key part to JSON
*
* ```ts
* import { keyPartToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const keyPart = 100n;
* const json = keyPartToJSON(keyPart);
* assertEquals(json, { type: "bigint", value: "100" });
* ```
*/
export function keyPartToJSON(value: Deno.KvKeyPart): KvKeyPartJSON {
switch (typeof value) {
case "bigint":
return { type: "bigint", value: String(value) };
case "boolean":
return { type: "boolean", value };
case "number":
if (Number.isNaN(value)) {
return { type: "number", value: "NaN" };
} else if (value === Infinity) {
return { type: "number", value: "Infinity" };
} else if (value === -Infinity) {
return { type: "number", value: "-Infinity" };
}
return { type: "number", value };
case "object":
if (value instanceof Uint8Array) {
return {
type: "Uint8Array",
value: encodeBase64Url(value),
byteLength: value.byteLength,
};
}
break;
case "string":
return { type: "string", value };
}
throw new TypeError("Unable to serialize value.");
}
/**
* Serialize a {@linkcode Deno.KvKey} to JSON.
*
* @param value The key to serialize.
* @returns The JSON representation of the key.
* @example Serialize a key to JSON
*
* ```ts
* import { keyToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const key = ["a", 100n];
* const json = keyToJSON(key);
* assertEquals(json, [
* { type: "string", value: "a" },
* { type: "bigint", value: "100" },
* ]);
* ```
*/
export function keyToJSON(value: Deno.KvKey): KvKeyJSON {
return value.map(keyPartToJSON);
}
/**
* Serialize an array that can be stored in Deno KV to JSON.
*
* @param value The array value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const json = valueToJSON([["a", 1], ["b", 2]]);
* assertEquals(json, { type: "Array", value: [
* { type: "Array", value: [{ type: "string", value: "a" }, { type: "number", value: 1 }] },
* { type: "Array", value: [{ type: "string", value: "b" }, { type: "number", value: 2 }] },
* ] });
* ```
*/
export function valueToJSON(value: unknown[]): KvArrayJSON;
/**
* Serialize a bigint that can be stored in Deno KV to JSON.
*
* @param value The bigint value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = 100n;
* const json = valueToJSON(value);
* assertEquals(json, { type: "bigint", value: "100" });
* ```
*/
export function valueToJSON(value: bigint): KvBigIntJSON;
/**
* Serialize a boolean that can be stored in Deno KV to JSON.
*
* @param value The boolean value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = true;
* const json = valueToJSON(value);
* assertEquals(json, { type: "boolean", value: true });
* ```
*/
export function valueToJSON(value: boolean): KvBooleanJSON;
/**
* Serialize a {@linkcode Date} that can be stored in Deno KV to JSON.
*
* @param value The date value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Date();
* const json = valueToJSON(value);
* assertEquals(json.type, "Date");
* ```
*/
export function valueToJSON(value: Date): KvDateJSON;
/**
* Serialize an error that can be stored in Deno KV to JSON.
*
* @typeParam ErrorType The type of error that can be serialized
* @param value The error value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new TypeError("That is the wrong type!");
* const json = valueToJSON(value);
* assertEquals(json, {
* type: "TypeError",
* value: { message: "That is the wrong type!", stack: value.stack },
* });
* ```
*/
export function valueToJSON<ErrorType extends CloneableErrorTypes>(
value: Error,
): KvErrorJSON<ErrorType>;
/**
* Serialize a {@linkcode Deno.KvU64} that can be stored in Deno KV to JSON.
*
* @param value The value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Deno.KvU64(100n);
* const json = valueToJSON(value);
* assertEquals(json, { type: "KvU64", value: "100" });
* ```
*/
export function valueToJSON(value: Deno.KvU64): KvKvU64JSON;
/**
* Serialize a {@linkcode Map} that can be stored in Deno KV to JSON.
*
* @param value The map value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Map([["a", 1], ["b", 2]]);
* const json = valueToJSON(value);
* assertEquals(json, { type: "Map", value: [
* [{ type: "string", value: "a" }, { type: "number", value: 1 }],
* [{ type: "string", value: "b" }, { type: "number", value: 2 }],
* ] });
* ```
*/
export function valueToJSON(value: Map<unknown, unknown>): KvMapJSON;
/**
* Serialize a `null` that can be stored in Deno KV to JSON.
*
* @param value The value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = null;
* const json = valueToJSON(value);
* assertEquals(json, { type: "null", value: null });
* ```
*/
export function valueToJSON(value: null): KvNullJSON;
/**
* Serialize a number that can be stored in Deno KV to JSON.
*
* This includes special numbers like `Infinity`, `-Infinity`, and `NaN`.
*
* @param value The number value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = 100;
* const json = valueToJSON(value);
* assertEquals(json, { type: "number", value: 100 });
* ```
*/
export function valueToJSON(value: number): KvNumberJSON;
/**
* Serialize a {@linkcode RegExp} that can be stored in Deno KV to JSON.
*
* @param value The regex value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = /1234/i;
* const json = valueToJSON(value);
* assertEquals(json, { type: "RegExp", value: "/1234/i" });
* ```
*/
export function valueToJSON(value: RegExp): KvRegExpJSON;
/**
* Serialize a {@linkcode Set} that can be stored in Deno KV to JSON.
*
* @param value The set value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Set([1, 2, 3]);
* const json = valueToJSON(value);
* assertEquals(json, { type: "Set", value: [
* { type: "number", value: 1 },
* { type: "number", value: 2 },
* { type: "number", value: 3 },
* ] });
* ```
*/
export function valueToJSON(value: Set<unknown>): KvSetJSON;
/**
* Serialize a string that can be stored in Deno KV to JSON.
*
* @param value The string value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = "hello, world!";
* const json = valueToJSON(value);
* assertEquals(json, { type: "string", value: "hello, world!" });
* ```
*/
export function valueToJSON(value: string): KvStringJSON;
/**
* Serialize a typed array that can be stored in Deno KV to JSON.
*
* @typeParam TA The type of the typed array, which is inferred from the value
* @param value The typed array value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Uint8Array([1, 2, 3]);
* const json = valueToJSON(value);
* assertEquals(json, { type: "Uint8Array", value: "AQID", byteLength: 3 });
* ```
*/
export function valueToJSON<TA extends TypedArray>(
value: TA,
): KvTypedArrayJSON<TA[typeof Symbol.toStringTag]>;
/**
* Serialize an {@linkcode ArrayBuffer} that can be stored in Deno KV to JSON.
*
* @param value The array buffer value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new Uint8Array([1, 2, 3]).buffer;
* const json = valueToJSON(value);
* assertEquals(json, { type: "ArrayBuffer", value: "AQID", byteLength: 3 });
* ```
*/
export function valueToJSON(value: ArrayBufferLike): KvArrayBufferJSON;
/**
* Serialize a {@linkcode DataView} that can be stored in Deno KV to JSON.
*
* @param value The data view value to serialize
* @returns The JSON representation of the value
* @example Serialize a value to JSON
*
* ```ts
* import { valueToJSON } from "@deno/kv-utils/json";
* import { assertEquals } from "@std/assert";
*
* const value = new DataView(new Uint8Array([1, 2, 3]).buffer);
* const json = valueToJSON(value);