json-as 0.5.16 → 0.5.18

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/asconfig.json CHANGED
@@ -11,7 +11,6 @@
11
11
  },
12
12
  "options": {
13
13
  "transform": [
14
- "./transform",
15
14
  "./transform"
16
15
  ],
17
16
  "bindings": "esm",
@@ -45,7 +45,7 @@ describe("Ser/de Numbers", () => {
45
45
  // canSerde<u128>(u128.from("100"))
46
46
  // canSerde<u128>(u128.from("101"))
47
47
 
48
- canSerde<u128Safe>(u128Safe.from("0"))
48
+ /* canSerde<u128Safe>(u128Safe.from("0"))
49
49
  canSerde<u128Safe>(u128Safe.from("100"))
50
50
  canSerde<u128Safe>(u128Safe.from("101"))
51
51
 
@@ -66,7 +66,7 @@ describe("Ser/de Numbers", () => {
66
66
  canSerde<i128Safe>(i128Safe.from("101"))
67
67
  canSerde<i128Safe>(i128Safe.from("-100"))
68
68
  canSerde<i128Safe>(i128Safe.from("-101"))
69
-
69
+ */
70
70
  //canSerde<i256Safe>(new i256Safe(10, 11, 500, 501))
71
71
  });
72
72
 
@@ -102,7 +102,7 @@ describe("Ser/de Numbers", () => {
102
102
  });
103
103
 
104
104
  it("should ser/de BigInt objects", () => {
105
- canSerde<i32>(0);
105
+ /* canSerde<i32>(0);
106
106
 
107
107
  canSerde<u32>(100);
108
108
  canSerde<u64>(101);
@@ -113,7 +113,7 @@ describe("Ser/de Numbers", () => {
113
113
  canSerde<u128>(u128.from("101"))
114
114
  canSerde<u128>(u128.from("-100"))
115
115
  canSerde<u128>(u128.from("-101"))
116
-
116
+ */
117
117
  })
118
118
  });
119
119
 
@@ -175,6 +175,21 @@ describe("Ser/de Array", () => {
175
175
  ],
176
176
  ]);
177
177
  });
178
+
179
+ it("should ser/de object arrays", () => {
180
+ canSerde<Vec3[]>([{
181
+ x: 3.4,
182
+ y: 1.2,
183
+ z: 8.3
184
+ },
185
+ {
186
+ x: 3.4,
187
+ y: -2.1,
188
+ z: 9.3
189
+ }
190
+ ]);
191
+
192
+ })
178
193
  });
179
194
 
180
195
  describe("Ser/de Objects", () => {
@@ -401,6 +401,7 @@ function parseArray<T extends unknown[]>(data: string): T {
401
401
  // @ts-ignore
402
402
  return parseObjectArray<T>(data);
403
403
  }
404
+ return unreachable();
404
405
  }
405
406
  return unreachable();
406
407
  }
@@ -510,29 +511,24 @@ function parseArrayArray<T extends unknown[][]>(data: string): T {
510
511
  }
511
512
 
512
513
  // @ts-ignore
513
- @inline
514
- function parseObjectArray<T extends unknown[][]>(data: string): T {
514
+ export function parseObjectArray<T extends unknown[]>(data: string): T {
515
515
  const result = instantiate<T>();
516
516
  let char = 0;
517
- let lastPos = 1;
518
- let depth = 1;
519
- let i = 1;
520
- // Find start of bracket
521
- //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
522
- //i++;
523
- for (; i < data.length - 1; i++) {
524
- char = unsafeCharCodeAt(data, i);
517
+ let lastPos: u32 = 1;
518
+ let depth: u32 = 0;
519
+ for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
520
+ char = unsafeCharCodeAt(data, pos);
525
521
  if (char === leftBraceCode) {
526
- if (depth === 1) {
527
- lastPos = i;
522
+ if (depth === 0) {
523
+ lastPos = pos;
528
524
  }
529
- // Shifting is 6% faster than incrementing
530
- depth = depth << 1;
525
+ depth++;
531
526
  } else if (char === rightBraceCode) {
532
- depth = depth >> 1;
533
- if (depth === 1) {
534
- i++;
535
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
527
+ depth--;
528
+ if (depth === 0) {
529
+ pos++;
530
+ result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
531
+ //lastPos = pos + 2;
536
532
  }
537
533
  }
538
534
  }
@@ -58,4 +58,23 @@ export function escapeChar(char: string): string {
58
58
  case 0x0B: return "\\u000b";
59
59
  default: return char;
60
60
  }
61
+ }
62
+
63
+ /**
64
+ * A terrible function which finds the depth of a certain array.
65
+ * Suffers no overhead besides function calling and a if/else.
66
+ * @returns depth of array
67
+ */
68
+ export function getArrayDepth<T>(depth: i32 = 1): i32 {
69
+ // @ts-ignore
70
+ if (isArray<T>()) {
71
+ return 0;
72
+ // @ts-ignore
73
+ } else if (isArray<valueof<T>>()) {
74
+ depth++;
75
+ // @ts-ignore
76
+ return getArrayDepth<valueof<T>>(depth);
77
+ } else {
78
+ return depth;
79
+ }
61
80
  }
package/assembly/test.ts CHANGED
@@ -3,6 +3,31 @@ import {
3
3
  JSON
4
4
  } from ".";
5
5
 
6
+ @json
7
+ class Candle {
8
+ timestamp!: i64;
9
+ high!: f64;
10
+ low!: f64;
11
+ open!: f64;
12
+ close!: f64;
13
+ constructor(timestamp: i64, high: f64, low: f64, open: f64, close: f64) {
14
+ this.timestamp = timestamp;
15
+ this.high = high;
16
+ this.low = low;
17
+ this.open = open;
18
+ this.close = close;
19
+ }
20
+ }
21
+
22
+ //console.log(JSON.stringify(candle));
23
+
24
+ const parsedCandleArray: Candle[] = JSON.parse<Candle[]>(`[{"timestamp":1620248400000,"high":573.3476371851107,"low":572.7653897862947,"close":572.7653897862947,"open":573.3476371851107},{"timestamp":1620249300000,"high":572.4966600947654,"low":572.3690847959957,"close":572.3690847959957,"open":572.4966600947654},{"timestamp":1620250200000,"high":572.2886600465616,"low":571.757999416375,"close":571.757999416375,"open":572.2886600465616},{"timestamp":1620251100000,"high":571.745549588537,"low":571.4831524457645,"close":571.4831524457645,"open":571.745549588537},{"timestamp":1620252000000,"high":571.4291070205617,"low":571.423771655803,"close":571.423771655803,"open":571.4291070205617},{"timestamp":1620252900000,"high":571.2682040427187,"low":570.0124454515138,"close":571.2543224050862,"open":571.2682040427187},{"timestamp":1620253800000,"high":571.950687856295,"low":571.950687856295,"close":571.950687856295,"open":571.950687856295},{"timestamp":1620254700000,"high":574.32247048919,"low":572.229619806987,"close":574.32247048919,"open":572.229619806987},{"timestamp":1620255600000,"high":573.7949788294341,"low":573.7949788294341,"close":573.7949788294341,"open":573.7949788294341},{"timestamp":1620256500000,"high":573.6703031128156,"low":573.6703031128156,"close":573.6703031128156,"open":573.6703031128156},{"timestamp":1620261000000,"high":573.4922654231464,"low":573.4922654231464,"close":573.4922654231464,"open":573.4922654231464},{"timestamp":1620261900000,"high":573.2630790607755,"low":572.1510958054668,"close":572.1510958054668,"open":573.2630790607755},{"timestamp":1620262800000,"high":570.381489136757,"low":570.381489136757,"close":570.381489136757,"open":570.381489136757},{"timestamp":1620264600000,"high":570.2742985701099,"low":570.0803689046074,"close":570.0803689046074,"open":570.2742985701099},{"timestamp":1620265500000,"high":570.1422401263446,"low":570.1422401263446,"close":570.1422401263446,"open":570.1422401263446},{"timestamp":1620266400000,"high":572.8052512272116,"low":572.8052201233321,"close":572.8052512272116,"open":572.8052201233321},{"timestamp":1620267300000,"high":572.7730922927839,"low":572.7447581107978,"close":572.7447581107978,"open":572.7730922927839},{"timestamp":1620268200000,"high":572.7272428903487,"low":572.7258230151764,"close":572.7258230151764,"open":572.7272428903487},{"timestamp":1620270900000,"high":572.2594215468664,"low":571.8195591664584,"close":571.8195591664584,"open":572.2578671930029},{"timestamp":1620271800000,"high":571.2809499888094,"low":571.2808568072832,"close":571.2808568072832,"open":571.2809499888094},{"timestamp":1620272700000,"high":571.1742152842157,"low":571.1742152842157,"close":571.1742152842157,"open":571.1742152842157},{"timestamp":1620273600000,"high":571.1716102454611,"low":571.0288842515337,"close":571.0288842515337,"open":571.0642228722581},{"timestamp":1620275400000,"high":571.0203010949516,"low":571.0203010949516,"close":571.0203010949516,"open":571.0203010949516},{"timestamp":1620277200000,"high":570.261632018576,"low":570.261632018576,"close":570.261632018576,"open":570.261632018576},{"timestamp":1620279000000,"high":570.1348876529629,"low":570.1348876529629,"close":570.1348876529629,"open":570.1348876529629},{"timestamp":1620279900000,"high":570.199730991694,"low":570.199730991694,"close":570.199730991694,"open":570.199730991694},{"timestamp":1620282600000,"high":568.4408310147167,"low":568.4408310147167,"close":568.4408310147167,"open":568.4408310147167},{"timestamp":1620284400000,"high":567.8217236373331,"low":567.5896453383704,"close":567.5896453383704,"open":567.8217236373331},{"timestamp":1620288000000,"high":568.652510360092,"low":568.652510360092,"close":568.652510360092,"open":568.652510360092},{"timestamp":1620288900000,"high":569.0169039594798,"low":569.0169039594798,"close":569.0169039594798,"open":569.0169039594798},{"timestamp":1620289800000,"high":569.1372681718557,"low":569.1372681718557,"close":569.1372681718557,"open":569.1372681718557},{"timestamp":1620293400000,"high":569.1425890413165,"low":569.1425890413165,"close":569.1425890413165,"open":569.1425890413165},{"timestamp":1620294300000,"high":571.4603226652566,"low":569.668539984208,"close":571.3510882379371,"open":569.668539984208},{"timestamp":1620295200000,"high":575.0297756308146,"low":565.6188600247405,"close":575.0297756308146,"open":571.2768914562338},{"timestamp":1620296100000,"high":579.5414875855951,"low":578.0875180733055,"close":579.5414875855951,"open":578.0875180733055},{"timestamp":1620297000000,"high":580.4591998073045,"low":580.4591998073045,"close":580.4591998073045,"open":580.4591998073045},{"timestamp":1620299700000,"high":580.462401844569,"low":580.462401844569,"close":580.462401844569,"open":580.462401844569},{"timestamp":1620303300000,"high":580.450191330308,"low":580.450191330308,"close":580.450191330308,"open":580.450191330308},{"timestamp":1620305100000,"high":580.4060918534583,"low":580.4060918534583,"close":580.4060918534583,"open":580.4060918534583},{"timestamp":1620306000000,"high":580.2988479516713,"low":580.2988479516713,"close":580.2988479516713,"open":580.2988479516713},{"timestamp":1620306900000,"high":578.5227831917862,"low":578.5121109550881,"close":578.5121109550881,"open":578.5227831917862},{"timestamp":1620307800000,"high":578.3521310367136,"low":578.224208690017,"close":578.224208690017,"open":578.3521310367136},{"timestamp":1620308700000,"high":578.1781390176766,"low":578.1781390176766,"close":578.1781390176766,"open":578.1781390176766},{"timestamp":1620309600000,"high":574.5402154361069,"low":574.2966785397088,"close":574.5402154361069,"open":574.2966785397088},{"timestamp":1620310500000,"high":574.6374782052543,"low":574.3595582585991,"close":574.6374782052543,"open":574.3595582585991},{"timestamp":1620311400000,"high":575.0993626799352,"low":575.0993626799352,"close":575.0993626799352,"open":575.0993626799352},{"timestamp":1620313200000,"high":573.7701312728145,"low":573.7701312728145,"close":573.7701312728145,"open":573.7701312728145},{"timestamp":1620314100000,"high":573.3906631152785,"low":573.3906631152785,"close":573.3906631152785,"open":573.3906631152785},{"timestamp":1620315000000,"high":573.4334610811361,"low":573.4334610811361,"close":573.4334610811361,"open":573.4334610811361},{"timestamp":1620315900000,"high":571.6595888644223,"low":571.6595888644223,"close":571.6595888644223,"open":571.6595888644223},{"timestamp":1620316800000,"high":571.7534533028926,"low":571.7534533028926,"close":571.7534533028926,"open":571.7534533028926},{"timestamp":1620317700000,"high":577.9623648979384,"low":574.8537177533384,"close":576.1674529591473,"open":574.8537177533384},{"timestamp":1620318600000,"high":576.1563193117207,"low":576.0620257439493,"close":576.0620257439493,"open":576.1563193117207},{"timestamp":1620319500000,"high":574.275952158709,"low":571.177013121888,"close":571.177013121888,"open":574.275952158709},{"timestamp":1620321300000,"high":571.2079586608608,"low":571.2079586608608,"close":571.2079586608608,"open":571.2079586608608},{"timestamp":1620323100000,"high":571.324057673865,"low":571.324057673865,"close":571.324057673865,"open":571.324057673865},{"timestamp":1620324000000,"high":571.1253547573914,"low":570.0875973567182,"close":570.0875973567182,"open":571.1253547573914},{"timestamp":1620324900000,"high":570.0805475832676,"low":570.0805475832676,"close":570.0805475832676,"open":570.0805475832676},{"timestamp":1620325800000,"high":570.0257972684403,"low":559.1035539331212,"close":559.1035539331212,"open":570.0257972684403},{"timestamp":1620328500000,"high":557.5649037694549,"low":556.0279702412793,"close":556.0279702412793,"open":557.5649037694549},{"timestamp":1620329400000,"high":556.6968218685071,"low":556.6968218685071,"close":556.6968218685071,"open":556.6968218685071},{"timestamp":1620330300000,"high":560.7228138291837,"low":558.2216468935494,"close":560.7228138291837,"open":558.2216468935494},{"timestamp":1620332100000,"high":560.5876666979001,"low":560.5876666979001,"close":560.5876666979001,"open":560.5876666979001},{"timestamp":1620333000000,"high":560.5186489036859,"low":559.9302964393763,"close":559.9302964393763,"open":560.5186489036859},{"timestamp":1620335700000,"high":560.8859962694225,"low":560.8859962694225,"close":560.8859962694225,"open":560.8859962694225},{"timestamp":1620337500000,"high":562.6549059693484,"low":561.1228262312293,"close":562.6549059693484,"open":561.1228262312293},{"timestamp":1620338400000,"high":562.5749239504988,"low":562.5749239504988,"close":562.5749239504988,"open":562.5749239504988},{"timestamp":1620340200000,"high":564.1086094674863,"low":564.1086094674863,"close":564.1086094674863,"open":564.1086094674863},{"timestamp":1620341100000,"high":564.1156752788603,"low":564.1156752788603,"close":564.1156752788603,"open":564.1156752788603},{"timestamp":1620342000000,"high":563.8686326269438,"low":563.8686326269438,"close":563.8686326269438,"open":563.8686326269438},{"timestamp":1620343800000,"high":565.4032072056408,"low":565.4032072056408,"close":565.4032072056408,"open":565.4032072056408},{"timestamp":1620344700000,"high":564.9161906724804,"low":564.846451581273,"close":564.846451581273,"open":564.9161906724804},{"timestamp":1620345600000,"high":565.3129449522828,"low":563.3005556458523,"close":565.3129449522828,"open":563.3005556458523},{"timestamp":1620346500000,"high":566.8491299730733,"low":566.7792504378252,"close":566.7792504378252,"open":566.8491299730733},{"timestamp":1620347400000,"high":567.6311377719769,"low":566.8226586070456,"close":567.6311377719769,"open":566.8226586070456},{"timestamp":1620349200000,"high":568.393145669603,"low":562.3084190409248,"close":562.3084190409248,"open":568.393145669603},{"timestamp":1620350100000,"high":563.0673266288399,"low":563.0673266288399,"close":563.0673266288399,"open":563.0673266288399},{"timestamp":1620351000000,"high":563.0460469915702,"low":563.0460469915702,"close":563.0460469915702,"open":563.0460469915702},{"timestamp":1620351900000,"high":562.6940165863563,"low":562.6940165863563,"close":562.6940165863563,"open":562.6940165863563},{"timestamp":1620352800000,"high":562.2290121119479,"low":562.2290121119479,"close":562.2290121119479,"open":562.2290121119479},{"timestamp":1620354600000,"high":560.6865799646575,"low":559.1464640169283,"close":559.1464640169283,"open":560.6865799646575},{"timestamp":1620356400000,"high":558.6737643409452,"low":558.5032601234623,"close":558.5032601234623,"open":558.6737643409452},{"timestamp":1620358200000,"high":557.7252518300813,"low":557.7252518300813,"close":557.7252518300813,"open":557.7252518300813},{"timestamp":1620359100000,"high":559.452591197762,"low":559.452591197762,"close":559.452591197762,"open":559.452591197762},{"timestamp":1620360000000,"high":557.9238732313372,"low":551.1413593852167,"close":554.1625302396624,"open":557.9238732313372},{"timestamp":1620360900000,"high":555.7240520136215,"low":555.674531175535,"close":555.7240520136215,"open":555.674531175535},{"timestamp":1620361800000,"high":554.0781517227211,"low":554.0781517227211,"close":554.0781517227211,"open":554.0781517227211},{"timestamp":1620362700000,"high":555.9093975255644,"low":554.0935763098537,"close":555.9093975255644,"open":554.0935763098537},{"timestamp":1620363600000,"high":556.7596063111193,"low":556.0023730597429,"close":556.7596063111193,"open":556.0023730597429},{"timestamp":1620365400000,"high":558.3270073972132,"low":556.8114287909416,"close":558.3270073972132,"open":556.8114287909416},{"timestamp":1620367200000,"high":558.7824867555943,"low":558.7824867555943,"close":558.7824867555943,"open":558.7824867555943},{"timestamp":1620369000000,"high":562.0179021454524,"low":560.3025781550792,"close":562.0179021454524,"open":560.3025781550792},{"timestamp":1620369900000,"high":562.0080817771758,"low":562.0080817771758,"close":562.0080817771758,"open":562.0080817771758},{"timestamp":1620373500000,"high":560.4783187009747,"low":560.4783187009747,"close":560.4783187009747,"open":560.4783187009747},{"timestamp":1620374400000,"high":558.9505059072079,"low":558.9505059072079,"close":558.9505059072079,"open":558.9505059072079},{"timestamp":1620375300000,"high":558.7316785819487,"low":558.7315744378288,"close":558.7315744378288,"open":558.7316785819487},{"timestamp":1620376200000,"high":559.2700965276954,"low":558.7201382932576,"close":558.7201382932576,"open":559.2700965276954},{"timestamp":1620377100000,"high":559.7884860040957,"low":558.2725955125745,"close":559.7884860040957,"open":558.3870242987017},{"timestamp":1620378000000,"high":561.2454700974902,"low":559.7277145674427,"close":561.0931142383333,"open":559.7780560002528},{"timestamp":1620379800000,"high":562.5589149792357,"low":561.0392143359311,"close":562.5589149792357,"open":561.0392143359311},{"timestamp":1620381600000,"high":564.825992475405,"low":564.0806119374416,"close":564.825992475405,"open":564.0806119374416},{"timestamp":1620382500000,"high":564.8106789982139,"low":564.8003967441988,"close":564.8003967441988,"open":564.8106789982139},{"timestamp":1620383400000,"high":565.0122815398387,"low":565.0122815398387,"close":565.0122815398387,"open":565.0122815398387},{"timestamp":1620384300000,"high":565.0122384439766,"low":565.0122384439766,"close":565.0122384439766,"open":565.0122384439766},{"timestamp":1620385200000,"high":564.9947583554654,"low":564.9947583554654,"close":564.9947583554654,"open":564.9947583554654},{"timestamp":1620386100000,"high":564.7315180958523,"low":564.726251367374,"close":564.726251367374,"open":564.7315180958523},{"timestamp":1620387000000,"high":566.1025337740446,"low":564.5781421341958,"close":566.1025337740446,"open":564.5781421341958}]`);
25
+
26
+ for (let i = 0; i < parsedCandleArray.length; i++) {
27
+ const chunk = parsedCandleArray[i];
28
+ console.log(JSON.stringify(chunk));
29
+ }
30
+ /*
6
31
  // @ts-ignore
7
32
  @JSON
8
33
  class Vec3 {
@@ -48,26 +73,4 @@ const player: Player = {
48
73
  const serializedPlayer = JSON.stringify<Player>(player);
49
74
  console.log("Serialized Player: " + serializedPlayer);
50
75
  const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
51
- console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));/*
52
-
53
- @json
54
- class Vec2 {
55
- x: f32;
56
- y: f32;
57
- }
58
-
59
- const vec: Vec2 = {
60
- x: 0.0,
61
- y: 0.0,
62
- }
63
-
64
- let i = 5_000_000;
65
- while (i--) {
66
- JSON.stringify<Vec2>(vec);
67
- }
68
- let start = Date.now();
69
- i = 10_000_000;
70
- while (i--) {
71
- JSON.stringify<Vec2>(vec);
72
- }
73
- console.log(`Took: ${Date.now() - start}ms`)*/
76
+ console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));*/
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.16",
3
+ "version": "0.5.18",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
7
- "contributors": [
8
- "DogWhich",
9
- "Joshua Tenner",
10
- "Rom"
11
- ],
7
+ "contributors": [
8
+ "DogWhich",
9
+ "Joshua Tenner",
10
+ "Rom"
11
+ ],
12
12
  "license": "MIT",
13
13
  "scripts": {
14
14
  "aspect": "asp",
@@ -23,18 +23,18 @@
23
23
  "devDependencies": {
24
24
  "@as-pect/cli": "^8.0.1",
25
25
  "@as-tral/cli": "^2.0.0",
26
- "@assemblyscript/loader": "^0.25.0",
26
+ "@assemblyscript/loader": "^0.27.0",
27
27
  "@assemblyscript/wasi-shim": "^0.1.0",
28
28
  "as-bignum": "^0.2.23",
29
- "assemblyscript": "^0.25.0",
30
- "assemblyscript-prettier": "^1.0.6",
31
- "prettier": "^2.8.1",
32
- "typescript": "^4.9.4",
29
+ "assemblyscript": "^0.27.0",
30
+ "assemblyscript-prettier": "^1.0.7",
31
+ "prettier": "^2.8.3",
32
+ "typescript": "^4.9.5",
33
33
  "visitor-as": "^0.11.4"
34
34
  },
35
35
  "dependencies": {
36
36
  "as-string-sink": "^0.5.0",
37
- "as-variant": "^0.4.0"
37
+ "as-variant": "^0.4.1"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.16",
3
+ "version": "0.5.18",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",