json-as 0.9.20 → 0.9.22

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.
@@ -1,103 +1,153 @@
1
1
  import { dtoa_buffered, itoa_buffered } from "util/number";
2
2
  import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
3
+ // @ts-ignore
3
4
  @inline const MAX_LEN: usize = 65536;
4
5
  const STORE: usize[] = [];
5
6
  let STORE_LEN: usize = 0;
6
7
  const CACHE = memory.data(i32(MAX_LEN));
7
8
  // Configurable amount of referenceable strings
8
9
  let POINTER = changetype<usize>(CACHE);
10
+ // @ts-ignore
9
11
  @inline const MAX_CACHE = CACHE + MAX_LEN;
10
12
 
11
13
  export namespace bs {
14
+ // @ts-ignore
12
15
  @inline export function write_int<T extends number>(num: T): void {
13
16
  POINTER += itoa_buffered(POINTER, num) << 1;
14
17
  if (MAX_CACHE <= POINTER) bs.shrink();
15
18
  }
19
+
20
+ // @ts-ignore
16
21
  @inline export function write_int_u<T extends number>(num: T): void {
17
22
  POINTER += itoa_buffered(POINTER, num) << 1;
18
23
  }
19
24
 
25
+ // @ts-ignore
20
26
  @inline export function write_fl<T extends number>(num: T): void {
21
27
  POINTER += dtoa_buffered(POINTER, num) << 1;
22
28
  if (MAX_CACHE <= POINTER) bs.shrink();
23
29
  }
30
+
31
+ // @ts-ignore
24
32
  @inline export function write_fl_u<T extends number>(num: T): void {
25
33
  POINTER += dtoa_buffered(POINTER, num) << 1;
26
34
  }
27
35
 
36
+ // @ts-ignore
28
37
  @inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
29
38
  memory.copy(POINTER, buf, bytes);
30
39
  POINTER += bytes;
31
40
  if (MAX_CACHE <= POINTER) bs.shrink();
32
41
  }
42
+
43
+ // @ts-ignore
33
44
  @inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
34
45
  memory.copy(POINTER, buf, bytes);
35
46
  POINTER += bytes;
36
47
  }
37
48
 
49
+ // @ts-ignore
38
50
  @inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
39
51
  memory.copy(POINTER, changetype<usize>(str), bytes);
40
52
  POINTER += bytes;
41
53
  if (MAX_CACHE <= POINTER) bs.shrink();
42
54
  }
55
+
56
+ // @ts-ignore
43
57
  @inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
44
58
  memory.copy(POINTER, changetype<usize>(str), bytes);
45
59
  POINTER += bytes;
46
60
  }
47
61
 
62
+ // @ts-ignore
48
63
  @inline export function write_s_se(str: string, start: usize, end: usize): void {
49
64
  const bytes = end - start;
50
65
  memory.copy(POINTER, changetype<usize>(str) + start, bytes);
51
66
  POINTER += bytes;
52
67
  if (MAX_CACHE <= POINTER) bs.shrink();
53
68
  }
69
+
70
+ // @ts-ignore
54
71
  @inline export function write_s_se_u(str: string, start: usize, end: usize): void {
55
72
  const bytes = end - start;
56
73
  memory.copy(POINTER, changetype<usize>(str) + start, bytes);
57
74
  POINTER += bytes;
58
75
  }
59
76
 
77
+ // @ts-ignore
78
+ @inline export function write_8(char: i32): void {
79
+ store<u8>(POINTER, char);
80
+ POINTER += 2;
81
+ if (MAX_CACHE <= POINTER) bs.shrink();
82
+ }
83
+
84
+ // @ts-ignore
85
+ @inline export function write_8_u(char: i32): void {
86
+ store<u8>(POINTER, char);
87
+ //POINTER += 2;
88
+ }
89
+
90
+ // @ts-ignore
60
91
  @inline export function write_16(char: i32): void {
61
92
  store<u16>(POINTER, char);
62
93
  POINTER += 2;
63
94
  if (MAX_CACHE <= POINTER) bs.shrink();
64
95
  }
96
+
97
+ // @ts-ignore
65
98
  @inline export function write_16_u(char: i32): void {
66
99
  store<u16>(POINTER, char);
67
100
  //POINTER += 2;
68
101
  }
69
102
 
103
+ // @ts-ignore
70
104
  @inline export function write_32(chars: i32): void {
71
105
  store<u32>(POINTER, chars);
72
106
  POINTER += 4;
73
107
  if (MAX_CACHE <= POINTER) bs.shrink();
74
108
  }
109
+
110
+ // @ts-ignore
75
111
  @inline export function write_32_u(chars: i32): void {
76
112
  store<u32>(POINTER, chars);
77
113
  //POINTER += 4;
78
114
  }
79
115
 
116
+ // @ts-ignore
80
117
  @inline export function write_64(chars: i64): void {
81
118
  store<u64>(POINTER, chars);
82
119
  POINTER += 8;
83
120
  if (MAX_CACHE <= POINTER) bs.shrink();
84
121
  }
85
122
 
123
+ // @ts-ignore
86
124
  @inline export function write_64_u(chars: i64): void {
87
125
  store<u64>(POINTER, chars);
88
126
  POINTER += 8;
89
127
  }
90
128
 
129
+ // @ts-ignore
91
130
  @inline export function write_128(chars: v128): void {
92
131
  store<v128>(POINTER, chars);
93
132
  POINTER += 16;
94
133
  if (MAX_CACHE <= POINTER) bs.shrink();
95
134
  }
135
+
136
+ // @ts-ignore
137
+ @inline export function write_128_n(chars: v128, n: usize): void {
138
+ store<v128>(POINTER, chars);
139
+ POINTER += n;
140
+ if (MAX_CACHE <= POINTER) bs.shrink();
141
+ }
142
+
143
+ // @ts-ignore
96
144
  @inline export function write_128_u(chars: v128): void {
97
145
  store<v128>(POINTER, chars);
98
146
  //POINTER += 16;
99
- //if (MAX_CACHE <= POINTER) bs.shrink();
147
+ //if (MAX_CACHE <= POINTER) bs.shrink();
100
148
  }
149
+
150
+ // @ts-ignore
101
151
  @inline export function shrink(): void {
102
152
  const len = POINTER - CACHE;
103
153
  STORE_LEN += len;
@@ -109,6 +159,8 @@ export namespace bs {
109
159
  bs.reset();
110
160
  STORE.push(out);
111
161
  }
162
+
163
+ // @ts-ignore
112
164
  @inline export function out<T>(): T {
113
165
  const len = POINTER - CACHE;
114
166
  let out = __new(
@@ -133,6 +185,7 @@ export namespace bs {
133
185
  return changetype<T>(out);
134
186
  }
135
187
 
188
+ // @ts-ignore
136
189
  @inline export function out_u<T>(): T {
137
190
  const len = POINTER - CACHE;
138
191
  const out = __new(
@@ -146,9 +199,12 @@ export namespace bs {
146
199
  return changetype<T>(out);
147
200
  }
148
201
 
202
+ // @ts-ignore
149
203
  @inline export function _out(out: usize): void {
150
204
  memory.copy(out, CACHE, POINTER - CACHE);
151
205
  }
206
+
207
+ // @ts-ignore
152
208
  @inline export function reset(): void {
153
209
  POINTER = CACHE;
154
210
  }
@@ -16,33 +16,16 @@ export class Sink {
16
16
 
17
17
  static withCapacity(capacity: i32): Sink {
18
18
  const sink = new Sink();
19
- sink.buffer = changetype<ArrayBuffer>(
20
- __new(
21
- max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1),
22
- idof<ArrayBuffer>(),
23
- ),
24
- );
19
+ sink.buffer = changetype<ArrayBuffer>(__new(max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1), idof<ArrayBuffer>()));
25
20
  return sink;
26
21
  }
27
22
 
28
- static fromString(
29
- initial: string = "",
30
- capacity: i32 = MIN_BUFFER_LEN,
31
- ): Sink {
23
+ static fromString(initial: string = "", capacity: i32 = MIN_BUFFER_LEN): Sink {
32
24
  const sink = new Sink();
33
25
  const size = (<u32>initial.length) << 1;
34
- sink.buffer = changetype<ArrayBuffer>(
35
- __new(
36
- max<u32>(size, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1)),
37
- idof<ArrayBuffer>(),
38
- ),
39
- );
26
+ sink.buffer = changetype<ArrayBuffer>(__new(max<u32>(size, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1)), idof<ArrayBuffer>()));
40
27
  if (size) {
41
- memory.copy(
42
- changetype<usize>(sink.buffer),
43
- changetype<usize>(initial),
44
- size,
45
- );
28
+ memory.copy(changetype<usize>(sink.buffer), changetype<usize>(initial), size);
46
29
  sink.offset += size;
47
30
  }
48
31
  return sink;
@@ -53,34 +36,18 @@ export class Sink {
53
36
  const size = (<u32>initial.length) << 1;
54
37
  sink.buffer = changetype<ArrayBuffer>(__new(size, idof<ArrayBuffer>()));
55
38
  if (size) {
56
- memory.copy(
57
- changetype<usize>(sink.buffer),
58
- changetype<usize>(initial),
59
- size,
60
- );
39
+ memory.copy(changetype<usize>(sink.buffer), changetype<usize>(initial), size);
61
40
  sink.offset += size;
62
41
  }
63
42
  return sink;
64
43
  }
65
44
 
66
- static fromBuffer(
67
- initial: ArrayBuffer,
68
- capacity: i32 = MIN_BUFFER_LEN,
69
- ): Sink {
45
+ static fromBuffer(initial: ArrayBuffer, capacity: i32 = MIN_BUFFER_LEN): Sink {
70
46
  const sink = new Sink();
71
47
  const size = <u32>initial.byteLength;
72
- sink.buffer = changetype<ArrayBuffer>(
73
- __new(
74
- max<u32>(size, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1)),
75
- idof<ArrayBuffer>(),
76
- ),
77
- );
48
+ sink.buffer = changetype<ArrayBuffer>(__new(max<u32>(size, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1)), idof<ArrayBuffer>()));
78
49
  if (size) {
79
- memory.copy(
80
- changetype<usize>(sink.buffer),
81
- changetype<usize>(initial),
82
- size,
83
- );
50
+ memory.copy(changetype<usize>(sink.buffer), changetype<usize>(initial), size);
84
51
  sink.offset = size;
85
52
  }
86
53
  return sink;
@@ -116,11 +83,7 @@ export class Sink {
116
83
  this.ensureCapacity(size);
117
84
  let offset = this.offset;
118
85
 
119
- memory.copy(
120
- changetype<usize>(this.buffer) + offset,
121
- changetype<usize>(src) + ((<usize>start) << 1),
122
- size,
123
- );
86
+ memory.copy(changetype<usize>(this.buffer) + offset, changetype<usize>(src) + ((<usize>start) << 1), size);
124
87
  this.offset = offset + size;
125
88
  return this;
126
89
  }
@@ -140,8 +103,7 @@ export class Sink {
140
103
  this.ensureCapacity(size + 2);
141
104
  let offset = this.offset;
142
105
  let dest = changetype<usize>(this.buffer) + offset;
143
- if (size)
144
- memory.copy(dest, changetype<usize>(src) + ((<usize>start) << 1), size);
106
+ if (size) memory.copy(dest, changetype<usize>(src) + ((<usize>start) << 1), size);
145
107
  store<u16>(dest + size, NEW_LINE_CHAR);
146
108
  this.offset = offset + (size + 2);
147
109
  return this;
@@ -209,12 +171,10 @@ export class Sink {
209
171
  maxCapacity = 21 << 1;
210
172
  }
211
173
  this.ensureCapacity(maxCapacity);
212
- offset +=
213
- itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
174
+ offset += itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
214
175
  } else {
215
176
  this.ensureCapacity(32 << 1);
216
- offset +=
217
- dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
177
+ offset += dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
218
178
  }
219
179
  this.offset = offset;
220
180
  return this;
@@ -222,11 +182,9 @@ export class Sink {
222
182
  writeNumberUnsafe<T extends number>(value: T): Sink {
223
183
  let offset = this.offset;
224
184
  if (isInteger<T>()) {
225
- offset +=
226
- itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
185
+ offset += itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
227
186
  } else {
228
- offset +=
229
- dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
187
+ offset += dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
230
188
  }
231
189
  this.offset = offset;
232
190
  return this;
@@ -234,11 +192,9 @@ export class Sink {
234
192
  writeIntegerUnsafe<T extends number>(value: T): Sink {
235
193
  let offset = this.offset;
236
194
  if (isInteger<T>()) {
237
- offset +=
238
- itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
195
+ offset += itoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
239
196
  } else {
240
- offset +=
241
- dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
197
+ offset += dtoa_buffered(changetype<usize>(this.buffer) + offset, value) << 1;
242
198
  }
243
199
  this.offset = offset;
244
200
  return this;
@@ -246,21 +202,11 @@ export class Sink {
246
202
 
247
203
  reserve(capacity: i32, clear: bool = false): void {
248
204
  if (clear) this.offset = 0;
249
- this.buffer = changetype<ArrayBuffer>(
250
- __renew(
251
- changetype<usize>(this.buffer),
252
- max<u32>(this.offset, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1)),
253
- ),
254
- );
205
+ this.buffer = changetype<ArrayBuffer>(__renew(changetype<usize>(this.buffer), max<u32>(this.offset, max<u32>(MIN_BUFFER_SIZE, (<u32>capacity) << 1))));
255
206
  }
256
207
 
257
208
  shrink(): void {
258
- this.buffer = changetype<ArrayBuffer>(
259
- __renew(
260
- changetype<usize>(this.buffer),
261
- max<u32>(this.offset, MIN_BUFFER_SIZE),
262
- ),
263
- );
209
+ this.buffer = changetype<ArrayBuffer>(__renew(changetype<usize>(this.buffer), max<u32>(this.offset, MIN_BUFFER_SIZE)));
264
210
  }
265
211
 
266
212
  clear(): void {
@@ -279,9 +225,7 @@ export class Sink {
279
225
  let buffer = this.buffer;
280
226
  let newSize = this.offset + deltaBytes;
281
227
  if (newSize > <u32>buffer.byteLength) {
282
- this.buffer = changetype<ArrayBuffer>(
283
- __renew(changetype<usize>(buffer), nextPowerOf2(newSize)),
284
- );
228
+ this.buffer = changetype<ArrayBuffer>(__renew(changetype<usize>(buffer), nextPowerOf2(newSize)));
285
229
  }
286
230
  }
287
231
  }
@@ -31,11 +31,7 @@ export function deserializeArray<T extends unknown[]>(data: string): T {
31
31
  if (isDefined(type.__DESERIALIZE)) {
32
32
  return deserializeObjectArray<T>(data);
33
33
  }
34
- throw new Error(
35
- "Could not parse array of type " +
36
- nameof<T>() +
37
- "! Make sure to add the @json decorator over classes!",
38
- );
34
+ throw new Error("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
39
35
  } else {
40
36
  throw new Error("Could not parse array of type " + nameof<T>() + "!");
41
37
  }
@@ -1,3 +1,4 @@
1
+ import { bs } from "../custom/bs";
1
2
  import {
2
3
  CHAR_B,
3
4
  BACK_SLASH,
@@ -85,4 +86,73 @@ import { unsafeCharCodeAt } from "../custom/util";
85
86
  result.write(data, last, end);
86
87
  }
87
88
  return result.toString()
88
- }
89
+ }
90
+
91
+ // @ts-ignore: Decorator valid here
92
+ // @inline export function deserializeString_BS(data: string, start: i32 = 0, end: i32 = 0): void {
93
+ // end = end || data.length - 1;
94
+ // let last = start + 1;
95
+ // for (let i = last; i < end; i++) {
96
+ // if (unsafeCharCodeAt(data, i) !== BACK_SLASH) {
97
+ // continue;
98
+ // }
99
+ // const char = unsafeCharCodeAt(data, ++i);
100
+ // bs.write_s_se_u(data, last, i - 1);
101
+ // switch (char) {
102
+ // case QUOTE: {
103
+ // bs.write_8(QUOTE);
104
+ // last = i + 1;
105
+ // break;
106
+ // }
107
+ // case BACK_SLASH: {
108
+ // bs.write_8(BACK_SLASH);
109
+ // last = i + 1;
110
+ // break;
111
+ // }
112
+ // case FWD_SLASH: {
113
+ // bs.write_8(FWD_SLASH);
114
+ // last = i + 1;
115
+ // break;
116
+ // }
117
+ // case CHAR_B: {
118
+ // bs.write_8(BACKSPACE);
119
+ // last = i + 1;
120
+ // break;
121
+ // }
122
+ // case CHAR_F: {
123
+ // bs.write_8(FORM_FEED);
124
+ // last = i + 1;
125
+ // break;
126
+ // }
127
+ // case CHAR_N: {
128
+ // bs.write_8(NEW_LINE);
129
+ // last = i + 1;
130
+ // break;
131
+ // }
132
+ // case CHAR_R: {
133
+ // bs.write_8(CARRIAGE_RETURN);
134
+ // last = i + 1;
135
+ // break;
136
+ // }
137
+ // case CHAR_T: {
138
+ // bs.write_8(TAB);
139
+ // last = i + 1;
140
+ // break;
141
+ // }
142
+ // case CHAR_U: {
143
+
144
+ // const code = u16.parse(data.slice(i + 1, i + 5), 16);
145
+ // bs.w(code);
146
+ // i += 4;
147
+ // last = i + 1;
148
+ // break;
149
+ // }
150
+ // default: {
151
+ // throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
152
+ // }
153
+ // }
154
+ // }
155
+ // if (end > last) {
156
+ // result.write(data, last, end);
157
+ // }
158
+ // }
package/assembly/index.ts CHANGED
@@ -3,7 +3,7 @@ import { serializeString } from "./serialize/string";
3
3
  import { serializeBool } from "./serialize/bool";
4
4
  import { serializeInteger } from "./serialize/integer";
5
5
  import { serializeFloat } from "./serialize/float";
6
- import { serializeObject } from "./serialize/object";
6
+ import { serializeObject, serializeObject_Pretty } from "./serialize/object";
7
7
  import { serializeDate } from "./serialize/date";
8
8
  import { serializeArray } from "./serialize/array";
9
9
  import { serializeMap } from "./serialize/map";
@@ -13,11 +13,18 @@ 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 { BRACKET_LEFT, NULL_WORD } from "./custom/chars";
16
+ import { NULL_WORD } from "./custom/chars";
17
17
  import { deserializeInteger } from "./deserialize/integer";
18
18
  import { deserializeString } from "./deserialize/string";
19
19
  import { Sink } from "./custom/sink";
20
- import { bs } from "./custom/bs";
20
+ import { getArrayDepth } from "./custom/util";
21
+
22
+ // Config
23
+ class SerializeOptions {
24
+ public pretty: bool = false;
25
+ }
26
+
27
+ const DEFAULT_SERIALIZE_OPTIONS = new SerializeOptions();
21
28
 
22
29
  /**
23
30
  * Offset of the 'storage' property in the JSON.Value class.
@@ -167,7 +174,7 @@ export namespace JSON {
167
174
  * @returns string
168
175
  */
169
176
  // @ts-ignore: Decorator
170
- export function stringify<T>(data: T): string {
177
+ export function stringify<T>(data: T/*, options: SerializeOptions = DEFAULT_SERIALIZE_OPTIONS*/): string {
171
178
  if (isBoolean<T>()) {
172
179
  return serializeBool(data as bool);
173
180
  } else if (isInteger<T>()) {
@@ -184,6 +191,10 @@ export namespace JSON {
184
191
  return serializeString(changetype<string>(data));
185
192
  // @ts-ignore: Supplied by transform
186
193
  } else if (isDefined(data.__SERIALIZE)) {
194
+ /*if (options.pretty) {
195
+ // @ts-ignore
196
+ return serializeObject_Pretty(changetype<nonnull<T>>(data));
197
+ }*/
187
198
  // @ts-ignore
188
199
  return serializeObject(changetype<nonnull<T>>(data));
189
200
  } else if (data instanceof Date) {
@@ -229,7 +240,7 @@ export namespace JSON {
229
240
  return deserializeArray<nonnull<T>>(data);
230
241
  }
231
242
  let type: nonnull<T> = changetype<nonnull<T>>(0);
232
- // @ts-ignore: Defined by trasnform
243
+ // @ts-ignore: Defined by transform
233
244
  if (isDefined(type.__DESERIALIZE)) {
234
245
  // @ts-ignore
235
246
  return deserializeObject<nonnull<T>>(data.trimStart());
@@ -3,7 +3,20 @@
3
3
  * @param data data to serialize
4
4
  * @returns string
5
5
  */
6
+
7
+ import { bs } from "../custom/bs";
8
+
6
9
  // @ts-ignore: Decorator valid here
7
10
  @inline export function serializeBool(data: bool): string {
8
11
  return data ? "true" : "false";
12
+ }
13
+
14
+ @inline export function serializeBool_BS(data: bool): void {
15
+ if (data === true) {
16
+ bs.write_64(28429475166421108); /* true */
17
+ } else {
18
+ //bs.write_128_n(i16x8(102, 97, 108, 115, 101, 0, 0, 0), 10);
19
+ bs.write_64(32370086184550502); /* fals */
20
+ bs.write_16(101); /* e */
21
+ }
9
22
  }
@@ -5,4 +5,9 @@ interface GeneratedInterface {
5
5
  // @ts-ignore: Decorator valid here
6
6
  @inline export function serializeObject<T extends GeneratedInterface>(data: T): string {
7
7
  return changetype<nonnull<T>>(data).__SERIALIZE();
8
+ }
9
+
10
+ // @ts-ignore: Decorator valid here
11
+ @inline export function serializeObject_Pretty<T extends GeneratedInterface>(data: T): string {
12
+ return changetype<nonnull<T>>(data).__SERIALIZE_PRETTY();
8
13
  }