Skip to content

Commit

Permalink
feat: alias field names
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Dec 19, 2023
1 parent 985e983 commit 0c236a8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Vec3 {

@json
class Player {
@alias("first name")
firstName!: string;
lastName!: string;
lastActive!: i32[];
Expand Down
10 changes: 6 additions & 4 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { JSON } from "./src/json";

// @ts-ignore

@serializable
class Vec3 {
x: f64 = 3.4;
y: f64 = 1.2;
z: f64 = 8.3;
}

// @ts-ignore
@json
@serializable
class Player extends Vec3 {
@alias("first name")
firstName: string;
lastName: string;
lastActive: Date;
Expand Down Expand Up @@ -47,7 +48,7 @@ console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));

console.log("Original: " + JSON.stringify(player));
//console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));
console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"first name":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));

@serializable
class Wrapper<T> {
Expand All @@ -56,6 +57,7 @@ class Wrapper<T> {

@serializable
class Foo {
@alias("ur mom")
foo!: string;
}

Expand All @@ -70,7 +72,7 @@ const foo: Wrapper<Foo> = {

foo.data.foo = "ha";
console.log(JSON.stringify(foo));
console.log(JSON.stringify(JSON.parse<Wrapper<Foo>>("{\"data\":{\"foo\":\"ha\"}}")))
console.log(JSON.stringify(JSON.parse<Wrapper<Foo>>("{\"data\":{\"ur mom\":\"ha\"}}")))
/*
// 9,325,755
bench("Stringify Object (Vec3)", () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-as",
"version": "0.6.6",
"version": "0.7.0",
"description": "JSON encoder/decoder for AssemblyScript",
"types": "assembly/index.ts",
"author": "Jairus Tanaka",
Expand Down
22 changes: 14 additions & 8 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AsJSONTransform extends BaseVisitor {
}
visitMethodDeclaration() { }
visitClassDeclaration(node) {
var _c;
var _c, _d;
const className = node.name.text;
if (!((_c = node.decorators) === null || _c === void 0 ? void 0 : _c.length))
return;
Expand Down Expand Up @@ -76,6 +76,12 @@ class AsJSONTransform extends BaseVisitor {
// @ts-ignore
let type = toString(member.type);
const name = member.name.text;
let aliasName = name;
if (member.decorators && ((_d = member.decorators[0]) === null || _d === void 0 ? void 0 : _d.name.text) === "alias") {
if (member.decorators[0] && member.decorators[0].args[0]) {
aliasName = member.decorators[0].args[0].value;
}
}
this.currentClass.keys.push(name);
// @ts-ignore
this.currentClass.types.push(type);
Expand All @@ -90,9 +96,9 @@ class AsJSONTransform extends BaseVisitor {
"u64",
"i64",
].includes(type.toLowerCase())) {
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
this.currentClass.encodeStmts.push(`"${aliasName}":\${this.${name}.toString()},`);
// @ts-ignore
this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
return;
}
Expand All @@ -106,9 +112,9 @@ class AsJSONTransform extends BaseVisitor {
"f32",
"f64",
].includes(type.toLowerCase())) {
this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
this.currentClass.encodeStmts.push(`"${aliasName}":\${this.${name}.toString()},`);
// @ts-ignore
this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
return;
}
Expand All @@ -118,9 +124,9 @@ class AsJSONTransform extends BaseVisitor {
}
}
else {
this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
this.currentClass.encodeStmts.push(`"${aliasName}":\${JSON.stringify<${type}>(this.${name})},`);
// @ts-ignore
this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
return;
}
Expand Down Expand Up @@ -178,7 +184,7 @@ class AsJSONTransform extends BaseVisitor {
const initializeMethod = SimpleParser.parseClassMember(initializeFunc, node);
node.members.push(initializeMethod);
this.schemasList.push(this.currentClass);
//console.log(toString(node));
console.log(toString(node));
}
visitSource(node) {
super.visitSource(node);
Expand Down
2 changes: 1 addition & 1 deletion transform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@json-as/transform",
"version": "0.6.6",
"version": "0.7.0",
"description": "JSON encoder/decoder for AssemblyScript",
"main": "./lib/index.js",
"author": "Jairus Tanaka",
Expand Down
20 changes: 13 additions & 7 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class AsJSONTransform extends BaseVisitor {
let type = toString(member.type);

const name = member.name.text;
let aliasName = name;
if (member.decorators && member.decorators[0]?.name.text === "alias") {
if (member.decorators[0] && member.decorators[0].args![0]) {
aliasName = member.decorators[0].args![0].value;
}
}
this.currentClass.keys.push(name);
// @ts-ignore
this.currentClass.types.push(type);
Expand All @@ -105,11 +111,11 @@ class AsJSONTransform extends BaseVisitor {
].includes(type.toLowerCase())
) {
this.currentClass.encodeStmts.push(
`"${name}":\${this.${name}.toString()},`
`"${aliasName}":\${this.${name}.toString()},`
);
// @ts-ignore
this.currentClass.setDataStmts.push(
`if (key.equals("${name}")) {
`if (key.equals("${aliasName}")) {
this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
return;
}
Expand All @@ -128,11 +134,11 @@ class AsJSONTransform extends BaseVisitor {
].includes(type.toLowerCase())
) {
this.currentClass.encodeStmts.push(
`"${name}":\${this.${name}.toString()},`
`"${aliasName}":\${this.${name}.toString()},`
);
// @ts-ignore
this.currentClass.setDataStmts.push(
`if (key.equals("${name}")) {
`if (key.equals("${aliasName}")) {
this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
return;
}
Expand All @@ -145,11 +151,11 @@ class AsJSONTransform extends BaseVisitor {
}
} else {
this.currentClass.encodeStmts.push(
`"${name}":\${JSON.stringify<${type}>(this.${name})},`
`"${aliasName}":\${JSON.stringify<${type}>(this.${name})},`
);
// @ts-ignore
this.currentClass.setDataStmts.push(
`if (key.equals("${name}")) {
`if (key.equals("${aliasName}")) {
this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
return;
}
Expand Down Expand Up @@ -220,7 +226,7 @@ class AsJSONTransform extends BaseVisitor {
node.members.push(initializeMethod);

this.schemasList.push(this.currentClass);
//console.log(toString(node));
console.log(toString(node));
}
visitSource(node: Source): void {
super.visitSource(node);
Expand Down

0 comments on commit 0c236a8

Please sign in to comment.