-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
168 lines (168 loc) · 7.52 KB
/
index.d.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
/**
* TSONSerializer interface
*/
export interface TSONSerializer<T = any, D = any> {
/**A function to deserialize the data.
* @param data A value to be deserialized.
* @returns Deserialized data for `data`
*/
load(data: D): T;
/**A function to serialize the value.
* @param value A value to be serialized.
* @returns Serialized data for `value`
*/
dump(value: T): D;
/**A function to check the value matches this serializer.
* @param value A value to be checked.
* @returns Match if `true`, otherwise pass.
*/
match(value: any): boolean;
/**The name of the serializer*/
readonly type: string;
/**Whether to process `load()` input and `dump()` output recursively. */
readonly recursive: boolean;
}
/**
* TSONData interface
*/
export interface TSONData<T = any, D = any> {
/**The name of the serializer that handles this data.*/
type: T;
/**Serialized data from serializer.*/
data: D;
}
/**ArrayBuffer serialized data interface */
export interface ArrayBfferData {
buffer: ArrayBuffer;
offset: number;
length: number;
}
/**
TSON - A Type-safe Serializer like JSON
*/
export declare class TSON {
private readonly types;
private readonly map;
/** @hidden */
private readonly refname;
constructor();
/** @hidden */
private referenceable;
/** @hidden */
private reference;
/** @hidden */
private dereference;
/**
* Transform the array of values to an array of TSONData objects.
* @param value Array of values.
* @returns The array of `TSONData` objects of the `value`.
*/
forward(value: any[]): TSONData[];
/**
* Transform the value to a TSONData object.
* @param value A value whose type can be object,number,boolean,or other type.
* @returns The `TSONData` object of the `value`.
*/
forward(value: any): TSONData;
/**
* Transform the `TSONData` back to the original object.
* @param value A TSONData object or array of TSONData objects to be transformed.
* @returns The original object of the `TSONData` object.
*/
backward<T = any>(value: TSONData | TSONData[]): T;
/**
* Wrapper for `JSON.stringify(TSON.forward(value),replacer,space)`\
* Converts a JavaScript value to a TSON string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer?: ((this: any, key: string, value: any) => any) | null, space?: number): string;
/**
* Wrapper for `TSON.backward(JSON.parse(text, reviver))`\
* Converts a TSON string into an object.
* @param text A valid TSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse<T = any>(text: string, reviver?: (this: any, key: string, value: any) => any): T;
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param serializer A TSONSerializer Object.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(serializer: TSONSerializer<T, D>): boolean;
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param name The name of the serializer.
* @param load A function to deserialize the data.
* @param dump A function to serialize the value.
* @param match A function that checks the value matches this serializer.
* @param recursive Whether to process `load()` input and `dump()` output recursively.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(name: string, load: (data: D) => T, dump: (value: T) => D, match: (value: any) => boolean, recursive?: boolean): boolean;
/**
* Register a TSON Serializer for Type.
* @template T The original type.
* @template D The serialized type.
* @param constructor Type constructor.
* @param load A function to deserialize the data.
* @param dump A function to serialize the value.
* @param match A function that checks the value matches this serializer. If null, `value instanceof constructor` is used by default.
* @param recursive Whether to process `load()` input and `dump()` output recursively.
* @returns `true` if register was successful, `false` otherwise.
*/
register<T, D>(constructor: {
new (...data: any): T;
}, load: (data: D) => T, dump: (value: T) => D, match?: ((value: any) => boolean) | null, recursive?: boolean): boolean;
/**
* Deregister a TSON Serializer.
* @param name The name of the serializer.
* @returns `true` if deregister was successful, `false` otherwise.
*/
deregister(name: string): boolean;
static readonly instance: TSON;
/**Link to {@link TSON.forward}. */
static readonly forward: {
(value: any[]): TSONData[];
(value: any): TSONData;
};
/**Link to {@link TSON.backward}. */
static readonly backward: <T = any>(value: TSONData | TSONData[]) => T;
/**Link to {@link TSON.stringify}. */
static readonly stringify: (value: any, replacer?: ((this: any, key: string, value: any) => any) | null | undefined, space?: number | undefined) => string;
/**Link to {@link TSON.parse}. */
static readonly parse: <T = any>(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => T;
/**Link to {@link TSON.register}. */
static readonly register: {
<T, D>(serializer: TSONSerializer<T, D>): boolean;
<T_1, D_1>(name: string, load: (data: D_1) => T_1, dump: (value: T_1) => D_1, match: (value: any) => boolean, recursive?: boolean | undefined): boolean;
<T_2, D_2>(constructor: new (...data: any) => T_2, load: (data: D_2) => T_2, dump: (value: T_2) => D_2, match?: ((value: any) => boolean) | null | undefined, recursive?: boolean | undefined): boolean;
};
/**Link to {@link TSON.deregister}. */
static readonly deregister: (name: string) => boolean;
}
/**Link to {@link TSON.forward}. */
export declare const forward: {
(value: any[]): TSONData[];
(value: any): TSONData;
};
/**Link to {@link TSON.backward}. */
export declare const backward: <T = any>(value: TSONData | TSONData[]) => T;
/**Link to {@link TSON.stringify}. */
export declare const stringify: (value: any, replacer?: ((this: any, key: string, value: any) => any) | null | undefined, space?: number | undefined) => string;
/**Link to {@link TSON.parse}. */
export declare const parse: <T = any>(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => T;
/**Link to {@link TSON.register}. */
export declare const register: {
<T, D>(serializer: TSONSerializer<T, D>): boolean;
<T_1, D_1>(name: string, load: (data: D_1) => T_1, dump: (value: T_1) => D_1, match: (value: any) => boolean, recursive?: boolean | undefined): boolean;
<T_2, D_2>(constructor: new (...data: any) => T_2, load: (data: D_2) => T_2, dump: (value: T_2) => D_2, match?: ((value: any) => boolean) | null | undefined, recursive?: boolean | undefined): boolean;
};
/**Link to {@link TSON.deregister}. */
export declare const deregister: (name: string) => boolean;
export default TSON;