Skip to content

Commit 9f3f730

Browse files
committed
fix: #154
1 parent c221ef4 commit 9f3f730

File tree

8 files changed

+285
-25
lines changed

8 files changed

+285
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## UNRELEASED
4+
5+
- fix: Type aliases should work across files [#154](https://github.com/JairusSW/json-as/issues/154)
6+
37
## 2025-08-14 - 1.1.21
48

59
- fix: JSON.parse on classes with enums [#155](https://github.com/JairusSW/json-as/issues/155)

assembly/test.tmp.ts

Lines changed: 197 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,197 @@
1-
import { JSON } from ".";
2-
const o = new JSON.Obj();
3-
o.set("schema", "http://json-schema.org/draft-07/schema#");
4-
o.set("additionalProperties", false);
5-
o.set("properties", new JSON.Obj());
6-
o.get("properties")!.as<JSON.Obj>().set("duration", new JSON.Obj());
7-
o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("default", 10);
8-
o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("description", "Duration of the operation in seconds");
9-
o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("type", "number");
10-
o.get("properties")!.as<JSON.Obj>().set("steps", new JSON.Obj());
11-
o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("default", 5);
12-
o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("description", "Number of steps in the operation");
13-
o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("type", "number");
14-
o.set("type", "object");
15-
console.log(o.toString());
16-
console.log((o.toString().length << 1).toString() + " == 596");
1+
import {
2+
bs
3+
} from "../lib/as-bs";
4+
import {
5+
JSON
6+
} from ".";
7+
import {
8+
Type2
9+
} from "./foo";
10+
@json
11+
class TypeAlias {
12+
baz: Type2 = "b";
13+
__SERIALIZE(ptr: usize): void {
14+
bs.proposeSize(16);
15+
store<u64>(bs.offset, 27303493649956987, 0);
16+
store<u32>(bs.offset, 2228346, 8);
17+
store<u16>(bs.offset, 58, 12);
18+
bs.offset += 14;
19+
JSON.__serialize<string>(load<string>(ptr, offsetof<this>("baz")));
20+
store<u16>(bs.offset, 125, 0);
21+
bs.offset += 2;
22+
}
23+
@inline
24+
__INITIALIZE(): this {
25+
store<string>(changetype<usize>(this), "b", offsetof<this>("baz"));
26+
return this;
27+
}
28+
__DESERIALIZE<__JSON_T>(srcStart: usize, srcEnd: usize, out: __JSON_T): __JSON_T {
29+
let keyStart: usize = 0;
30+
let keyEnd: usize = 0;
31+
let isKey = false;
32+
let depth: i32 = 0;
33+
let lastIndex: usize = 0;
34+
while (srcStart < srcEnd && JSON.Util.isSpace(load<u16>(srcStart))) srcStart += 2;
35+
while (srcEnd > srcStart && JSON.Util.isSpace(load<u16>(srcEnd - 2))) srcEnd -= 2;
36+
if (srcStart - srcEnd == 0) throw new Error("Input string had zero length or was all whitespace");
37+
;
38+
if (load<u16>(srcStart) != 123) throw new Error("Expected '{' at start of object at position " + (srcEnd - srcStart).toString());
39+
;
40+
if (load<u16>(srcEnd - 2) != 125) throw new Error("Expected '}' at end of object at position " + (srcEnd - srcStart).toString());
41+
;
42+
srcStart += 2;
43+
while (srcStart < srcEnd) {
44+
let code = load<u16>(srcStart);
45+
while (JSON.Util.isSpace(code)) code = load<u16>(srcStart += 2);
46+
if (keyStart == 0) {
47+
if (code == 34 && load<u16>(srcStart - 2) !== 92) {
48+
if (isKey) {
49+
keyStart = lastIndex;
50+
keyEnd = srcStart;
51+
while (JSON.Util.isSpace((code = load<u16>((srcStart += 2))))) {}
52+
if (code !== 58) throw new Error("Expected ':' after key at position " + (srcEnd - srcStart).toString());
53+
;
54+
isKey = false;
55+
} else {
56+
isKey = true;
57+
lastIndex = srcStart + 2;
58+
}
59+
}
60+
srcStart += 2;
61+
} else {
62+
if (code == 34) {
63+
lastIndex = srcStart;
64+
srcStart += 2;
65+
while (srcStart < srcEnd) {
66+
const code = load<u16>(srcStart);
67+
if (code == 34 && load<u16>(srcStart - 2) !== 92) {
68+
switch (<u32>keyEnd - <u32>keyStart) {
69+
case 6:
70+
{
71+
const code48 = load<u64>(keyStart) & 281474976710655;
72+
if (code48 == 523992367202) {
73+
store<string>(changetype<usize>(out), JSON.__deserialize<string>(lastIndex, srcStart + 2), offsetof<this>("baz"));
74+
srcStart += 4;
75+
keyStart = 0;
76+
break;
77+
} else {
78+
srcStart += 4;
79+
keyStart = 0;
80+
break;
81+
}
82+
}
83+
84+
default:
85+
{
86+
srcStart += 4;
87+
keyStart = 0;
88+
break;
89+
}
90+
91+
}
92+
break;
93+
}
94+
srcStart += 2;
95+
}
96+
} else if (code - 48 <= 9 || code == 45) {
97+
lastIndex = srcStart;
98+
srcStart += 2;
99+
while (srcStart < srcEnd) {
100+
const code = load<u16>(srcStart);
101+
if (code == 44 || code == 125 || JSON.Util.isSpace(code)) {
102+
srcStart += 2;
103+
keyStart = 0;
104+
break;
105+
}
106+
srcStart += 2;
107+
}
108+
} else if (code == 123) {
109+
lastIndex = srcStart;
110+
depth++;
111+
srcStart += 2;
112+
while (srcStart < srcEnd) {
113+
const code = load<u16>(srcStart);
114+
if (code == 34) {
115+
srcStart += 2;
116+
while (!(load<u16>(srcStart) == 34 && load<u16>(srcStart - 2) != 92)) srcStart += 2;
117+
} else if (code == 125) {
118+
if (--depth == 0) {
119+
srcStart += 2;
120+
switch (<u32>keyEnd - <u32>keyStart) {
121+
case 6:
122+
{
123+
const code48 = load<u64>(keyStart) & 281474976710655;
124+
if (code48 == 523992367202) {
125+
store<string>(changetype<usize>(out), JSON.__deserialize<string>(lastIndex, srcStart), offsetof<this>("baz"));
126+
keyStart = 0;
127+
break;
128+
} else {
129+
keyStart = 0;
130+
break;
131+
}
132+
}
133+
134+
default:
135+
{
136+
keyStart = 0;
137+
break;
138+
}
139+
140+
}
141+
break;
142+
}
143+
} else if (code == 123) depth++;
144+
;
145+
srcStart += 2;
146+
}
147+
} else if (code == 91) {
148+
lastIndex = srcStart;
149+
depth++;
150+
srcStart += 2;
151+
while (srcStart < srcEnd) {
152+
const code = load<u16>(srcStart);
153+
if (code == 34) {
154+
srcStart += 2;
155+
while (!(load<u16>(srcStart) == 34 && load<u16>(srcStart - 2) != 92)) srcStart += 2;
156+
} else if (code == 93) {
157+
if (--depth == 0) {
158+
srcStart += 2;
159+
keyStart = 0;
160+
break;
161+
}
162+
} else if (code == 91) depth++;
163+
;
164+
srcStart += 2;
165+
}
166+
} else if (code == 116) {
167+
if (load<u64>(srcStart) == 28429475166421108) {
168+
srcStart += 8;
169+
srcStart += 2;
170+
keyStart = 0;
171+
} else {
172+
throw new Error("Expected to find 'true' but found '" + JSON.Util.ptrToStr(lastIndex, srcStart) + "' instead at position " + (srcEnd - srcStart).toString());
173+
}
174+
} else if (code == 102) {
175+
if (load<u64>(srcStart, 2) == 28429466576093281) {
176+
srcStart += 10;
177+
srcStart += 2;
178+
keyStart = 0;
179+
} else {
180+
throw new Error("Expected to find 'false' but found '" + JSON.Util.ptrToStr(lastIndex, srcStart) + "' instead at position " + (srcEnd - srcStart).toString());
181+
}
182+
} else if (code == 110) {
183+
if (load<u64>(srcStart) == 30399761348886638) {
184+
srcStart += 8;
185+
srcStart += 2;
186+
keyStart = 0;
187+
}
188+
} else {
189+
srcStart += 2;
190+
keyStart = 0;
191+
}
192+
}
193+
}
194+
return out;
195+
}
196+
}
197+
console.log(JSON.stringify(new TypeAlias()));

transform/lib/index.js

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transform/lib/types.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)