json-as 0.9.27 → 0.9.29

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.
Files changed (39) hide show
  1. package/.prettierrc.json +3 -1
  2. package/README.md +15 -68
  3. package/assembly/__benches__/misc.bench.ts +15 -14
  4. package/assembly/__tests__/date.spec.ts +12 -0
  5. package/assembly/__tests__/types.ts +17 -0
  6. package/assembly/custom/bs.ts +189 -198
  7. package/assembly/custom/chars.ts +2 -2
  8. package/assembly/custom/types.ts +1 -0
  9. package/assembly/custom/util.ts +47 -50
  10. package/assembly/deserialize/array/array.ts +24 -24
  11. package/assembly/deserialize/array/bool.ts +1 -1
  12. package/assembly/deserialize/array/float.ts +16 -16
  13. package/assembly/deserialize/array/integer.ts +16 -16
  14. package/assembly/deserialize/array/map.ts +20 -20
  15. package/assembly/deserialize/array/object.ts +20 -20
  16. package/assembly/deserialize/array/string.ts +1 -1
  17. package/assembly/deserialize/array.ts +2 -2
  18. package/assembly/deserialize/bool.ts +15 -15
  19. package/assembly/deserialize/date.ts +4 -4
  20. package/assembly/deserialize/float.ts +15 -15
  21. package/assembly/deserialize/integer.ts +8 -8
  22. package/assembly/deserialize/map.ts +111 -161
  23. package/assembly/deserialize/object.ts +16 -76
  24. package/assembly/deserialize/string.ts +70 -85
  25. package/assembly/index.ts +25 -24
  26. package/assembly/serialize/array.ts +37 -44
  27. package/assembly/serialize/bool.ts +2 -2
  28. package/assembly/serialize/date.ts +2 -2
  29. package/assembly/serialize/float.ts +2 -2
  30. package/assembly/serialize/integer.ts +3 -3
  31. package/assembly/serialize/map.ts +16 -16
  32. package/assembly/serialize/object.ts +4 -4
  33. package/assembly/serialize/string.ts +60 -63
  34. package/assembly/test.ts +3 -2
  35. package/package.json +2 -1
  36. package/transform/lib/index.js +29 -92
  37. package/transform/lib/index.js.map +1 -1
  38. package/transform/package.json +1 -1
  39. package/transform/src/index.ts +53 -186
@@ -1,24 +1,6 @@
1
1
  import { Virtual } from "as-virtual/assembly";
2
2
  import { containsCodePoint, unsafeCharCodeAt } from "../custom/util";
3
- import {
4
- CHAR_A,
5
- BACK_SLASH,
6
- COLON,
7
- COMMA,
8
- CHAR_E,
9
- CHAR_F,
10
- CHAR_L,
11
- BRACE_LEFT,
12
- BRACKET_LEFT,
13
- CHAR_N,
14
- QUOTE,
15
- CHAR_R,
16
- BRACE_RIGHT,
17
- BRACKET_RIGHT,
18
- CHAR_S,
19
- CHAR_T,
20
- CHAR_U
21
- } from "../custom/chars";
3
+ import { CHAR_A, BACK_SLASH, COLON, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../custom/chars";
22
4
  import { deserializeBoolean } from "./bool";
23
5
  import { JSON } from "..";
24
6
  import { deserializeString } from "./string";
@@ -28,162 +10,130 @@ import { deserializeFloat } from "./float";
28
10
 
29
11
  // @ts-ignore: Decorator valid here
30
12
  @inline export function deserializeMap<T extends Map>(data: string): T {
13
+ const map: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
31
14
 
32
- const map: nonnull<T> = changetype<nonnull<T>>(
33
- __new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
34
- );
35
-
36
- const key = Virtual.createEmpty<string>();
37
- let isKey = false;
38
- let depth = 0;
39
- let outerLoopIndex = 1;
40
- for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
41
- const char = unsafeCharCodeAt(data, outerLoopIndex);
15
+ const key = Virtual.createEmpty<string>();
16
+ let isKey = false;
17
+ let depth = 0;
18
+ let outerLoopIndex = 1;
19
+ for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
20
+ const char = unsafeCharCodeAt(data, outerLoopIndex);
21
+ if (char === BRACKET_LEFT) {
22
+ for (let arrayValueIndex = outerLoopIndex; arrayValueIndex < data.length - 1; arrayValueIndex++) {
23
+ const char = unsafeCharCodeAt(data, arrayValueIndex);
42
24
  if (char === BRACKET_LEFT) {
43
- for (
44
- let arrayValueIndex = outerLoopIndex;
45
- arrayValueIndex < data.length - 1;
46
- arrayValueIndex++
47
- ) {
48
- const char = unsafeCharCodeAt(data, arrayValueIndex);
49
- if (char === BRACKET_LEFT) {
50
- depth++;
51
- } else if (char === BRACKET_RIGHT) {
52
- depth--;
53
- if (depth === 0) {
54
- ++arrayValueIndex;
55
- map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, arrayValueIndex)));
56
- outerLoopIndex = arrayValueIndex;
57
- isKey = false;
58
- break;
59
- }
60
- }
61
- }
62
- } else if (char === BRACE_LEFT) {
63
- for (
64
- let objectValueIndex = outerLoopIndex;
65
- objectValueIndex < data.length - 1;
66
- objectValueIndex++
67
- ) {
68
- const char = unsafeCharCodeAt(data, objectValueIndex);
69
- if (char === BRACE_LEFT) {
70
- depth++;
71
- } else if (char === BRACE_RIGHT) {
72
- depth--;
73
- if (depth === 0) {
74
- ++objectValueIndex;
75
- map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
76
- outerLoopIndex = objectValueIndex;
77
- isKey = false;
78
- break;
79
- }
80
- }
81
- }
82
- } else if (char === QUOTE) {
83
- let escaping = false;
84
- for (
85
- let stringValueIndex = ++outerLoopIndex;
86
- stringValueIndex < data.length - 1;
87
- stringValueIndex++
88
- ) {
89
- const char = unsafeCharCodeAt(data, stringValueIndex);
90
- if (char === BACK_SLASH && !escaping) {
91
- escaping = true;
92
- } else {
93
- if (
94
- char === QUOTE && !escaping
95
- ) {
96
- if (isKey === false) {
97
- // perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
98
- if (containsCodePoint(data, BACK_SLASH, outerLoopIndex, stringValueIndex)) {
99
- key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
100
- } else {
101
- key.reinst(data, outerLoopIndex, stringValueIndex);
102
- }
103
- isKey = true;
104
- } else {
105
- if (isString<valueof<T>>()) {
106
- const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
107
- map.set(deserializeMapKey<indexof<T>>(key), value);
108
- }
109
- isKey = false;
110
- }
111
- outerLoopIndex = ++stringValueIndex;
112
- break;
113
- }
114
- escaping = false;
115
- }
116
- }
117
- } else if (
118
- char == CHAR_N &&
119
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
120
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
121
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L) {
122
- if (isNullable<valueof<T>>()) {
123
- map.set(deserializeMapKey<indexof<T>>(key), null);
124
- }
125
- isKey = false;
126
- } else if (
127
- char === CHAR_T &&
128
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_R &&
129
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
130
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
131
- ) {
132
- if (isBoolean<valueof<T>>()) {
133
- map.set(deserializeMapKey<indexof<T>>(key), true);
134
- }
25
+ depth++;
26
+ } else if (char === BRACKET_RIGHT) {
27
+ depth--;
28
+ if (depth === 0) {
29
+ ++arrayValueIndex;
30
+ map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, arrayValueIndex)));
31
+ outerLoopIndex = arrayValueIndex;
135
32
  isKey = false;
136
- } else if (
137
- char === CHAR_F &&
138
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_A &&
139
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
140
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_S &&
141
- unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
142
- ) {
143
- if (isBoolean<valueof<T>>()) {
144
- map.set(deserializeMapKey<indexof<T>>(key), false);
145
- }
33
+ break;
34
+ }
35
+ }
36
+ }
37
+ } else if (char === BRACE_LEFT) {
38
+ for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
39
+ const char = unsafeCharCodeAt(data, objectValueIndex);
40
+ if (char === BRACE_LEFT) {
41
+ depth++;
42
+ } else if (char === BRACE_RIGHT) {
43
+ depth--;
44
+ if (depth === 0) {
45
+ ++objectValueIndex;
46
+ map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
47
+ outerLoopIndex = objectValueIndex;
146
48
  isKey = false;
147
- } else if ((char >= 48 && char <= 57) || char === 45) {
148
- let numberValueIndex = ++outerLoopIndex;
149
- for (; numberValueIndex < data.length; numberValueIndex++) {
150
- const char = unsafeCharCodeAt(data, numberValueIndex);
151
- if (char === COLON || char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
152
- if (isInteger<valueof<T>>()) {
153
- map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
154
- } else if (isFloat<valueof<T>>()) {
155
- map.set(deserializeMapKey<indexof<T>>(key), deserializeFloat<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
156
- }
157
- outerLoopIndex = numberValueIndex;
158
- isKey = false;
159
- break;
160
- }
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ } else if (char === QUOTE) {
54
+ let escaping = false;
55
+ for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
56
+ const char = unsafeCharCodeAt(data, stringValueIndex);
57
+ if (char === BACK_SLASH && !escaping) {
58
+ escaping = true;
59
+ } else {
60
+ if (char === QUOTE && !escaping) {
61
+ if (isKey === false) {
62
+ // perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
63
+ if (containsCodePoint(data, BACK_SLASH, outerLoopIndex, stringValueIndex)) {
64
+ key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
65
+ } else {
66
+ key.reinst(data, outerLoopIndex, stringValueIndex);
67
+ }
68
+ isKey = true;
69
+ } else {
70
+ if (isString<valueof<T>>()) {
71
+ const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
72
+ map.set(deserializeMapKey<indexof<T>>(key), value);
73
+ }
74
+ isKey = false;
161
75
  }
76
+ outerLoopIndex = ++stringValueIndex;
77
+ break;
78
+ }
79
+ escaping = false;
162
80
  }
81
+ }
82
+ } else if (char == CHAR_N && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L) {
83
+ if (isNullable<valueof<T>>()) {
84
+ map.set(deserializeMapKey<indexof<T>>(key), null);
85
+ }
86
+ isKey = false;
87
+ } else if (char === CHAR_T && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_R && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E) {
88
+ if (isBoolean<valueof<T>>()) {
89
+ map.set(deserializeMapKey<indexof<T>>(key), true);
90
+ }
91
+ isKey = false;
92
+ } else if (char === CHAR_F && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_A && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_S && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E) {
93
+ if (isBoolean<valueof<T>>()) {
94
+ map.set(deserializeMapKey<indexof<T>>(key), false);
95
+ }
96
+ isKey = false;
97
+ } else if ((char >= 48 && char <= 57) || char === 45) {
98
+ let numberValueIndex = ++outerLoopIndex;
99
+ for (; numberValueIndex < data.length; numberValueIndex++) {
100
+ const char = unsafeCharCodeAt(data, numberValueIndex);
101
+ if (char === COLON || char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
102
+ if (isInteger<valueof<T>>()) {
103
+ map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
104
+ } else if (isFloat<valueof<T>>()) {
105
+ map.set(deserializeMapKey<indexof<T>>(key), deserializeFloat<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
106
+ }
107
+ outerLoopIndex = numberValueIndex;
108
+ isKey = false;
109
+ break;
110
+ }
111
+ }
163
112
  }
113
+ }
164
114
 
165
- return map;
115
+ return map;
166
116
  }
167
117
 
168
118
  //@ts-ignore: Decorator
169
119
  function deserializeMapKey<T>(key: Virtual<string>): T {
170
- const k = key.copyOut();
171
- if (isString<T>()) {
172
- return k as T;
173
- } else if (isBoolean<T>()) {
174
- return deserializeBoolean(k) as T;
175
- } else if (isInteger<T>()) {
176
- return deserializeInteger<T>(k);
177
- } else if (isFloat<T>()) {
178
- return deserializeFloat<T>(k);
179
- }
120
+ const k = key.copyOut();
121
+ if (isString<T>()) {
122
+ return k as T;
123
+ } else if (isBoolean<T>()) {
124
+ return deserializeBoolean(k) as T;
125
+ } else if (isInteger<T>()) {
126
+ return deserializeInteger<T>(k);
127
+ } else if (isFloat<T>()) {
128
+ return deserializeFloat<T>(k);
129
+ }
180
130
 
181
- throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
131
+ throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
182
132
  }
183
133
 
184
134
  // @ts-ignore: Decorator valid here
185
135
  @inline export function deserializeMap_Safe<T extends Map>(data: string): T {
186
- const firstChar = load<u8>(changetype<usize>(data));
187
- if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + " but got \"" + data.slice(0, 100) + "\" instead!");
188
- return deserializeMap<T>(data);
189
- }
136
+ const firstChar = load<u8>(changetype<usize>(data));
137
+ if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + ' but got "' + data.slice(0, 100) + '" instead!');
138
+ return deserializeMap<T>(data);
139
+ }
@@ -4,9 +4,7 @@ import { isSpace } from "util/string";
4
4
 
5
5
  // @ts-ignore: Decorator valid here
6
6
  @inline export function deserializeObject<T>(data: string): T {
7
- const schema: nonnull<T> = changetype<nonnull<T>>(
8
- __new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
9
- );
7
+ const schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
10
8
 
11
9
  // @ts-ignore
12
10
  schema.__INITIALIZE();
@@ -19,11 +17,7 @@ import { isSpace } from "util/string";
19
17
  for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
20
18
  const char = unsafeCharCodeAt(data, outerLoopIndex);
21
19
  if (char === BRACKET_LEFT) {
22
- for (
23
- let arrayValueIndex = outerLoopIndex;
24
- arrayValueIndex < data.length - 1;
25
- arrayValueIndex++
26
- ) {
20
+ for (let arrayValueIndex = outerLoopIndex; arrayValueIndex < data.length - 1; arrayValueIndex++) {
27
21
  const char = unsafeCharCodeAt(data, arrayValueIndex);
28
22
  if (char === BRACKET_LEFT) {
29
23
  depth++;
@@ -40,11 +34,7 @@ import { isSpace } from "util/string";
40
34
  }
41
35
  }
42
36
  } else if (char === BRACE_LEFT) {
43
- for (
44
- let objectValueIndex = outerLoopIndex;
45
- objectValueIndex < data.length - 1;
46
- objectValueIndex++
47
- ) {
37
+ for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
48
38
  const char = unsafeCharCodeAt(data, objectValueIndex);
49
39
  if (char === BRACE_LEFT) {
50
40
  depth++;
@@ -62,11 +52,7 @@ import { isSpace } from "util/string";
62
52
  }
63
53
  } else if (char === QUOTE) {
64
54
  let escaping = false;
65
- for (
66
- let stringValueIndex = ++outerLoopIndex;
67
- stringValueIndex < data.length - 1;
68
- stringValueIndex++
69
- ) {
55
+ for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
70
56
  const char = unsafeCharCodeAt(data, stringValueIndex);
71
57
  if (char === BACK_SLASH && !escaping) {
72
58
  escaping = true;
@@ -87,33 +73,17 @@ import { isSpace } from "util/string";
87
73
  escaping = false;
88
74
  }
89
75
  }
90
- } else if (
91
- char == CHAR_N &&
92
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U &&
93
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
94
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L
95
- ) {
76
+ } else if (char == CHAR_N && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L) {
96
77
  // @ts-ignore
97
78
  schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
98
79
  outerLoopIndex += 3;
99
80
  isKey = false;
100
- } else if (
101
- char === CHAR_T &&
102
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R &&
103
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U &&
104
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E
105
- ) {
81
+ } else if (char === CHAR_T && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E) {
106
82
  // @ts-ignore
107
83
  schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
108
84
  outerLoopIndex += 3;
109
85
  isKey = false;
110
- } else if (
111
- char === CHAR_F &&
112
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A &&
113
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
114
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S &&
115
- unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E
116
- ) {
86
+ } else if (char === CHAR_F && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S && unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E) {
117
87
  // @ts-ignore
118
88
  schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
119
89
  outerLoopIndex += 4;
@@ -138,10 +108,8 @@ import { isSpace } from "util/string";
138
108
  // @ts-ignore: Decorator valid here
139
109
  @inline export function deserializeObject_Safe<T>(data: string): T {
140
110
  const firstChar = load<u8>(changetype<usize>(data));
141
- if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + " but got \"" + data.slice(0, 100) + "\" instead!");
142
- const schema: nonnull<T> = changetype<nonnull<T>>(
143
- __new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
144
- );
111
+ if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + ' but got "' + data.slice(0, 100) + '" instead!');
112
+ const schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
145
113
 
146
114
  // @ts-ignore
147
115
  schema.__INITIALIZE();
@@ -154,11 +122,7 @@ import { isSpace } from "util/string";
154
122
  for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
155
123
  const char = unsafeCharCodeAt(data, outerLoopIndex);
156
124
  if (char === BRACKET_LEFT) {
157
- for (
158
- let arrayValueIndex = outerLoopIndex;
159
- arrayValueIndex < data.length - 1;
160
- arrayValueIndex++
161
- ) {
125
+ for (let arrayValueIndex = outerLoopIndex; arrayValueIndex < data.length - 1; arrayValueIndex++) {
162
126
  const char = unsafeCharCodeAt(data, arrayValueIndex);
163
127
  if (char === BRACKET_LEFT) {
164
128
  depth++;
@@ -175,11 +139,7 @@ import { isSpace } from "util/string";
175
139
  }
176
140
  }
177
141
  } else if (char === BRACE_LEFT) {
178
- for (
179
- let objectValueIndex = outerLoopIndex;
180
- objectValueIndex < data.length - 1;
181
- objectValueIndex++
182
- ) {
142
+ for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
183
143
  const char = unsafeCharCodeAt(data, objectValueIndex);
184
144
  if (char === BRACE_LEFT) {
185
145
  depth++;
@@ -197,11 +157,7 @@ import { isSpace } from "util/string";
197
157
  }
198
158
  } else if (char === QUOTE) {
199
159
  let escaping = false;
200
- for (
201
- let stringValueIndex = ++outerLoopIndex;
202
- stringValueIndex < data.length - 1;
203
- stringValueIndex++
204
- ) {
160
+ for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
205
161
  const char = unsafeCharCodeAt(data, stringValueIndex);
206
162
  if (char === BACK_SLASH && !escaping) {
207
163
  escaping = true;
@@ -222,33 +178,17 @@ import { isSpace } from "util/string";
222
178
  escaping = false;
223
179
  }
224
180
  }
225
- } else if (
226
- char == CHAR_N &&
227
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U &&
228
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
229
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L
230
- ) {
181
+ } else if (char == CHAR_N && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L) {
231
182
  // @ts-ignore
232
183
  schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
233
184
  outerLoopIndex += 3;
234
185
  isKey = false;
235
- } else if (
236
- char === CHAR_T &&
237
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R &&
238
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U &&
239
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E
240
- ) {
186
+ } else if (char === CHAR_T && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E) {
241
187
  // @ts-ignore
242
188
  schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
243
189
  outerLoopIndex += 3;
244
190
  isKey = false;
245
- } else if (
246
- char === CHAR_F &&
247
- unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A &&
248
- unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
249
- unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S &&
250
- unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E
251
- ) {
191
+ } else if (char === CHAR_F && unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A && unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L && unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S && unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E) {
252
192
  // @ts-ignore
253
193
  schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
254
194
  outerLoopIndex += 4;
@@ -268,4 +208,4 @@ import { isSpace } from "util/string";
268
208
  }
269
209
  }
270
210
  return schema;
271
- }
211
+ }
@@ -1,90 +1,75 @@
1
- import {
2
- CHAR_B,
3
- BACK_SLASH,
4
- BACKSPACE,
5
- CARRIAGE_RETURN,
6
- CHAR_F,
7
- FORM_FEED,
8
- FWD_SLASH,
9
- CHAR_N,
10
- NEW_LINE,
11
- QUOTE,
12
- CHAR_R,
13
- CHAR_T,
14
- TAB,
15
- CHAR_U
16
- } from "../custom/chars";
1
+ import { CHAR_B, BACK_SLASH, BACKSPACE, CARRIAGE_RETURN, CHAR_F, FORM_FEED, FWD_SLASH, CHAR_N, NEW_LINE, QUOTE, CHAR_R, CHAR_T, TAB, CHAR_U } from "../custom/chars";
17
2
  import { Sink } from "../custom/sink";
18
3
  import { unsafeCharCodeAt } from "../custom/util";
19
4
 
20
5
  // @ts-ignore: Decorator valid here
21
6
  @inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
22
- end = end || data.length - 1;
23
- let result = Sink.withCapacity(end - start - 1);
24
- let last = start + 1;
25
- for (let i = last; i < end; i++) {
26
- if (unsafeCharCodeAt(data, i) !== BACK_SLASH) {
27
- continue;
28
- }
29
- const char = unsafeCharCodeAt(data, ++i);
30
- result.write(data, last, i - 1);
31
- switch (char) {
32
- case QUOTE: {
33
- result.writeCodePoint(QUOTE);
34
- last = i + 1;
35
- break;
36
- }
37
- case BACK_SLASH: {
38
- result.writeCodePoint(BACK_SLASH);
39
- last = i + 1;
40
- break;
41
- }
42
- case FWD_SLASH: {
43
- result.writeCodePoint(FWD_SLASH);
44
- last = i + 1;
45
- break;
46
- }
47
- case CHAR_B: {
48
- result.writeCodePoint(BACKSPACE);
49
- last = i + 1;
50
- break;
51
- }
52
- case CHAR_F: {
53
- result.writeCodePoint(FORM_FEED);
54
- last = i + 1;
55
- break;
56
- }
57
- case CHAR_N: {
58
- result.writeCodePoint(NEW_LINE);
59
- last = i + 1;
60
- break;
61
- }
62
- case CHAR_R: {
63
- result.writeCodePoint(CARRIAGE_RETURN);
64
- last = i + 1;
65
- break;
66
- }
67
- case CHAR_T: {
68
- result.writeCodePoint(TAB);
69
- last = i + 1;
70
- break;
71
- }
72
- case CHAR_U: {
73
- const code = u16.parse(data.slice(i + 1, i + 5), 16);
74
- result.writeCodePoint(code);
75
- i += 4;
76
- last = i + 1;
77
- break;
78
- }
79
- default: {
80
- throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
81
- }
82
- }
7
+ end = end || data.length - 1;
8
+ let result = Sink.withCapacity(end - start - 1);
9
+ let last = start + 1;
10
+ for (let i = last; i < end; i++) {
11
+ if (unsafeCharCodeAt(data, i) !== BACK_SLASH) {
12
+ continue;
83
13
  }
84
- if (end > last) {
85
- result.write(data, last, end);
14
+ const char = unsafeCharCodeAt(data, ++i);
15
+ result.write(data, last, i - 1);
16
+ switch (char) {
17
+ case QUOTE: {
18
+ result.writeCodePoint(QUOTE);
19
+ last = i + 1;
20
+ break;
21
+ }
22
+ case BACK_SLASH: {
23
+ result.writeCodePoint(BACK_SLASH);
24
+ last = i + 1;
25
+ break;
26
+ }
27
+ case FWD_SLASH: {
28
+ result.writeCodePoint(FWD_SLASH);
29
+ last = i + 1;
30
+ break;
31
+ }
32
+ case CHAR_B: {
33
+ result.writeCodePoint(BACKSPACE);
34
+ last = i + 1;
35
+ break;
36
+ }
37
+ case CHAR_F: {
38
+ result.writeCodePoint(FORM_FEED);
39
+ last = i + 1;
40
+ break;
41
+ }
42
+ case CHAR_N: {
43
+ result.writeCodePoint(NEW_LINE);
44
+ last = i + 1;
45
+ break;
46
+ }
47
+ case CHAR_R: {
48
+ result.writeCodePoint(CARRIAGE_RETURN);
49
+ last = i + 1;
50
+ break;
51
+ }
52
+ case CHAR_T: {
53
+ result.writeCodePoint(TAB);
54
+ last = i + 1;
55
+ break;
56
+ }
57
+ case CHAR_U: {
58
+ const code = u16.parse(data.slice(i + 1, i + 5), 16);
59
+ result.writeCodePoint(code);
60
+ i += 4;
61
+ last = i + 1;
62
+ break;
63
+ }
64
+ default: {
65
+ throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
66
+ }
86
67
  }
87
- return result.toString()
68
+ }
69
+ if (end > last) {
70
+ result.write(data, last, end);
71
+ }
72
+ return result.toString();
88
73
  }
89
74
 
90
75
  // @ts-ignore: Decorator valid here
@@ -139,7 +124,7 @@ import { unsafeCharCodeAt } from "../custom/util";
139
124
  // break;
140
125
  // }
141
126
  // case CHAR_U: {
142
-
127
+
143
128
  // const code = u16.parse(data.slice(i + 1, i + 5), 16);
144
129
  // bs.w(code);
145
130
  // i += 4;
@@ -158,7 +143,7 @@ import { unsafeCharCodeAt } from "../custom/util";
158
143
 
159
144
  // @ts-ignore: Decorator valid here
160
145
  @inline export function deserializeString_Safe(data: string, start: i32 = 0, end: i32 = 0): string {
161
- const firstChar = load<u8>(changetype<usize>(data));
162
- if (firstChar != QUOTE) throw new Error("Mismatched Types! Expected string but got \""+data.slice(0, 100)+"\" instead!");
163
- return deserializeString(data, start, end);
164
- }
146
+ const firstChar = load<u8>(changetype<usize>(data));
147
+ if (firstChar != QUOTE) throw new Error('Mismatched Types! Expected string but got "' + data.slice(0, 100) + '" instead!');
148
+ return deserializeString(data, start, end);
149
+ }