json-as 0.5.19 → 0.5.21
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/LICENSE +1 -1
- package/README.md +2 -20
- package/assembly/__tests__/as-json.spec.ts +4 -3
- package/assembly/src/json.ts +13 -13
- package/assembly/test.ts +9 -1
- package/package.json +4 -4
- package/transform/lib/index.js +3 -1
- package/transform/package.json +1 -1
- package/transform/src/index.ts +3 -1
- package/assembly/Candle.ts +0 -18
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2023 Jairus Tanaka <jairus.v.tanaka@outlook.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -73,28 +73,10 @@ const stringified = JSON.stringify<Player>(player);
|
|
|
73
73
|
const parsed = JSON.parse<Player>(stringified);
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
#
|
|
76
|
+
# Notes
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
Performance is nearly equal to the JavaScript JSON implementation which is in C++.
|
|
79
79
|
|
|
80
|
-
Yes, it does. However, dynamic objects and arrays are not supported, but planned in the near future.
|
|
81
|
-
|
|
82
|
-
**Is it fast?**
|
|
83
|
-
|
|
84
|
-
Look below
|
|
85
|
-
|
|
86
|
-
**How does it compare to other libs?**
|
|
87
|
-
|
|
88
|
-
Its pretty much the same as the other libraries out there (near/assemblyscript-json and @serial-as/json), but it focuses highly on performance
|
|
89
|
-
|
|
90
|
-
**Will it catch invalid JSON?**
|
|
91
|
-
|
|
92
|
-
No, it does not check for invalid JSON, but gives its best shot at parsing instead. Will probably throw an error.
|
|
93
|
-
|
|
94
|
-
**How does it compare performance-wise to other libraries?**
|
|
95
|
-
|
|
96
|
-
In my testing, parsing a Vector 2 runs at 2.2m ops/s with as-json and around 10,000 ops/s with assemblyscript-json and @serial-as/json.
|
|
97
|
-
Both are great libraries however.
|
|
98
80
|
## Performance
|
|
99
81
|
|
|
100
82
|
**Serialize Object (Vec2):** ~7.20m ops/s
|
|
@@ -9,9 +9,9 @@ function canSerde<T>(data: T): void {
|
|
|
9
9
|
// @ts-ignore
|
|
10
10
|
@json
|
|
11
11
|
class Vec3 {
|
|
12
|
-
x:
|
|
13
|
-
y:
|
|
14
|
-
z:
|
|
12
|
+
x: f64;
|
|
13
|
+
y: f64;
|
|
14
|
+
z: f64;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// @ts-ignore
|
|
@@ -136,6 +136,7 @@ describe("Ser/de Array", () => {
|
|
|
136
136
|
});
|
|
137
137
|
|
|
138
138
|
it("should ser/de string arrays", () => {
|
|
139
|
+
// ["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]
|
|
139
140
|
canSerde<string[]>([
|
|
140
141
|
"abcdefg",
|
|
141
142
|
'st"ring" w""ith quotes"',
|
package/assembly/src/json.ts
CHANGED
|
@@ -241,7 +241,7 @@ export function parseNumber<T>(data: string): T {
|
|
|
241
241
|
// @ts-ignore
|
|
242
242
|
const type: T = 0;
|
|
243
243
|
// @ts-ignore
|
|
244
|
-
if (type instanceof f64) return
|
|
244
|
+
if (type instanceof f64) return f64.parse(data);
|
|
245
245
|
// @ts-ignore
|
|
246
246
|
else if (type instanceof f32) return f32.parse(data);
|
|
247
247
|
// @ts-ignore
|
|
@@ -268,7 +268,7 @@ function parseObject<T>(data: string): T {
|
|
|
268
268
|
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
|
|
269
269
|
let key = "";
|
|
270
270
|
let isKey = false;
|
|
271
|
-
let depth =
|
|
271
|
+
let depth = 0;
|
|
272
272
|
let char = 0;
|
|
273
273
|
let outerLoopIndex = 1;
|
|
274
274
|
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
@@ -281,10 +281,10 @@ function parseObject<T>(data: string): T {
|
|
|
281
281
|
) {
|
|
282
282
|
char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
283
283
|
if (char === leftBracketCode) {
|
|
284
|
-
depth
|
|
284
|
+
depth++;
|
|
285
285
|
} else if (char === rightBracketCode) {
|
|
286
|
-
depth
|
|
287
|
-
if (depth ===
|
|
286
|
+
depth--;
|
|
287
|
+
if (depth === 0) {
|
|
288
288
|
++arrayValueIndex;
|
|
289
289
|
// @ts-ignore
|
|
290
290
|
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, arrayValueIndex));
|
|
@@ -302,10 +302,10 @@ function parseObject<T>(data: string): T {
|
|
|
302
302
|
) {
|
|
303
303
|
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
304
304
|
if (char === leftBraceCode) {
|
|
305
|
-
depth
|
|
305
|
+
depth++;
|
|
306
306
|
} else if (char === rightBraceCode) {
|
|
307
|
-
depth
|
|
308
|
-
if (depth ===
|
|
307
|
+
depth--;
|
|
308
|
+
if (depth === 0) {
|
|
309
309
|
++objectValueIndex;
|
|
310
310
|
// @ts-ignore
|
|
311
311
|
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, objectValueIndex));
|
|
@@ -486,7 +486,7 @@ function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
|
486
486
|
const result = instantiate<T>();
|
|
487
487
|
let char = 0;
|
|
488
488
|
let lastPos = 0;
|
|
489
|
-
let depth =
|
|
489
|
+
let depth = 0;
|
|
490
490
|
let i = 1;
|
|
491
491
|
// Find start of bracket
|
|
492
492
|
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
|
|
@@ -494,14 +494,14 @@ function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
|
494
494
|
for (; i < data.length - 1; i++) {
|
|
495
495
|
char = unsafeCharCodeAt(data, i);
|
|
496
496
|
if (char === leftBracketCode) {
|
|
497
|
-
if (depth ===
|
|
497
|
+
if (depth === 0) {
|
|
498
498
|
lastPos = i;
|
|
499
499
|
}
|
|
500
500
|
// Shifting is 6% faster than incrementing
|
|
501
|
-
depth
|
|
501
|
+
depth++;
|
|
502
502
|
} else if (char === rightBracketCode) {
|
|
503
|
-
depth
|
|
504
|
-
if (depth ===
|
|
503
|
+
depth--;
|
|
504
|
+
if (depth === 0) {
|
|
505
505
|
i++;
|
|
506
506
|
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
507
507
|
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { u128 } from "as-bignum/assembly";
|
|
2
|
-
import { Candle } from "./Candle";
|
|
3
2
|
import {
|
|
4
3
|
JSON
|
|
5
4
|
} from ".";
|
|
6
5
|
|
|
6
|
+
const exp = `["abcdefg","st\\"ring\\" w\\"\\"ith quotes\\"","string \\t\\r\\"with ran\\tdom spa\\nces and \\nnewlines\\n\\n\\n","string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""]`;
|
|
7
|
+
|
|
8
|
+
console.log(exp);
|
|
9
|
+
console.log(JSON.stringify([
|
|
10
|
+
"abcdefg",
|
|
11
|
+
'st"ring" w""ith quotes"',
|
|
12
|
+
'string \t\r"with ran\tdom spa\nces and \nnewlines\n\n\n',
|
|
13
|
+
'string with colon : comma , brace [ ] bracket { } and quote " and other quote "',
|
|
14
|
+
]))
|
|
7
15
|
// @ts-ignore
|
|
8
16
|
@JSON
|
|
9
17
|
class Vec3 {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-as",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.21",
|
|
4
4
|
"description": "JSON encoder/decoder for AssemblyScript",
|
|
5
5
|
"types": "assembly/index.ts",
|
|
6
6
|
"author": "Jairus Tanaka",
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
"@as-tral/cli": "^2.0.0",
|
|
26
26
|
"@assemblyscript/loader": "^0.27.0",
|
|
27
27
|
"@assemblyscript/wasi-shim": "^0.1.0",
|
|
28
|
-
"as-bignum": "^0.2.23",
|
|
29
28
|
"assemblyscript": "^0.27.0",
|
|
30
29
|
"assemblyscript-prettier": "^1.0.7",
|
|
31
|
-
"prettier": "^2.8.
|
|
30
|
+
"prettier": "^2.8.4",
|
|
32
31
|
"typescript": "^4.9.5",
|
|
33
32
|
"visitor-as": "^0.11.4"
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
|
36
|
-
"as-
|
|
35
|
+
"as-bignum": "^0.2.23",
|
|
36
|
+
"as-string-sink": "^0.5.3",
|
|
37
37
|
"as-variant": "^0.4.1"
|
|
38
38
|
},
|
|
39
39
|
"repository": {
|
package/transform/lib/index.js
CHANGED
|
@@ -85,7 +85,9 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
85
85
|
console.error("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?");
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
this.
|
|
88
|
+
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
89
|
+
const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])];
|
|
90
|
+
this.visit(members);
|
|
89
91
|
let serializeFunc = "";
|
|
90
92
|
if (this.currentClass.encodeStmts.length > 0) {
|
|
91
93
|
const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -94,7 +94,9 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
this.
|
|
97
|
+
const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
|
|
98
|
+
const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])]
|
|
99
|
+
this.visit(members);
|
|
98
100
|
|
|
99
101
|
let serializeFunc = "";
|
|
100
102
|
|
package/assembly/Candle.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
JSON
|
|
3
|
-
} from ".";
|
|
4
|
-
@json
|
|
5
|
-
export class Candle {
|
|
6
|
-
timestamp!: i64;
|
|
7
|
-
high!: f64;
|
|
8
|
-
low!: f64;
|
|
9
|
-
open!: f64;
|
|
10
|
-
close!: f64;
|
|
11
|
-
constructor(timestamp: i64, high: f64, low: f64, open: f64, close: f64) {
|
|
12
|
-
this.timestamp = timestamp;
|
|
13
|
-
this.high = high;
|
|
14
|
-
this.low = low;
|
|
15
|
-
this.open = open;
|
|
16
|
-
this.close = close;
|
|
17
|
-
}
|
|
18
|
-
}
|