json-as 0.5.33 → 0.5.34

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/test.ts CHANGED
@@ -2,20 +2,47 @@ import { backSlashCode, quoteCode } from "./src/chars";
2
2
  import { JSON } from "./src/json";
3
3
  import { atoi_fast, unsafeCharCodeAt } from "./src/util";
4
4
 
5
- // @json or @serializable work here
6
5
  @json
7
6
  class Vec3 {
8
- x: i32;
9
- y: i32;
10
- z: i32;
7
+ x!: f32;
8
+ y!: f32;
9
+ z!: f32;
11
10
  }
12
11
 
12
+ @json
13
+ class Player {
14
+ firstName!: string;
15
+ lastName!: string;
16
+ lastActive!: i32[];
17
+ age!: i32;
18
+ pos!: Vec3 | null;
19
+ isVerified!: boolean;
20
+ }
21
+
22
+ const player: Player = {
23
+ firstName: "Emmet",
24
+ lastName: "West",
25
+ lastActive: [8, 27, 2022],
26
+ age: 23,
27
+ pos: {
28
+ x: 3.4,
29
+ y: 1.2,
30
+ z: 8.3
31
+ },
32
+ isVerified: true
33
+ };
34
+
13
35
  const vec: Vec3 = {
14
36
  x: 3,
15
37
  y: 1,
16
38
  z: 8
17
39
  }
18
40
 
41
+ const serializedPlayer = JSON.stringify<Player>(player);
42
+ console.log(serializedPlayer);
43
+ const parsedPlayer = JSON.parse<Player>(serializedPlayer);
44
+ console.log(JSON.stringify(parsedPlayer));
45
+
19
46
  const serializedVec3 = JSON.stringify(vec);
20
47
  console.log(serializedVec3);
21
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.33",
3
+ "version": "0.5.34",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -0,0 +1,74 @@
1
+ // XXHash 32-bit as a starting point, see: https://cyan4973.github.io/xxHash
2
+ // primes
3
+ // @ts-ignore: decorator
4
+ const XXH32_P1 = 2654435761;
5
+ // @ts-ignore: decorator
6
+ const XXH32_P2 = 2246822519;
7
+ // @ts-ignore: decorator
8
+ const XXH32_P3 = 3266489917;
9
+ // @ts-ignore: decorator
10
+ const XXH32_P4 = 668265263;
11
+ // @ts-ignore: decorator
12
+ const XXH32_P5 = 374761393;
13
+ // @ts-ignore: decorator
14
+ const XXH32_SEED = 0;
15
+ function hash32(key, len = 4) {
16
+ let h = XXH32_SEED + XXH32_P5 + len;
17
+ h += key * XXH32_P3;
18
+ h = rotl(h, 17) * XXH32_P4;
19
+ h ^= h >> 15;
20
+ h *= XXH32_P2;
21
+ h ^= h >> 13;
22
+ h *= XXH32_P3;
23
+ h ^= h >> 16;
24
+ return h;
25
+ }
26
+ function rotl(x, r) {
27
+ return (x << r) | (x >>> (32 - r));
28
+ }
29
+ function mix(h, key) {
30
+ return rotl(h + key * XXH32_P2, 13) * XXH32_P1;
31
+ }
32
+ export function hashStr(key) {
33
+ if (key == null)
34
+ return XXH32_SEED;
35
+ let h = key.length;
36
+ let len = h;
37
+ let pos = 0;
38
+ if (len >= 16) {
39
+ let s1 = XXH32_SEED + XXH32_P1 + XXH32_P2;
40
+ let s2 = XXH32_SEED + XXH32_P2;
41
+ let s3 = XXH32_SEED;
42
+ let s4 = XXH32_SEED - XXH32_P1;
43
+ let end = len + pos - 16;
44
+ while (pos <= end) {
45
+ s1 = mix(s1, key.charCodeAt(pos));
46
+ s2 = mix(s2, key.charCodeAt(pos + 1));
47
+ s3 = mix(s3, key.charCodeAt(pos + 2));
48
+ s4 = mix(s4, load(pos, 12));
49
+ pos += 16;
50
+ }
51
+ h += rotl(s1, 1) + rotl(s2, 7) + rotl(s3, 12) + rotl(s4, 18);
52
+ }
53
+ else {
54
+ h += XXH32_SEED + XXH32_P5;
55
+ }
56
+ let end = changetype(key) + len - 4;
57
+ while (pos <= end) {
58
+ h += load(pos) * XXH32_P3;
59
+ h = rotl(h, 17) * XXH32_P4;
60
+ pos += 4;
61
+ }
62
+ end = changetype(key) + len;
63
+ while (pos < end) {
64
+ h += load(pos) * XXH32_P5;
65
+ h = rotl(h, 11) * XXH32_P1;
66
+ pos++;
67
+ }
68
+ h ^= h >> 15;
69
+ h *= XXH32_P2;
70
+ h ^= h >> 13;
71
+ h *= XXH32_P3;
72
+ h ^= h >> 16;
73
+ return h;
74
+ }
@@ -68,7 +68,7 @@ class AsJSONTransform extends BaseVisitor {
68
68
  if (lineText.startsWith("private"))
69
69
  return;
70
70
  // @ts-ignore
71
- const type = member.type.name.identifier.text;
71
+ let type = toString(member.type);
72
72
  const name = member.name.text;
73
73
  this.currentClass.keys.push(name);
74
74
  // @ts-ignore
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.33",
3
+ "version": "0.5.34",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -0,0 +1,83 @@
1
+ // XXHash 32-bit as a starting point, see: https://cyan4973.github.io/xxHash
2
+
3
+ // primes
4
+ // @ts-ignore: decorator
5
+ const XXH32_P1: number = 2654435761;
6
+ // @ts-ignore: decorator
7
+ const XXH32_P2: number = 2246822519;
8
+ // @ts-ignore: decorator
9
+ const XXH32_P3: number = 3266489917;
10
+ // @ts-ignore: decorator
11
+ const XXH32_P4: number = 668265263;
12
+ // @ts-ignore: decorator
13
+ const XXH32_P5: number = 374761393;
14
+ // @ts-ignore: decorator
15
+ const XXH32_SEED: number = 0;
16
+
17
+ function hash32(key: number, len: number = 4): number {
18
+ let h: number = XXH32_SEED + XXH32_P5 + len;
19
+ h += key * XXH32_P3;
20
+ h = rotl(h, 17) * XXH32_P4;
21
+ h ^= h >> 15;
22
+ h *= XXH32_P2;
23
+ h ^= h >> 13;
24
+ h *= XXH32_P3;
25
+ h ^= h >> 16;
26
+ return h;
27
+ }
28
+
29
+ function rotl(x: number, r: number): number {
30
+ return (x << r) | (x >>> (32 - r));
31
+ }
32
+
33
+ function mix(h: number, key: number): number {
34
+ return rotl(h + key * XXH32_P2, 13) * XXH32_P1;
35
+ }
36
+
37
+ export function hashStr(key: string): number {
38
+ if (key == null) return XXH32_SEED;
39
+
40
+ let h: number = key.length;
41
+ let len: number = h;
42
+ let pos = 0;
43
+
44
+ if (len >= 16) {
45
+ let s1 = XXH32_SEED + XXH32_P1 + XXH32_P2;
46
+ let s2 = XXH32_SEED + XXH32_P2;
47
+ let s3 = XXH32_SEED;
48
+ let s4 = XXH32_SEED - XXH32_P1;
49
+
50
+ let end = len + pos - 16;
51
+ while (pos <= end) {
52
+ s1 = mix(s1, key.charCodeAt(pos));
53
+ s2 = mix(s2, key.charCodeAt(pos + 1));
54
+ s3 = mix(s3, key.charCodeAt(pos + 2));
55
+ s4 = mix(s4, load<number>(pos, 12));
56
+ pos += 16;
57
+ }
58
+ h += rotl(s1, 1) + rotl(s2, 7) + rotl(s3, 12) + rotl(s4, 18);
59
+ } else {
60
+ h += XXH32_SEED + XXH32_P5;
61
+ }
62
+
63
+ let end = changetype<number>(key) + len - 4;
64
+ while (pos <= end) {
65
+ h += load<number>(pos) * XXH32_P3;
66
+ h = rotl(h, 17) * XXH32_P4;
67
+ pos += 4;
68
+ }
69
+
70
+ end = changetype<number>(key) + len;
71
+ while (pos < end) {
72
+ h += <number>load<u8>(pos) * XXH32_P5;
73
+ h = rotl(h, 11) * XXH32_P1;
74
+ pos++;
75
+ }
76
+
77
+ h ^= h >> 15;
78
+ h *= XXH32_P2;
79
+ h ^= h >> 13;
80
+ h *= XXH32_P3;
81
+ h ^= h >> 16;
82
+ return h;
83
+ }
@@ -71,7 +71,8 @@ class AsJSONTransform extends BaseVisitor {
71
71
  if (lineText.startsWith("private")) return;
72
72
 
73
73
  // @ts-ignore
74
- const type = member.type.name.identifier.text;
74
+ let type = toString(member.type);
75
+
75
76
  const name = member.name.text;
76
77
  this.currentClass.keys.push(name);
77
78
  // @ts-ignore