json-as 0.9.10 → 0.9.11

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.
@@ -0,0 +1 @@
1
+ github: [JairusSW]
package/CHANGELOG CHANGED
@@ -17,7 +17,8 @@ v0.9.8a - Fix #80 - Empty Maps were not serialized to {}, instead, threw memory
17
17
  v0.9.8b - Fix #81 - Revert transform
18
18
  v0.9.9 - Fix #82 - Initialize maps
19
19
  v0.9.9a - Remove extraneous logs from transform
20
- v0.9.10 - Fix transform type checks. switch to nodekind checks
20
+ v0.9.11 - Fix transform type checks. switch to nodekind checks
21
+ v0.9.11 - Remove MpZ--implement custom serializers and deserializers in the works
21
22
 
22
23
  [UNRELEASED] v1.0.0
23
24
  - Allow nullable primitives
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.10
6
+ v0.9.11
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -0,0 +1,24 @@
1
+ {
2
+ "input": [
3
+ "./assembly/__tests__/*.spec.ts"
4
+ ],
5
+ "outDir": "./build",
6
+ "config": "./asconfig.json",
7
+ "suites": [],
8
+ "coverage": {
9
+ "enabled": true,
10
+ "show": false
11
+ },
12
+ "buildOptions": {
13
+ "args": [],
14
+ "wasi": true,
15
+ "parallel": true,
16
+ "verbose": true
17
+ },
18
+ "runOptions": {
19
+ "runtime": {
20
+ "name": "wasmtime",
21
+ "run": "wasmtime <file>"
22
+ }
23
+ }
24
+ }
@@ -5,7 +5,6 @@ import {
5
5
  run
6
6
  } from "as-test/assembly";
7
7
  import { DerivedObject, Null, ObjWithStrangeKey, ObjectWithFloat, OmitIf, Player, Vec3 } from "./types";
8
- import { MpZ } from "@hypercubed/as-mpz";
9
8
 
10
9
  describe("Should serialize strings", () => {
11
10
 
@@ -51,18 +50,6 @@ describe("Should serialize integers", () => {
51
50
 
52
51
  });
53
52
 
54
- describe("Should serialize MpZ (Big Int)", () => {
55
-
56
- expect(
57
- JSON.stringify<MpZ>(MpZ.from(0))
58
- ).toBe("0");
59
-
60
- expect(
61
- JSON.stringify<MpZ>(MpZ.from(2).pow(512))
62
- ).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
63
-
64
- });
65
-
66
53
  describe("Should serialize floats", () => {
67
54
 
68
55
  expect(
@@ -370,18 +357,6 @@ describe("Should deserialize integers", () => {
370
357
 
371
358
  });
372
359
 
373
- describe("Should deserialize MpZ (Big Int)", () => {
374
-
375
- expect(
376
- JSON.parse<MpZ>("0").toString()
377
- ).toBe("0");
378
-
379
- expect(
380
- JSON.parse<MpZ>("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096").toString()
381
- ).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
382
-
383
- });
384
-
385
360
  describe("Should deserialize floats", () => {
386
361
 
387
362
  expect(
@@ -31,8 +31,8 @@ export function deserializeArray<T extends unknown[]>(data: string): T {
31
31
  if (isDefined(type.__DESERIALIZE)) {
32
32
  return deserializeObjectArray<T>(data);
33
33
  }
34
- ERROR("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
34
+ throw new Error("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
35
35
  } else {
36
- ERROR("Could not parse array of type " + nameof<T>() + "!");
36
+ throw new Error("Could not parse array of type " + nameof<T>() + "!");
37
37
  }
38
38
  }
package/assembly/index.ts CHANGED
@@ -13,198 +13,14 @@ import { deserializeFloat } from "./deserialize/float";
13
13
  import { deserializeObject } from "./deserialize/object";
14
14
  import { deserializeMap } from "./deserialize/map";
15
15
  import { deserializeDate } from "./deserialize/date";
16
- import { FALSE_WORD, NULL_WORD, TRUE_WORD } from "./chars";
16
+ import { NULL_WORD } from "./chars";
17
17
  import { deserializeInteger } from "./deserialize/integer";
18
18
  import { deserializeString } from "./deserialize/string";
19
- import { Sink } from "./sink";
20
- import { Variant } from "as-variant/assembly";
21
- import { MpZ } from "@hypercubed/as-mpz";
22
- import { serializeMpZ } from "./serialize/mpz";
23
- import { deserializeMpZ } from "./deserialize/mpz";
24
-
25
- /**
26
- * Offset of the 'storage' property in the JSON.Value class.
27
- */
28
- // @ts-ignore: Decorator valid here
29
- const STORAGE = offsetof<JSON.Value>("storage");
30
19
 
31
20
  /**
32
21
  * JSON Encoder/Decoder for AssemblyScript
33
22
  */
34
23
  export namespace JSON {
35
- /**
36
- * Enum representing the different types supported by JSON.
37
- */
38
- export enum Types {
39
- U8,
40
- U16,
41
- U32,
42
- U64,
43
- F32,
44
- F64,
45
- Bool,
46
- String,
47
- ManagedString,
48
- Struct,
49
- ManagedStruct,
50
- Array,
51
- ManagedArray
52
- }
53
-
54
- export class Value {
55
- public type: i32;
56
- // @ts-ignore: storage is set directly through memory
57
- private storage: u64;
58
-
59
- private constructor() { unreachable(); }
60
-
61
- /**
62
- * Creates an JSON.Value instance from a given value.
63
- * @param value - The value to be encapsulated.
64
- * @returns An instance of JSON.Value.
65
- */
66
- static from<T>(value: T): JSON.Value {
67
- if (value instanceof Variant) {
68
- // Handle
69
- } else if (value instanceof JSON.Value) {
70
- return value;
71
- }
72
- const out = changetype<JSON.Value>(__new(offsetof<JSON.Value>(), idof<JSON.Value>()));
73
- out.set<T>(value);
74
- return out;
75
- }
76
-
77
- /**
78
- * Sets the value of the JSON.Value instance.
79
- * @param value - The value to be set.
80
- */
81
- set<T>(value: T): void {
82
- if (isBoolean<T>()) {
83
- this.type = JSON.Types.Bool;
84
- store<T>(changetype<usize>(this), value, STORAGE);
85
- } else if (value instanceof u8 || value instanceof i8) {
86
- this.type = JSON.Types.U8;
87
- store<T>(changetype<usize>(this), value, STORAGE);
88
- } else if (value instanceof u16 || value instanceof i16) {
89
- this.type = JSON.Types.U16;
90
- store<T>(changetype<usize>(this), value, STORAGE);
91
- } else if (value instanceof u32 || value instanceof i32) {
92
- this.type = JSON.Types.U32;
93
- store<T>(changetype<usize>(this), value, STORAGE);
94
- } else if (value instanceof u64 || value instanceof i64) {
95
- this.type = JSON.Types.U64;
96
- store<T>(changetype<usize>(this), value, STORAGE);
97
- } else if (value instanceof f32) {
98
- this.type = JSON.Types.F64;
99
- store<T>(changetype<usize>(this), value, STORAGE);
100
- } else if (value instanceof f64) {
101
- this.type = JSON.Types.F64;
102
- store<T>(changetype<usize>(this), value, STORAGE);
103
- } else if (isString<T>()) {
104
- this.type = JSON.Types.String;
105
- store<T>(changetype<usize>(this), value, STORAGE);
106
- } else if (value instanceof Map) {
107
- if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
108
- throw new Error("Maps must be of type Map<string, JSON.Value>!");
109
- }
110
- this.type = JSON.Types.Struct;
111
- store<T>(changetype<usize>(this), value, STORAGE);
112
- // @ts-ignore: __SERIALIZE is implemented by the transform
113
- } else if (isDefined(value.__SERIALIZE)) {
114
- this.type = JSON.Types.Struct;
115
- store<T>(changetype<usize>(this), value, STORAGE);
116
- } else if (isArray<T>()) {
117
- // @ts-ignore: T satisfies constraints of any[]
118
- this.type = JSON.Types.Array + getArrayDepth<T>(0);
119
- store<T>(changetype<usize>(this), value, STORAGE);
120
- }
121
- }
122
-
123
- /**
124
- * Gets the value of the JSON.Value instance.
125
- * @returns The encapsulated value.
126
- */
127
- unwrap<T>(): T {
128
- if (isManaged<T>()) {
129
- if (this.type !== JSON.Types.Struct) throw new Error("Type mismatch");
130
- if (idof<T>() !== load<u32>(changetype<usize>(this.storage), -8)) throw new Error("Type mismatch");
131
- }
132
- return load<T>(changetype<usize>(this), STORAGE);
133
- }
134
-
135
- /**
136
- * Gets the value of the JSON.Value instance.
137
- * @returns The encapsulated value.
138
- */
139
- unwrapUnsafe<T>(): T {
140
- return load<T>(changetype<usize>(this), STORAGE);
141
- }
142
-
143
- /**
144
- * Gets the value of the JSON.Value instance.
145
- * @returns The encapsulated value.
146
- */
147
- get<T>(): T {
148
- return load<T>(changetype<usize>(this), STORAGE);
149
- }
150
-
151
- /**
152
- * Gets the value of the JSON.Value instance.
153
- * @returns The encapsulated value.
154
- */
155
- getUnsafe<T>(): T {
156
- return load<T>(changetype<usize>(this), STORAGE);
157
- }
158
-
159
- /**
160
- * Gets the value of the JSON.Value instance.
161
- * @returns The encapsulated value.
162
- */
163
- is<T>(): T {
164
- return load<T>(changetype<usize>(this), STORAGE);
165
- }
166
-
167
- /**
168
- * Gets the value of the JSON.Value instance.
169
- * @returns The encapsulated value.
170
- */
171
- clone<T>(): T {
172
- return load<T>(changetype<usize>(this), STORAGE);
173
- }
174
-
175
- /**
176
- * Converts the JSON.Value to a string representation.
177
- * @param useString - If true, treats Buffer as a string.
178
- * @returns The string representation of the JSON.Value.
179
- */
180
- toString(useString: boolean = false): string {
181
- switch (this.type) {
182
- case JSON.Types.U8: return this.get<u8>().toString();
183
- case JSON.Types.U16: return this.get<u16>().toString();
184
- case JSON.Types.U32: return this.get<u32>().toString();
185
- case JSON.Types.U64: return this.get<u64>().toString();
186
- case JSON.Types.String: return "\"" + this.get<string>() + "\"";
187
- case JSON.Types.Bool: return this.get<boolean>() ? TRUE_WORD : FALSE_WORD;
188
- default: {
189
- const arr = this.get<JSON.Value[]>();
190
- if (!arr.length) return "[]";
191
- const out = Sink.fromString("[");
192
- for (let i = 0; i < arr.length - 1; i++) {
193
- const element = unchecked(arr[i]);
194
- out.write(element.toString(useString));
195
- out.write(",");
196
- }
197
-
198
- const element = unchecked(arr[arr.length - 1]);
199
- out.write(element.toString(useString));
200
-
201
- out.write("]");
202
- return out.toString();
203
- }
204
- }
205
- }
206
- }
207
-
208
24
  export class Box<T> {
209
25
  constructor(public value: T) {}
210
26
  @inline static from<T>(value: T): Box<T> {
@@ -236,6 +52,7 @@ export namespace JSON {
236
52
  // @ts-ignore
237
53
  } else if (isString<nonnull<T>>()) {
238
54
  return serializeString(changetype<string>(data));
55
+ // @ts-ignore: Supplied by trasnform
239
56
  } else if (isDefined(data.__SERIALIZE)) {
240
57
  // @ts-ignore
241
58
  return serializeObject(changetype<nonnull<T>>(data));
@@ -248,8 +65,6 @@ export namespace JSON {
248
65
  } else if (data instanceof Map) {
249
66
  // @ts-ignore
250
67
  return serializeMap(changetype<nonnull<T>>(data));
251
- } else if (data instanceof MpZ) {
252
- return serializeMpZ(data);
253
68
  } else {
254
69
  throw new Error(
255
70
  `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
@@ -284,16 +99,14 @@ export namespace JSON {
284
99
  return deserializeArray<nonnull<T>>(data);
285
100
  }
286
101
  let type: nonnull<T> = changetype<nonnull<T>>(0);
102
+ // @ts-ignore: Defined by trasnform
287
103
  if (isDefined(type.__DESERIALIZE)) {
288
104
  // @ts-ignore
289
105
  return deserializeObject<nonnull<T>>(data.trimStart());
290
106
  } else if (type instanceof Map) {
291
107
  // @ts-ignore
292
108
  return deserializeMap<nonnull<T>>(data.trimStart());
293
- } else if (type instanceof MpZ) {
294
- // @ts-ignore
295
- return deserializeMpZ(data);
296
- }else if (type instanceof Date) {
109
+ } else if (type instanceof Date) {
297
110
  // @ts-ignore
298
111
  return deserializeDate(data);
299
112
  } else {
@@ -12,9 +12,7 @@ import { serializeString } from "./string";
12
12
 
13
13
  // @ts-ignore: Decorator valid here
14
14
  @inline export function serializeArray<T extends any[]>(data: T): string {
15
- if (changetype<usize>(data) == <usize>0) return EMPTY_BRACKET_WORD;
16
- // @ts-ignore
17
- else if (data.length == 0) {
15
+ if (data.length == 0) {
18
16
  return EMPTY_BRACKET_WORD;
19
17
  // @ts-ignore
20
18
  } else if (isString<valueof<T>>()) {
@@ -4,7 +4,6 @@ import { Sink } from "../sink";
4
4
 
5
5
  // @ts-ignore: Decorator valid here
6
6
  @inline export function serializeMap<T extends Map<any, any>>(data: T): string {
7
- if (changetype<usize>(data) == <usize>0) return "{}";
8
7
  let result = Sink.fromString(BRACE_LEFT_WORD);
9
8
  if (!data.size) return "{}";
10
9
  let keys = data.keys();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.9.10",
3
+ "version": "0.9.11",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -15,9 +15,9 @@
15
15
  ],
16
16
  "license": "MIT",
17
17
  "scripts": {
18
- "test": "wasmtime build/test.spec.wasm",
19
- "pretest": "asc assembly/__tests__/test.spec.ts --target test",
20
- "build:test": "JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm",
18
+ "test": "ast test",
19
+ "pretest": "rm -rf ./build/ && ast build",
20
+ "build:test": "rm -rf ./build/ && JSON_DEBUG=true asc assembly/test.ts --transform ./transform -o ./build/test.wasm",
21
21
  "build:bench": "asc bench/benchmark.ts -o bench/benchmark.wasm --transform ./transform --optimizeLevel 3 --shrinkLevel 0 --converge --noAssert --uncheckedBehavior always --runtime stub",
22
22
  "bench:wasmtime": "wasmtime ./bench/benchmark.wasm",
23
23
  "bench:wasmer": "wasmer --llvm ./bench/benchmark.wasm",
@@ -32,6 +32,7 @@
32
32
  "@assemblyscript/wasi-shim": "^0.1.0",
33
33
  "as-bench": "^0.0.0-alpha",
34
34
  "as-console": "^7.0.0",
35
+ "as-test": "0.1.7",
35
36
  "assemblyscript": "^0.27.28",
36
37
  "assemblyscript-prettier": "^3.0.1",
37
38
  "benchmark": "^2.1.4",
@@ -41,12 +42,7 @@
41
42
  "typescript": "^5.5.3",
42
43
  "visitor-as": "^0.11.4"
43
44
  },
44
- "dependencies": {
45
- "@hypercubed/as-mpz": "^2.2.0",
46
- "as-string-sink": "^0.5.3",
47
- "as-test": "^0.1.4",
48
- "as-virtual": "^0.2.0"
49
- },
45
+ "dependencies": { "as-virtual": "^0.2.0" },
50
46
  "overrides": {
51
47
  "assemblyscript": "$assemblyscript"
52
48
  },
@@ -60,7 +56,11 @@
60
56
  "serialize",
61
57
  "deserialize",
62
58
  "dynamic",
63
- "serde"
59
+ "serde",
60
+ "SIMD",
61
+ "optimized",
62
+ "fast",
63
+ "algorithm"
64
64
  ],
65
65
  "bugs": {
66
66
  "url": "https://github.com/JairusSW/as-json/issues"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.9.10",
3
+ "version": "0.9.11",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -12,9 +12,7 @@
12
12
  "lekiano"
13
13
  ],
14
14
  "license": "MIT",
15
- "devDependencies": {
16
- "assemblyscript": "^0.27.1"
17
- },
15
+ "devDependencies": {},
18
16
  "dependencies": {},
19
17
  "repository": {
20
18
  "type": "git",
@@ -13,6 +13,7 @@ import { toString, isStdlib } from "visitor-as/dist/utils.js";
13
13
  import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
14
14
  import { Transform } from "assemblyscript/dist/transform.js";
15
15
  import { CommonFlags } from "types:assemblyscript/src/common";
16
+ import { DecoratorNode } from "types:assemblyscript/src/ast";
16
17
 
17
18
  class JSONTransform extends BaseVisitor {
18
19
  public schemasList: SchemaData[] = [];
@@ -1,12 +0,0 @@
1
- import { MpZ } from "@hypercubed/as-mpz";
2
-
3
- /**
4
- * Deserialize a string to type MpZ
5
- * @param data data to parse
6
- * @returns MpZ
7
- */
8
- // @ts-ignore: Decorator valid here
9
- @inline export function deserializeMpZ(data: string, start: i32 = 0, end: i32 = 0): MpZ {
10
- if (!end) end = data.length;
11
- return MpZ.from(data.slice(start, end));
12
- }
@@ -1,6 +0,0 @@
1
- import { MpZ } from "@hypercubed/as-mpz";
2
-
3
- // @ts-ignore: Decorator valid here
4
- @inline export function serializeMpZ(data: MpZ): string {
5
- return data.toString();
6
- }
Binary file