json-as 0.9.14 → 0.9.16

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/CHANGELOG CHANGED
@@ -22,6 +22,8 @@ v0.9.11 - Remove MpZ--implement custom serializers and deserializers in the work
22
22
  v0.9.12 - Add compat with aspect
23
23
  v0.9.13 - Fix empty strings not indexing correctly
24
24
  v0.9.14 - Ignore properties of type Function
25
+ v0.9.15 - Support JSON.Raw blocks
26
+ v0.9.16 - JSON.Raw should be completely untouched
25
27
 
26
28
  [UNRELEASED] v1.0.0
27
29
  - Allow nullable primitives
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.14
6
+ v0.9.16
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -3,17 +3,13 @@
3
3
  "./assembly/__tests__/*.spec.ts"
4
4
  ],
5
5
  "outDir": "./build",
6
- "config": "./asconfig.json",
7
- "suites": [],
8
- "coverage": {
9
- "enabled": true,
10
- "show": false
6
+ "config": "none",
7
+ "plugins": {
8
+ "coverage": false
11
9
  },
12
10
  "buildOptions": {
13
11
  "args": [],
14
- "wasi": true,
15
- "parallel": true,
16
- "verbose": true
12
+ "target": "wasi"
17
13
  },
18
14
  "runOptions": {
19
15
  "runtime": {
@@ -1,4 +1,4 @@
1
- import { JSON } from "..";
1
+ import { JSON } from "json-as";
2
2
  import {
3
3
  describe,
4
4
  expect,
@@ -33,4 +33,4 @@ declare function omitnull(): Function;
33
33
  * Property decorator that allows a field to be flattened.
34
34
  * @param fieldName - Points to the field to flatten. Can use dot-notation here like @omit("foo.identifier.text")
35
35
  */
36
- declare function flatten(fieldName: string = "value"): Function;
36
+ declare function flatten(fieldName: string = "value"): Function;
package/assembly/index.ts CHANGED
@@ -13,21 +13,151 @@ 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 { NULL_WORD } from "./custom/chars";
16
+ import { BRACKET_LEFT, NULL_WORD } from "./custom/chars";
17
17
  import { deserializeInteger } from "./deserialize/integer";
18
18
  import { deserializeString } from "./deserialize/string";
19
+ import { Sink } from "./custom/sink";
20
+ import { bs } from "./custom/bs";
21
+
22
+ /**
23
+ * Offset of the 'storage' property in the JSON.Value class.
24
+ */
25
+ // @ts-ignore: Decorator valid here
26
+ @inline const STORAGE = offsetof<JSON.Value>("storage");
19
27
 
20
28
  /**
21
29
  * JSON Encoder/Decoder for AssemblyScript
22
30
  */
23
31
  export namespace JSON {
32
+ /**
33
+ * Enum representing the different types supported by JSON.
34
+ */
35
+ export enum Types {
36
+ Raw = 0,
37
+ U8 = 1,
38
+ U16 = 2,
39
+ U32 = 3,
40
+ U64 = 4,
41
+ F32 = 5,
42
+ F64 = 6,
43
+ Bool = 7,
44
+ String = 8,
45
+ Obj = 8,
46
+ Array = 9
47
+ }
48
+ export type Raw = string;
49
+ export class Value {
50
+ public type: i32;
51
+
52
+ // @ts-ignore
53
+ private storage: u64;
54
+
55
+ private constructor() { unreachable(); }
56
+
57
+ /**
58
+ * Creates an JSON.Value instance from a given value.
59
+ * @param value - The value to be encapsulated.
60
+ * @returns An instance of JSON.Value.
61
+ */
62
+ @inline static from<T>(value: T): JSON.Value {
63
+ if (value instanceof JSON.Value) {
64
+ return value;
65
+ }
66
+ const out = changetype<JSON.Value>(__new(offsetof<JSON.Value>(), idof<JSON.Value>()));
67
+ out.set<T>(value);
68
+ return out;
69
+ }
70
+
71
+ /**
72
+ * Sets the value of the JSON.Value instance.
73
+ * @param value - The value to be set.
74
+ */
75
+ @inline set<T>(value: T): void {
76
+ if (isBoolean<T>()) {
77
+ this.type = JSON.Types.Bool;
78
+ store<T>(changetype<usize>(this), value, STORAGE);
79
+ } else if (value instanceof u8 || value instanceof i8) {
80
+ this.type = JSON.Types.U8;
81
+ store<T>(changetype<usize>(this), value, STORAGE);
82
+ } else if (value instanceof u16 || value instanceof i16) {
83
+ this.type = JSON.Types.U16;
84
+ store<T>(changetype<usize>(this), value, STORAGE);
85
+ } else if (value instanceof u32 || value instanceof i32) {
86
+ this.type = JSON.Types.U32;
87
+ store<T>(changetype<usize>(this), value, STORAGE);
88
+ } else if (value instanceof u64 || value instanceof i64) {
89
+ this.type = JSON.Types.U64;
90
+ store<T>(changetype<usize>(this), value, STORAGE);
91
+ } else if (value instanceof f32) {
92
+ this.type = JSON.Types.F64;
93
+ store<T>(changetype<usize>(this), value, STORAGE);
94
+ } else if (value instanceof f64) {
95
+ this.type = JSON.Types.F64;
96
+ store<T>(changetype<usize>(this), value, STORAGE);
97
+ } else if (isString<T>()) {
98
+ this.type = JSON.Types.String;
99
+ store<T>(changetype<usize>(this), value, STORAGE);
100
+ } else if (value instanceof Map) {
101
+ if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
102
+ abort("Maps must be of type Map<string, JSON.Value>!");
103
+ }
104
+ this.type = JSON.Types.Obj;
105
+ store<T>(changetype<usize>(this), value, STORAGE);
106
+ } else if (isArray<T>()) {
107
+ // @ts-ignore: T satisfies constraints of any[]
108
+ this.type = JSON.Types.Array + getArrayDepth<T>(0);
109
+ store<T>(changetype<usize>(this), value, STORAGE);
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Gets the value of the JSON.Value instance.
115
+ * @returns The encapsulated value.
116
+ */
117
+ @inline get<T>(): T {
118
+ return load<T>(changetype<usize>(this), STORAGE);
119
+ }
120
+
121
+ /**
122
+ * Converts the JSON.Value to a string representation.
123
+ * @param useString - If true, treats Buffer as a string.
124
+ * @returns The string representation of the JSON.Value.
125
+ */
126
+ toString(): string {
127
+ switch (this.type) {
128
+ case JSON.Types.U8: return this.get<u8>().toString();
129
+ case JSON.Types.U16: return this.get<u16>().toString();
130
+ case JSON.Types.U32: return this.get<u32>().toString();
131
+ case JSON.Types.U64: return this.get<u64>().toString();
132
+ case JSON.Types.String: return "\"" + this.get<string>() + "\"";
133
+ case JSON.Types.Bool: return this.get<boolean>() ? "true" : "false";
134
+ default: {
135
+ const arr = this.get<JSON.Value[]>();
136
+ if (!arr.length) return "[]";
137
+ const out = Sink.fromStringLiteral("[");
138
+ const end = arr.length - 1;
139
+ for (let i = 0; i < end; i++) {
140
+ const element = unchecked(arr[i]);
141
+ out.write(element.toString());
142
+ out.write(",");
143
+ }
144
+
145
+ const element = unchecked(arr[end]);
146
+ out.write(element.toString());
147
+
148
+ out.write("]");
149
+ return out.toString();
150
+ }
151
+ }
152
+ }
153
+ }
24
154
  export class Box<T> {
25
- constructor(public value: T) {}
155
+ constructor(public value: T) { }
26
156
  @inline static from<T>(value: T): Box<T> {
27
157
  return new Box(value);
28
158
  }
29
159
  }
30
-
160
+
31
161
  /**
32
162
  * Stringifies valid JSON data.
33
163
  * ```js
@@ -52,7 +182,7 @@ export namespace JSON {
52
182
  // @ts-ignore
53
183
  } else if (isString<nonnull<T>>()) {
54
184
  return serializeString(changetype<string>(data));
55
- // @ts-ignore: Supplied by trasnform
185
+ // @ts-ignore: Supplied by transform
56
186
  } else if (isDefined(data.__SERIALIZE)) {
57
187
  // @ts-ignore
58
188
  return serializeObject(changetype<nonnull<T>>(data));
package/assembly/test.ts CHANGED
@@ -1,42 +1,33 @@
1
1
  // import { JSON } from ".";
2
- import { bs } from "./custom/bs";
3
- // @json
4
- // class Vec3 {
5
- // x: f32 = 0.0;
6
- // y: f32 = 0.0;
7
- // z: f32 = 0.0;
8
- // }
2
+ import { JSON } from ".";
3
+ @json
4
+ class Vec3 {
5
+ x: f32 = 0.0;
6
+ y: f32 = 0.0;
7
+ z: f32 = 0.0;
8
+ }
9
9
 
10
- // @json
11
- // class Player {
12
- // @alias("first name")
13
- // firstName!: string;
14
- // lastName!: string;
15
- // lastActive!: i32[];
16
- // // Drop in a code block, function, or expression that evaluates to a boolean
17
- // @omitif("this.age < 18")
18
- // age!: i32;
19
- // @omitnull()
20
- // pos!: Vec3 | null;
21
- // isVerified!: boolean;
22
- // }
10
+ @json
11
+ class Player {
12
+ firstName!: string;
13
+ lastName!: string;
14
+ lastActive!: i32[];
15
+ age!: i32;
16
+ pos!: JSON.Raw;
17
+ isVerified!: boolean;
18
+ }
23
19
 
24
- // const player: Player = {
25
- // firstName: "Emmet",
26
- // lastName: "West",
27
- // lastActive: [8, 27, 2022],
28
- // age: 23,
29
- // pos: {
30
- // x: 3.4,
31
- // y: 1.2,
32
- // z: 8.3
33
- // },
34
- // isVerified: true
35
- // };
20
+ const player: Player = {
21
+ firstName: "Emmet",
22
+ lastName: "West",
23
+ lastActive: [8, 27, 2022],
24
+ age: 23,
25
+ pos: "{\"x\":3.4,\"y\":1.2,\"z\":8.3}",
26
+ isVerified: true
27
+ };
36
28
 
37
- // const stringified = JSON.stringify<Player>(player);
38
-
39
- // const parsed = JSON.parse<Player>(stringified);
40
-
41
- bs.write_32(6422620);
42
- console.log(bs.out<string>())
29
+ const stringified = JSON.stringify<Player>(player);
30
+ console.log(stringified);
31
+ console.log(idof<JSON.Raw>().toString());
32
+ console.log(idof<string>().toString())
33
+ // const parsed = JSON.parse<Player>(stringified);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.9.14",
3
+ "version": "0.9.16",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -32,7 +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.9",
35
+ "as-test": "0.3.1",
36
36
  "assemblyscript": "^0.27.29",
37
37
  "assemblyscript-prettier": "^3.0.1",
38
38
  "benchmark": "^2.1.4",
@@ -117,8 +117,14 @@ class JSONTransform extends BaseVisitor {
117
117
  }
118
118
  if (!mem.flags.length) {
119
119
  mem.flags = [PropertyFlags.None];
120
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
121
- mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
120
+ if (type == "JSON.Raw") {
121
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + "}";
122
+ mem.deserialize = "this." + name.text + " = " + "data.substring(value_start, value_end);";
123
+ }
124
+ else {
125
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
126
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
127
+ }
122
128
  }
123
129
  if (mem.flags.includes(PropertyFlags.OmitNull)) {
124
130
  mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
@@ -136,11 +142,11 @@ class JSONTransform extends BaseVisitor {
136
142
  else if (mem.flags.includes(PropertyFlags.Flatten)) {
137
143
  const nullable = mem.node.type.isNullable;
138
144
  if (nullable) {
139
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
145
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
140
146
  mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
141
147
  }
142
148
  else {
143
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
149
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
144
150
  mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");";
145
151
  }
146
152
  mem.name = name.text;
@@ -164,6 +170,9 @@ class JSONTransform extends BaseVisitor {
164
170
  else if (t === "bool" || t === "boolean") {
165
171
  mem.initialize = "this." + name.text + " = false";
166
172
  }
173
+ else if (t === "JSON.Raw") {
174
+ mem.initialize = "this." + name.text + " = \"\"";
175
+ }
167
176
  else if (t === "u8" ||
168
177
  t === "u16" ||
169
178
  t === "u32" ||
@@ -284,6 +293,8 @@ class JSONTransform extends BaseVisitor {
284
293
  let f = true;
285
294
  for (let i = 0; i < memberSet.length; i++) {
286
295
  const member = memberSet[i];
296
+ if (!member.deserialize)
297
+ continue;
287
298
  const name = encodeKey(member.alias || member.name);
288
299
  if (name.length === 1) {
289
300
  DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.9.14",
3
+ "version": "0.9.16",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -135,8 +135,13 @@ class JSONTransform extends BaseVisitor {
135
135
 
136
136
  if (!mem.flags.length) {
137
137
  mem.flags = [PropertyFlags.None];
138
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
139
- mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
138
+ if (type == "JSON.Raw") {
139
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + "}";
140
+ mem.deserialize = "this." + name.text + " = " + "data.substring(value_start, value_end);"
141
+ } else {
142
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
143
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
144
+ }
140
145
  }
141
146
 
142
147
  if (mem.flags.includes(PropertyFlags.OmitNull)) {
@@ -152,10 +157,10 @@ class JSONTransform extends BaseVisitor {
152
157
  } else if (mem.flags.includes(PropertyFlags.Flatten)) {
153
158
  const nullable = (mem.node.type as NamedTypeNode).isNullable;
154
159
  if (nullable) {
155
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
160
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
156
161
  mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
157
162
  } else {
158
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
163
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
159
164
  mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");";
160
165
  }
161
166
  mem.name = name.text;
@@ -174,6 +179,8 @@ class JSONTransform extends BaseVisitor {
174
179
  mem.initialize = "this." + name.text + " = instantiate<" + mem.type + ">()";
175
180
  } else if (t === "bool" || t === "boolean") {
176
181
  mem.initialize = "this." + name.text + " = false";
182
+ } else if (t === "JSON.Raw") {
183
+ mem.initialize = "this." + name.text + " = \"\"";
177
184
  } else if (
178
185
  t === "u8" ||
179
186
  t === "u16" ||
@@ -300,6 +307,7 @@ class JSONTransform extends BaseVisitor {
300
307
  let f = true;
301
308
  for (let i = 0; i < memberSet.length; i++) {
302
309
  const member = memberSet[i]!;
310
+ if (!member.deserialize) continue;
303
311
  const name = encodeKey(member.alias || member.name);
304
312
  if (name.length === 1) {
305
313
  DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;