json-as 0.4.7 → 0.4.9
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/as-pect.asconfig.json +23 -23
- package/as-pect.config.js +1 -1
- package/asconfig.json +1 -1
- package/assembly/__benches__/{benchmark.ts → as-json.ts} +35 -32
- package/assembly/__tests__/as-json.spec.ts +3 -2
- package/assembly/__tests__/as-json.spec.wasm +0 -0
- package/assembly/__tests__/as-json.spec.wat +18630 -0
- package/assembly/index.ts +26 -23
- package/assembly/test.ts +1 -1
- package/package.json +3 -1
- package/transform/package.json +3 -3
package/assembly/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { StringSink } from "as-string-sink/assembly";
|
|
2
2
|
import { Variant } from "as-variant/assembly";
|
|
3
|
-
import { isSpace } from "
|
|
4
|
-
import { stringify } from "as-console/assembly";
|
|
3
|
+
import { isSpace } from "util/string";
|
|
5
4
|
import {
|
|
6
5
|
backSlashCode,
|
|
7
6
|
colonCode,
|
|
@@ -80,11 +79,11 @@ export class JSON {
|
|
|
80
79
|
// @ts-ignore
|
|
81
80
|
for (let i = 0; i < data.length - 1; i++) {
|
|
82
81
|
// @ts-ignore
|
|
83
|
-
result.write(stringify(unchecked(data[i])));
|
|
82
|
+
result.write(JSON.stringify(unchecked(data[i])));
|
|
84
83
|
result.write(",");
|
|
85
84
|
}
|
|
86
85
|
// @ts-ignore
|
|
87
|
-
result.write(stringify(unchecked(data[data.length - 1])));
|
|
86
|
+
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
88
87
|
result.write("]");
|
|
89
88
|
return result.toString();
|
|
90
89
|
}
|
|
@@ -128,13 +127,13 @@ export class JSON {
|
|
|
128
127
|
|
|
129
128
|
// @ts-ignore
|
|
130
129
|
@inline
|
|
131
|
-
function parseString(data: string): string {
|
|
130
|
+
function parseString(data: string): string {
|
|
132
131
|
return data.slice(1, data.length - 1).replaceAll('\\"', '"');
|
|
133
132
|
}
|
|
134
133
|
|
|
135
134
|
// @ts-ignore
|
|
136
135
|
@inline
|
|
137
|
-
function parseBoolean<T extends boolean>(data: string): T {
|
|
136
|
+
function parseBoolean<T extends boolean>(data: string): T {
|
|
138
137
|
if (data.length > 3 && data.startsWith("true")) return <T>true;
|
|
139
138
|
else if (data.length > 4 && data.startsWith("false")) return <T>false;
|
|
140
139
|
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
|
|
@@ -142,14 +141,14 @@ function parseBoolean<T extends boolean>(data: string): T {
|
|
|
142
141
|
|
|
143
142
|
// @ts-ignore
|
|
144
143
|
@inline
|
|
145
|
-
function parseNumber<T>(data: string): T {
|
|
144
|
+
function parseNumber<T>(data: string): T {
|
|
146
145
|
let type: T;
|
|
147
146
|
// @ts-ignore
|
|
148
147
|
if (type instanceof f64) return F64.parseFloat(data);
|
|
149
148
|
// @ts-ignore
|
|
150
149
|
else if (type instanceof f32) return F32.parseFloat(data);
|
|
151
150
|
// @ts-ignore
|
|
152
|
-
else if (type instanceof
|
|
151
|
+
else if (type instanceof u64) return U64.parseInt(data);
|
|
153
152
|
// @ts-ignore
|
|
154
153
|
else if (type instanceof u32) return U32.parseInt(data);
|
|
155
154
|
// @ts-ignore
|
|
@@ -157,6 +156,10 @@ function parseNumber<T>(data: string): T {
|
|
|
157
156
|
// @ts-ignore
|
|
158
157
|
else if (type instanceof u16) return U16.parseInt(data);
|
|
159
158
|
// @ts-ignore
|
|
159
|
+
else if (type instanceof i64) return I64.parseInt(data);
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
else if (type instanceof i32) return I32.parseInt(data);
|
|
162
|
+
// @ts-ignore
|
|
160
163
|
else if (type instanceof i16) return I16.parseInt(data);
|
|
161
164
|
// @ts-ignore
|
|
162
165
|
else if (type instanceof i8) return I8.parseInt(data);
|
|
@@ -168,7 +171,7 @@ function parseNumber<T>(data: string): T {
|
|
|
168
171
|
|
|
169
172
|
// @ts-ignore
|
|
170
173
|
@inline
|
|
171
|
-
export function parseObject<T>(data: string): T {
|
|
174
|
+
export function parseObject<T>(data: string): T {
|
|
172
175
|
let schema!: T;
|
|
173
176
|
const result = new Map<string, string>();
|
|
174
177
|
let key = "";
|
|
@@ -269,7 +272,7 @@ export function parseObject<T>(data: string): T {
|
|
|
269
272
|
) {
|
|
270
273
|
result.set(key, "false");
|
|
271
274
|
isKey = false;
|
|
272
|
-
} else if (char >= 48 && char <= 57) {
|
|
275
|
+
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
273
276
|
let numberValueIndex = ++outerLoopIndex;
|
|
274
277
|
for (; numberValueIndex < data.length - 1; numberValueIndex++) {
|
|
275
278
|
char = unsafeCharCodeAt(data, numberValueIndex);
|
|
@@ -295,18 +298,18 @@ export function parseObject<T>(data: string): T {
|
|
|
295
298
|
|
|
296
299
|
// @ts-ignore
|
|
297
300
|
@inline
|
|
298
|
-
// @ts-ignore
|
|
299
|
-
export function parseArray<T extends unknown[]>(data: string): T {
|
|
301
|
+
// @ts-ignore
|
|
302
|
+
export function parseArray<T extends unknown[]>(data: string): T {
|
|
300
303
|
// TODO: Replace with opt
|
|
301
304
|
let type!: valueof<T>;
|
|
302
305
|
if (type instanceof String) {
|
|
303
306
|
return <T>parseStringArray(data);
|
|
304
|
-
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
305
|
-
// @ts-ignore
|
|
306
|
-
return parseNumberArray<T>(data);
|
|
307
307
|
} else if (isBoolean<valueof<T>>()) {
|
|
308
308
|
// @ts-ignore
|
|
309
309
|
return parseBooleanArray<T>(data);
|
|
310
|
+
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
311
|
+
// @ts-ignore
|
|
312
|
+
return parseNumberArray<T>(data);
|
|
310
313
|
} else if (isArrayLike<valueof<T>>()) {
|
|
311
314
|
// @ts-ignore
|
|
312
315
|
return parseArrayArray<T>(data);
|
|
@@ -319,7 +322,7 @@ export function parseArray<T extends unknown[]>(data: string): T {
|
|
|
319
322
|
|
|
320
323
|
// @ts-ignore
|
|
321
324
|
@inline
|
|
322
|
-
export function parseStringArray(data: string): string[] {
|
|
325
|
+
export function parseStringArray(data: string): string[] {
|
|
323
326
|
const result: string[] = [];
|
|
324
327
|
let lastPos = 0;
|
|
325
328
|
let instr = false;
|
|
@@ -339,7 +342,7 @@ export function parseStringArray(data: string): string[] {
|
|
|
339
342
|
|
|
340
343
|
// @ts-ignore
|
|
341
344
|
@inline
|
|
342
|
-
export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
345
|
+
export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
343
346
|
const result = instantiate<T>();
|
|
344
347
|
let lastPos = 1;
|
|
345
348
|
let char = 0;
|
|
@@ -367,21 +370,21 @@ export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
|
367
370
|
|
|
368
371
|
// @ts-ignore
|
|
369
372
|
@inline
|
|
370
|
-
export function parseNumberArray<T extends number[]>(data: string): T {
|
|
373
|
+
export function parseNumberArray<T extends number[]>(data: string): T {
|
|
371
374
|
const result = instantiate<T>();
|
|
372
375
|
let lastPos = 0;
|
|
373
376
|
let char = 0;
|
|
374
377
|
let i = 1;
|
|
375
378
|
for (; i < data.length - 1; i++) {
|
|
376
379
|
char = unsafeCharCodeAt(data, i);
|
|
377
|
-
if (lastPos === 0 && char >= 48 && char <= 57) {
|
|
380
|
+
if (lastPos === 0 && (char >= 48 && char <= 57) || char === 45) {
|
|
378
381
|
lastPos = i;
|
|
379
382
|
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
|
|
380
383
|
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
|
|
381
384
|
lastPos = 0;
|
|
382
385
|
}
|
|
383
386
|
}
|
|
384
|
-
for (; i > lastPos; i--) {
|
|
387
|
+
for (; i > lastPos - 1; i--) {
|
|
385
388
|
char = unsafeCharCodeAt(data, i);
|
|
386
389
|
if (char !== rightBracketCode) {
|
|
387
390
|
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
|
|
@@ -393,7 +396,7 @@ export function parseNumberArray<T extends number[]>(data: string): T {
|
|
|
393
396
|
|
|
394
397
|
// @ts-ignore
|
|
395
398
|
@inline
|
|
396
|
-
export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
399
|
+
export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
397
400
|
const result = instantiate<T>();
|
|
398
401
|
let char = 0;
|
|
399
402
|
let lastPos = 0;
|
|
@@ -423,7 +426,7 @@ export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
|
423
426
|
|
|
424
427
|
// @ts-ignore
|
|
425
428
|
@inline
|
|
426
|
-
export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
429
|
+
export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
427
430
|
const result = instantiate<T>();
|
|
428
431
|
let char = 0;
|
|
429
432
|
let lastPos = 1;
|
|
@@ -451,4 +454,4 @@ export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
|
451
454
|
return result;
|
|
452
455
|
}
|
|
453
456
|
|
|
454
|
-
class Nullable {}
|
|
457
|
+
class Nullable { }
|
package/assembly/test.ts
CHANGED
|
@@ -46,4 +46,4 @@ const serializedVec2 = JSON.stringify<Vec2>(vec);
|
|
|
46
46
|
console.log("Serialized Vec2: " + serializedVec2);
|
|
47
47
|
const deserializedVec2 = JSON.parse<Vec2>(serializedVec2);
|
|
48
48
|
|
|
49
|
-
console.log("Deserialized: " + JSON.stringify(deserializedVec2));
|
|
49
|
+
console.log("Deserialized Vec2: " + JSON.stringify(deserializedVec2));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.9",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@as-pect/cli": "^7.0.7",
|
|
22
22
|
"@as-tral/cli": "^1.1.1",
|
|
23
|
+
"@serial-as/json": "^1.0.2",
|
|
23
24
|
"as-console": "^6.0.2",
|
|
25
|
+
"as-rainbow": "^0.1.0",
|
|
24
26
|
"assemblyscript": "^0.20.7",
|
|
25
27
|
"assemblyscript-prettier": "^1.0.2",
|
|
26
28
|
"typescript": "^4.7.2"
|
package/transform/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
{
|
|
2
2
|
"name": "@json-as/transform",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.9",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"json",
|
|
22
22
|
"serialize",
|
|
23
23
|
"deserialize",
|
|
24
|
-
"
|
|
24
|
+
"serde"
|
|
25
25
|
],
|
|
26
26
|
"bugs": {
|
|
27
27
|
"url": "https://github.com/JairusSW/as-json/issues"
|