UNPKG

1.24 kBPlain TextView Raw
1// deno-lint-ignore-file no-explicit-any ban-ts-comment
2import typeson from './typeson.js'
3// @ts-ignore
4import { structuredCloningThrowing } from 'typeson-registry';
5
6import blob from './blob.js'
7import file from './file.js'
8import filelist from './filelist.js'
9
10const { Typeson } = typeson;
11
12const structuredCloningThrowingPatched = (structuredCloningThrowing as any[])
13 .filter((x: any) => !x.file && !x.blob)
14 .concat([blob, file, filelist])
15
16const Structured = new Typeson().register([structuredCloningThrowingPatched]);
17
18export function parse(s: string) {
19 return Structured.revive(JSON.parse(s))
20}
21
22export function stringify(value: any): string {
23 return Structured.stringifySync(value);
24}
25
26export function stringifyAsync(value: any): string | Promise<string> {
27 return Structured.stringifyAsync(value);
28}
29
30export function toJSON(value: any): any {
31 return Structured.encapsulateSync(value)
32}
33
34export function toJSONAsync(value: any): any | Promise<any> {
35 return Structured.encapsulateAsync(value)
36}
37
38export function fromJSON(json: any) {
39 return Structured.revive(json)
40}
41
42export {
43 toJSON as toJSONValue,
44 fromJSON as fromJSONValue,
45 fromJSON as revive,
46 toJSON as encapsulate,
47 toJSONAsync as encapsulateAsync,
48}