json-as 0.5.2 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/assembly/json.ts CHANGED
@@ -1,22 +1,17 @@
1
1
  import { StringSink } from "as-string-sink/assembly";
2
2
  import { isSpace } from "util/string";
3
- import { Type } from "./type";
4
3
  import {
5
- backSlashCode,
6
- colonCode,
7
- commaCode,
8
- eCode,
9
- fCode,
10
- forwardSlashCode,
11
- lCode,
12
- leftBraceCode,
13
- leftBracketCode,
14
- nCode,
15
- quoteCode,
16
- rightBraceCode,
17
- rightBracketCode,
18
- tCode,
19
- uCode,
4
+ backSlashCode,
5
+ commaCode,
6
+ eCode,
7
+ fCode,
8
+ leftBraceCode,
9
+ leftBracketCode,
10
+ nCode,
11
+ quoteCode,
12
+ rightBraceCode,
13
+ rightBracketCode,
14
+ tCode,
20
15
  } from "./chars";
21
16
  import { unsafeCharCodeAt } from "./util";
22
17
 
@@ -24,339 +19,332 @@ import { unsafeCharCodeAt } from "./util";
24
19
  * JSON Encoder/Decoder for AssemblyScript
25
20
  */
26
21
  export class JSON {
27
- /**
28
- * Stringifies valid JSON data.
29
- * ```js
30
- * JSON.stringify<T>(data)
31
- * ```
32
- * @param data T
33
- * @returns string
34
- */
35
- static stringify<T>(data: T): string {
36
- // String
37
- if (isString<T>()) {
38
- return '"' + (<string>data).replaceAll('"', '\\"') + '"';
39
- }
40
- // Boolean
41
- else if (isBoolean<T>()) {
42
- return data ? "true" : "false";
43
- }
44
- // Nullable
45
- else if (isNullable<T>() && data == null) {
46
- return "null";
47
- }
48
- // Integers/Floats
49
- // @ts-ignore
50
- else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
51
- // @ts-ignore
52
- return data.toString();
53
- }
54
- // Class-Based serialization
22
+ /**
23
+ * Stringifies valid JSON data.
24
+ * ```js
25
+ * JSON.stringify<T>(data)
26
+ * ```
27
+ * @param data T
28
+ * @returns string
29
+ */
30
+ static stringify<T>(data: T): string {
31
+ // String
32
+ if (isString<T>()) {
33
+ return '"' + (<string>data).replaceAll('"', '\\"') + '"';
34
+ }
35
+ // Boolean
36
+ else if (isBoolean<T>()) {
37
+ return data ? "true" : "false";
38
+ }
39
+ // Nullable
40
+ else if (isNullable<T>() && data == null) {
41
+ return "null";
42
+ }
43
+ // Integers/Floats
44
+ // @ts-ignore
45
+ else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
46
+ // @ts-ignore
47
+ return data.toString();
48
+ }
49
+ // Class-Based serialization
50
+ // @ts-ignore
51
+ else if (isDefined(data.__JSON_Serialize)) {
52
+ // @ts-ignore
53
+ //if (isNullable<T>()) return "null";
54
+ // @ts-ignore
55
+ return data.__JSON_Serialize();
56
+ }
57
+ // ArrayLike
58
+ else if (isArrayLike<T>()) {
59
+ let result = new StringSink("[");
60
+ // @ts-ignore
61
+ if (data.length == 0) return "[]";
62
+ // @ts-ignore
63
+ for (let i = 0; i < data.length - 1; i++) {
55
64
  // @ts-ignore
56
- else if (isDefined(data.__JSON_Serialize)) {
57
- // @ts-ignore
58
- //if (isNullable<T>()) return "null";
59
- // @ts-ignore
60
- return data.__JSON_Serialize();
61
- }
62
- // ArrayLike
63
- else if (isArrayLike<T>()) {
64
- let result = new StringSink("[");
65
- // @ts-ignore
66
- if (data.length == 0) return "[]";
67
- // @ts-ignore
68
- for (let i = 0; i < data.length - 1; i++) {
69
- // @ts-ignore
70
- result.write(JSON.stringify(unchecked(data[i])) + ",");
71
- }
72
- // @ts-ignore
73
- result.write(JSON.stringify(unchecked(data[data.length - 1])));
74
- result.write("]");
75
- return result.toString();
76
- } else {
77
- return "null";
78
- }
65
+ result.write(JSON.stringify(unchecked(data[i])) + ",");
66
+ }
67
+ // @ts-ignore
68
+ result.write(JSON.stringify(unchecked(data[data.length - 1])));
69
+ result.write("]");
70
+ return result.toString();
71
+ } else {
72
+ return "null";
79
73
  }
80
- /**
81
- * Parses valid JSON strings into their original format.
82
- * ```js
83
- * JSON.parse<T>(data)
84
- * ```
85
- * @param data string
86
- * @returns T
87
- */
88
- static parse<T>(data: string): T {
89
- let type!: T;
90
- if (isString<T>()) {
91
- // @ts-ignore
92
- return parseString(data);
93
- } else if (isBoolean<T>()) {
94
- // @ts-ignore
95
- return parseBoolean<T>(data);
96
- } else if (isFloat<T>() || isInteger<T>()) {
97
- return parseNumber<T>(data);
98
- } else if (isArrayLike<T>()) {
99
- // @ts-ignore
100
- return parseArray<T>(data.trimStart());
101
- // @ts-ignore
102
- } else if (isNullable<T>() && data == "null") {
103
- // @ts-ignore
104
- return null
105
- // @ts-ignore
106
- } else if (isDefined(type.__JSON_Deserialize)) {
107
- return parseObject<T>(data.trimStart());
108
- } else {
109
- // @ts-ignore
110
- return null;
111
- }
74
+ }
75
+ /**
76
+ * Parses valid JSON strings into their original format.
77
+ * ```js
78
+ * JSON.parse<T>(data)
79
+ * ```
80
+ * @param data string
81
+ * @returns T
82
+ */
83
+ static parse<T>(data: string): T {
84
+ let type!: T;
85
+ if (isString<T>()) {
86
+ // @ts-ignore
87
+ return parseString(data);
88
+ } else if (isBoolean<T>()) {
89
+ // @ts-ignore
90
+ return parseBoolean<T>(data);
91
+ } else if (isFloat<T>() || isInteger<T>()) {
92
+ return parseNumber<T>(data);
93
+ } else if (isArrayLike<T>()) {
94
+ // @ts-ignore
95
+ return parseArray<T>(data.trimStart());
96
+ // @ts-ignore
97
+ } else if (isNullable<T>() && data == "null") {
98
+ // @ts-ignore
99
+ return null;
100
+ // @ts-ignore
101
+ } else if (isDefined(type.__JSON_Deserialize)) {
102
+ return parseObject<T>(data.trimStart());
103
+ } else {
104
+ // @ts-ignore
105
+ return null;
112
106
  }
113
- private static parseObjectValue<T>(data: string): T {
114
- let type!: T;
115
- if (isString<T>()) {
116
- // @ts-ignore
117
- return data.replaceAll('\\"', '"');
118
- } else if (isBoolean<T>()) {
119
- // @ts-ignore
120
- return parseBoolean<T>(data);
121
- } else if (isFloat<T>() || isInteger<T>()) {
122
- return parseNumber<T>(data);
123
- } else if (isArrayLike<T>()) {
124
- // @ts-ignore
125
- return parseArray<T>(data);
126
- // @ts-ignore
127
- } else if (isNullable<T>() && data == "null") {
128
- // @ts-ignore
129
- return null
130
- // @ts-ignore
131
- } else if (isDefined(type.__JSON_Deserialize)) {
132
- // @ts-ignore
133
- //if (isNullable<T>()) return null;
134
- return parseObject<T>(data);
135
- } else {
136
- // @ts-ignore
137
- //return null;
138
- throw new Error(`Could not parse value: ${data}`)
139
- }
107
+ }
108
+ private static parseObjectValue<T>(data: string): T {
109
+ let type!: T;
110
+ if (isString<T>()) {
111
+ // @ts-ignore
112
+ return data.replaceAll('\\"', '"');
113
+ } else if (isBoolean<T>()) {
114
+ // @ts-ignore
115
+ return parseBoolean<T>(data);
116
+ } else if (isFloat<T>() || isInteger<T>()) {
117
+ return parseNumber<T>(data);
118
+ } else if (isArrayLike<T>()) {
119
+ // @ts-ignore
120
+ return parseArray<T>(data);
121
+ // @ts-ignore
122
+ } else if (isNullable<T>() && data == "null") {
123
+ // @ts-ignore
124
+ return null;
125
+ // @ts-ignore
126
+ } else if (isDefined(type.__JSON_Deserialize)) {
127
+ // @ts-ignore
128
+ //if (isNullable<T>()) return null;
129
+ return parseObject<T>(data);
130
+ } else {
131
+ // @ts-ignore
132
+ //return null;
133
+ throw new Error(`Could not parse value: ${data}`);
140
134
  }
135
+ }
141
136
  }
142
137
 
143
138
  // @ts-ignore
144
139
  @inline
145
- function parseString(data: string): string {
146
- return data.slice(1, data.length - 1).replaceAll('\\"', '"');
140
+ function parseString(data: string): string {
141
+ return data.slice(1, data.length - 1).replaceAll('\\"', '"');
147
142
  }
148
143
 
149
144
  // @ts-ignore
150
145
  @inline
151
- function parseBoolean<T extends boolean>(data: string): T {
152
- if (data.length > 3 && data.startsWith("true")) return <T>true;
153
- else if (data.length > 4 && data.startsWith("false")) return <T>false;
154
- else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
146
+ function parseBoolean<T extends boolean>(data: string): T {
147
+ if (data.length > 3 && data.startsWith("true")) return <T>true;
148
+ else if (data.length > 4 && data.startsWith("false")) return <T>false;
149
+ else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
155
150
  }
156
151
 
157
152
  // @ts-ignore
158
153
  @inline
159
- function parseNumber<T>(data: string): T {
160
- let type: T;
161
- // @ts-ignore
162
- if (type instanceof f64) return F64.parseFloat(data);
163
- // @ts-ignore
164
- else if (type instanceof f32) return F32.parseFloat(data);
165
- // @ts-ignore
166
- else if (type instanceof u64) return U64.parseInt(data);
167
- // @ts-ignore
168
- else if (type instanceof u32) return U32.parseInt(data);
169
- // @ts-ignore
170
- else if (type instanceof u8) return U8.parseInt(data);
171
- // @ts-ignore
172
- else if (type instanceof u16) return U16.parseInt(data);
173
- // @ts-ignore
174
- else if (type instanceof i64) return I64.parseInt(data);
175
- // @ts-ignore
176
- else if (type instanceof i32) return I32.parseInt(data);
177
- // @ts-ignore
178
- else if (type instanceof i16) return I16.parseInt(data);
179
- // @ts-ignore
180
- else if (type instanceof i8) return I8.parseInt(data);
181
- else
182
- throw new Error(
183
- `JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
184
- );
154
+ function parseNumber<T>(data: string): T {
155
+ let type: T;
156
+ // @ts-ignore
157
+ if (type instanceof f64) return F64.parseFloat(data);
158
+ // @ts-ignore
159
+ else if (type instanceof f32) return F32.parseFloat(data);
160
+ // @ts-ignore
161
+ else if (type instanceof u64) return U64.parseInt(data);
162
+ // @ts-ignore
163
+ else if (type instanceof u32) return U32.parseInt(data);
164
+ // @ts-ignore
165
+ else if (type instanceof u8) return U8.parseInt(data);
166
+ // @ts-ignore
167
+ else if (type instanceof u16) return U16.parseInt(data);
168
+ // @ts-ignore
169
+ else if (type instanceof i64) return I64.parseInt(data);
170
+ // @ts-ignore
171
+ else if (type instanceof i32) return I32.parseInt(data);
172
+ // @ts-ignore
173
+ else if (type instanceof i16) return I16.parseInt(data);
174
+ // @ts-ignore
175
+ else if (type instanceof i8) return I8.parseInt(data);
176
+ else
177
+ throw new Error(
178
+ `JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
179
+ );
185
180
  }
186
181
 
187
182
  // @ts-ignore
188
183
  @inline
189
- export function parseObject<T>(data: string): T {
190
- let schema: T = changetype<T>(__new(offsetof<T>(), idof<T>()));
191
- const result = new Map<string, string>();
192
- let key = "";
193
- let isKey = false;
194
- let depth = 1;
195
- let char = 0;
196
- let outerLoopIndex = 1
197
- for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
198
- char = unsafeCharCodeAt(data, outerLoopIndex);
184
+ export function parseObject<T>(data: string): T {
185
+ let schema: T = changetype<T>(__new(offsetof<T>(), idof<T>()));
186
+ const result = new Map<string, string>();
187
+ let key = "";
188
+ let isKey = false;
189
+ let depth = 1;
190
+ let char = 0;
191
+ let outerLoopIndex = 1;
192
+ for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
193
+ char = unsafeCharCodeAt(data, outerLoopIndex);
194
+ if (char === leftBracketCode) {
195
+ for (
196
+ let arrayValueIndex = outerLoopIndex;
197
+ arrayValueIndex < data.length - 1;
198
+ arrayValueIndex++
199
+ ) {
200
+ char = unsafeCharCodeAt(data, arrayValueIndex);
199
201
  if (char === leftBracketCode) {
200
- for (let arrayValueIndex = outerLoopIndex; arrayValueIndex < data.length - 1; arrayValueIndex++) {
201
- char = unsafeCharCodeAt(data, arrayValueIndex);
202
- if (char === leftBracketCode) {
203
- depth = depth << 1;
204
- } else if (char === rightBracketCode) {
205
- depth = depth >> 1;
206
- if (depth === 1) {
207
- ++arrayValueIndex;
208
- result.set(
209
- key,
210
- data.slice(outerLoopIndex, arrayValueIndex)
211
- );
212
- outerLoopIndex = arrayValueIndex;
213
- isKey = false;
214
- break;
215
- }
216
- }
217
- }
218
- } else if (char === leftBraceCode) {
219
- for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
220
- char = unsafeCharCodeAt(data, objectValueIndex);
221
- if (char === leftBraceCode) {
222
- depth = depth << 1;
223
- } else if (char === rightBraceCode) {
224
- depth = depth >> 1;
225
- if (depth === 1) {
226
- ++objectValueIndex;
227
- result.set(
228
- key,
229
- data.slice(outerLoopIndex, objectValueIndex)
230
- );
231
- outerLoopIndex = objectValueIndex;
232
- isKey = false;
233
- break;
234
- }
235
- }
236
- }
237
- } else if (char === quoteCode) {
238
- for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
239
- char = unsafeCharCodeAt(data, stringValueIndex);
240
- if (
241
- char === quoteCode &&
242
- unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
243
- ) {
244
- if (isKey === false) {
245
- key = data.slice(outerLoopIndex, stringValueIndex);
246
- isKey = true;
247
- } else {
248
- result.set(
249
- key,
250
- data.slice(outerLoopIndex, stringValueIndex)
251
- );
252
- isKey = false;
253
- }
254
- outerLoopIndex = ++stringValueIndex;
255
- break;
256
- }
257
- }
258
- } else if (char == nCode) {
259
- result.set(
260
- key,
261
- "null"
262
- )
202
+ depth = depth << 1;
203
+ } else if (char === rightBracketCode) {
204
+ depth = depth >> 1;
205
+ if (depth === 1) {
206
+ ++arrayValueIndex;
207
+ result.set(key, data.slice(outerLoopIndex, arrayValueIndex));
208
+ outerLoopIndex = arrayValueIndex;
263
209
  isKey = false;
264
- } else if (
265
- char === tCode &&
266
- unsafeCharCodeAt(data, ++outerLoopIndex) === "r".charCodeAt(0) &&
267
- unsafeCharCodeAt(data, ++outerLoopIndex) === "u".charCodeAt(0) &&
268
- unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
269
- ) {
270
- result.set(key, "true");
210
+ break;
211
+ }
212
+ }
213
+ }
214
+ } else if (char === leftBraceCode) {
215
+ for (
216
+ let objectValueIndex = outerLoopIndex;
217
+ objectValueIndex < data.length - 1;
218
+ objectValueIndex++
219
+ ) {
220
+ char = unsafeCharCodeAt(data, objectValueIndex);
221
+ if (char === leftBraceCode) {
222
+ depth = depth << 1;
223
+ } else if (char === rightBraceCode) {
224
+ depth = depth >> 1;
225
+ if (depth === 1) {
226
+ ++objectValueIndex;
227
+ result.set(key, data.slice(outerLoopIndex, objectValueIndex));
228
+ outerLoopIndex = objectValueIndex;
271
229
  isKey = false;
272
- } else if (
273
- char === fCode &&
274
- unsafeCharCodeAt(data, ++outerLoopIndex) === "a".charCodeAt(0) &&
275
- unsafeCharCodeAt(data, ++outerLoopIndex) === "l".charCodeAt(0) &&
276
- unsafeCharCodeAt(data, ++outerLoopIndex) === "s".charCodeAt(0) &&
277
- unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
230
+ break;
231
+ }
232
+ }
233
+ }
234
+ } else if (char === quoteCode) {
235
+ for (
236
+ let stringValueIndex = ++outerLoopIndex;
237
+ stringValueIndex < data.length - 1;
238
+ stringValueIndex++
239
+ ) {
240
+ char = unsafeCharCodeAt(data, stringValueIndex);
241
+ if (
242
+ char === quoteCode &&
243
+ unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
278
244
  ) {
279
- result.set(key, "false");
245
+ if (isKey === false) {
246
+ key = data.slice(outerLoopIndex, stringValueIndex);
247
+ isKey = true;
248
+ } else {
249
+ result.set(key, data.slice(outerLoopIndex, stringValueIndex));
280
250
  isKey = false;
281
- } else if ((char >= 48 && char <= 57) || char === 45) {
282
- let numberValueIndex = ++outerLoopIndex;
283
- for (; numberValueIndex < data.length; numberValueIndex++) {
284
- char = unsafeCharCodeAt(data, numberValueIndex);
285
- if (
286
- char === commaCode ||
287
- char === rightBraceCode ||
288
- isSpace(char)
289
- ) {
290
- result.set(
291
- key,
292
- data.slice(outerLoopIndex - 1, numberValueIndex)
293
- );
294
- outerLoopIndex = numberValueIndex;
295
- isKey = false;
296
- break;
297
- }
298
- }
251
+ }
252
+ outerLoopIndex = ++stringValueIndex;
253
+ break;
254
+ }
255
+ }
256
+ } else if (char == nCode) {
257
+ result.set(key, "null");
258
+ isKey = false;
259
+ } else if (
260
+ char === tCode &&
261
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "r".charCodeAt(0) &&
262
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "u".charCodeAt(0) &&
263
+ unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
264
+ ) {
265
+ result.set(key, "true");
266
+ isKey = false;
267
+ } else if (
268
+ char === fCode &&
269
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "a".charCodeAt(0) &&
270
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "l".charCodeAt(0) &&
271
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "s".charCodeAt(0) &&
272
+ unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
273
+ ) {
274
+ result.set(key, "false");
275
+ isKey = false;
276
+ } else if ((char >= 48 && char <= 57) || char === 45) {
277
+ let numberValueIndex = ++outerLoopIndex;
278
+ for (; numberValueIndex < data.length; numberValueIndex++) {
279
+ char = unsafeCharCodeAt(data, numberValueIndex);
280
+ if (char === commaCode || char === rightBraceCode || isSpace(char)) {
281
+ result.set(key, data.slice(outerLoopIndex - 1, numberValueIndex));
282
+ outerLoopIndex = numberValueIndex;
283
+ isKey = false;
284
+ break;
299
285
  }
286
+ }
300
287
  }
301
- // @ts-ignore
302
- const deserialized = changetype<nonnull<T>>(schema).__JSON_Deserialize<T>(result)
303
- heap.free(changetype<usize>(schema));
304
- return deserialized;
288
+ }
289
+ // @ts-ignore
290
+ //const deserialized =
291
+ //changetype<nonnull<T>>(schema).__JSON_Deserialize<T>(result);
292
+ //heap.free(changetype<usize>(schema));
293
+ return changetype<nonnull<T>>(schema).__JSON_Deserialize<T>(result);
305
294
  }
306
295
 
307
296
  // @ts-ignore
308
297
  @inline
298
+ // @ts-ignore
299
+ export function parseArray<T extends unknown[]>(data: string): T {
300
+ let type!: valueof<T>;
301
+ if (type instanceof String) {
302
+ return <T>parseStringArray(data);
303
+ } else if (isBoolean<valueof<T>>()) {
309
304
  // @ts-ignore
310
- export function parseArray<T extends unknown[]>(data: string): T {
311
- // TODO: Replace with opt
312
- let type!: valueof<T>;
313
- if (type instanceof String) {
314
- return <T>parseStringArray(data);
315
- } else if (isBoolean<valueof<T>>()) {
316
- // @ts-ignore
317
- return parseBooleanArray<T>(data);
318
- } else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
319
- // @ts-ignore
320
- return parseNumberArray<T>(data);
321
- } else if (isArrayLike<valueof<T>>()) {
322
- // @ts-ignore
323
- return parseArrayArray<T>(data);
324
- // @ts-ignore
325
- } else if (isDefined(type.__JSON_Deserialize)) {
326
- // @ts-ignore
327
- return parseObjectArray<T>(data);
328
- }
305
+ return parseBooleanArray<T>(data);
306
+ } else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
307
+ // @ts-ignore
308
+ return parseNumberArray<T>(data);
309
+ } else if (isArrayLike<valueof<T>>()) {
310
+ // @ts-ignore
311
+ return parseArrayArray<T>(data);
312
+ // @ts-ignore
313
+ } else if (isDefined(type.__JSON_Deserialize)) {
314
+ // @ts-ignore
315
+ return parseObjectArray<T>(data);
316
+ }
329
317
  }
330
318
 
331
319
  // @ts-ignore
332
320
  @inline
333
- export function parseStringArray(data: string): string[] {
334
- const result: string[] = [];
335
- let lastPos = 0;
336
- let instr = false;
337
- for (let i = 1; i < data.length - 1; i++) {
338
- if (unsafeCharCodeAt(data, i) === quoteCode) {
339
- if (instr === false) {
340
- instr = true;
341
- lastPos = i;
342
- } else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
343
- instr = false;
344
- result.push(data.slice(lastPos + 1, i).replaceAll('\\"', '"'));
345
- }
346
- }
321
+ export function parseStringArray(data: string): string[] {
322
+ const result: string[] = [];
323
+ let lastPos = 0;
324
+ let instr = false;
325
+ for (let i = 1; i < data.length - 1; i++) {
326
+ if (unsafeCharCodeAt(data, i) === quoteCode) {
327
+ if (instr === false) {
328
+ instr = true;
329
+ lastPos = i;
330
+ } else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
331
+ instr = false;
332
+ result.push(data.slice(lastPos + 1, i).replaceAll('\\"', '"'));
333
+ }
347
334
  }
348
- return result;
335
+ }
336
+ return result;
349
337
  }
350
338
 
351
339
  // @ts-ignore
352
340
  @inline
353
- export function parseBooleanArray<T extends boolean[]>(data: string): T {
354
- const result = instantiate<T>();
355
- let lastPos = 1;
356
- let char = 0;
357
- for (let i = 1; i < data.length - 1; i++) {
358
- char = unsafeCharCodeAt(data, i);
359
- /*// if char == "t" && i+3 == "e"
341
+ export function parseBooleanArray<T extends boolean[]>(data: string): T {
342
+ const result = instantiate<T>();
343
+ let lastPos = 1;
344
+ let char = 0;
345
+ for (let i = 1; i < data.length - 1; i++) {
346
+ char = unsafeCharCodeAt(data, i);
347
+ /*// if char == "t" && i+3 == "e"
360
348
  if (char === tCode && data.charCodeAt(i + 3) === eCode) {
361
349
  //i += 3;
362
350
  result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
@@ -366,98 +354,98 @@ export class JSON {
366
354
  result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
367
355
  //i++;
368
356
  }*/
369
- if (char === tCode || char === fCode) {
370
- lastPos = i;
371
- } else if (char === eCode) {
372
- i++;
373
- result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i)));
374
- }
357
+ if (char === tCode || char === fCode) {
358
+ lastPos = i;
359
+ } else if (char === eCode) {
360
+ i++;
361
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i)));
375
362
  }
376
- return result;
363
+ }
364
+ return result;
377
365
  }
378
366
 
379
367
  // @ts-ignore
380
368
  @inline
381
- export function parseNumberArray<T extends number[]>(data: string): T {
382
- const result = instantiate<T>();
383
- let lastPos = 0;
384
- let char = 0;
385
- let i = 1;
386
- for (; i < data.length - 1; i++) {
387
- char = unsafeCharCodeAt(data, i);
388
- if (lastPos === 0 && (char >= 48 && char <= 57) || char === 45) {
389
- lastPos = i;
390
- } else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
391
- result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
392
- lastPos = 0;
393
- }
369
+ export function parseNumberArray<T extends number[]>(data: string): T {
370
+ const result = instantiate<T>();
371
+ let lastPos = 0;
372
+ let char = 0;
373
+ let i = 1;
374
+ for (; i < data.length - 1; i++) {
375
+ char = unsafeCharCodeAt(data, i);
376
+ if ((lastPos === 0 && char >= 48 && char <= 57) || char === 45) {
377
+ lastPos = i;
378
+ } else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
379
+ result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
380
+ lastPos = 0;
394
381
  }
395
- for (; i > lastPos - 1; i--) {
396
- char = unsafeCharCodeAt(data, i);
397
- if (char !== rightBracketCode) {
398
- result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
399
- break;
400
- }
382
+ }
383
+ for (; i > lastPos - 1; i--) {
384
+ char = unsafeCharCodeAt(data, i);
385
+ if (char !== rightBracketCode) {
386
+ result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
387
+ break;
401
388
  }
402
- return result;
389
+ }
390
+ return result;
403
391
  }
404
392
 
405
393
  // @ts-ignore
406
394
  @inline
407
- export function parseArrayArray<T extends unknown[][]>(data: string): T {
408
- const result = instantiate<T>();
409
- let char = 0;
410
- let lastPos = 0;
411
- let depth = 1;
412
- let i = 1;
413
- // Find start of bracket
414
- //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
415
- //i++;
416
- for (; i < data.length - 1; i++) {
417
- char = unsafeCharCodeAt(data, i);
418
- if (char === leftBracketCode) {
419
- if (depth === 1) {
420
- lastPos = i;
421
- }
422
- // Shifting is 6% faster than incrementing
423
- depth = depth << 1;
424
- } else if (char === rightBracketCode) {
425
- depth = depth >> 1;
426
- if (depth === 1) {
427
- i++;
428
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
429
- }
430
- }
395
+ export function parseArrayArray<T extends unknown[][]>(data: string): T {
396
+ const result = instantiate<T>();
397
+ let char = 0;
398
+ let lastPos = 0;
399
+ let depth = 1;
400
+ let i = 1;
401
+ // Find start of bracket
402
+ //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
403
+ //i++;
404
+ for (; i < data.length - 1; i++) {
405
+ char = unsafeCharCodeAt(data, i);
406
+ if (char === leftBracketCode) {
407
+ if (depth === 1) {
408
+ lastPos = i;
409
+ }
410
+ // Shifting is 6% faster than incrementing
411
+ depth = depth << 1;
412
+ } else if (char === rightBracketCode) {
413
+ depth = depth >> 1;
414
+ if (depth === 1) {
415
+ i++;
416
+ result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
417
+ }
431
418
  }
432
- return result;
419
+ }
420
+ return result;
433
421
  }
434
422
 
435
423
  // @ts-ignore
436
424
  @inline
437
- export function parseObjectArray<T extends unknown[][]>(data: string): T {
438
- const result = instantiate<T>();
439
- let char = 0;
440
- let lastPos = 1;
441
- let depth = 1;
442
- let i = 1;
443
- // Find start of bracket
444
- //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
445
- //i++;
446
- for (; i < data.length - 1; i++) {
447
- char = unsafeCharCodeAt(data, i);
448
- if (char === leftBraceCode) {
449
- if (depth === 1) {
450
- lastPos = i;
451
- }
452
- // Shifting is 6% faster than incrementing
453
- depth = depth << 1;
454
- } else if (char === rightBraceCode) {
455
- depth = depth >> 1;
456
- if (depth === 1) {
457
- i++;
458
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
459
- }
460
- }
425
+ export function parseObjectArray<T extends unknown[][]>(data: string): T {
426
+ const result = instantiate<T>();
427
+ let char = 0;
428
+ let lastPos = 1;
429
+ let depth = 1;
430
+ let i = 1;
431
+ // Find start of bracket
432
+ //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
433
+ //i++;
434
+ for (; i < data.length - 1; i++) {
435
+ char = unsafeCharCodeAt(data, i);
436
+ if (char === leftBraceCode) {
437
+ if (depth === 1) {
438
+ lastPos = i;
439
+ }
440
+ // Shifting is 6% faster than incrementing
441
+ depth = depth << 1;
442
+ } else if (char === rightBraceCode) {
443
+ depth = depth >> 1;
444
+ if (depth === 1) {
445
+ i++;
446
+ result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
447
+ }
461
448
  }
462
- return result;
449
+ }
450
+ return result;
463
451
  }