diff --git a/CHANGELOG b/CHANGELOG index 8871418..1d29bb4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ v0.8.2 - Properties starting with `static` or `private` would be ignored v0.8.3 - Dirty fix to issue #68. Add __JSON_Stringify callable to global scope. -v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice. \ No newline at end of file +v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice. +v0.8.5 - Fix #73. Support for nullable primatives with Box from as-container +v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5 \ No newline at end of file diff --git a/README.md b/README.md index 6ab55ed..6770015 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ██║ ██║███████║ ╚█████╔╝███████║╚██████╔╝██║ ╚████║ ╚═╝ ╚═╝╚══════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═══╝ -v0.8.5 +v0.8.6 diff --git a/assembly/__tests__/as-json.spec.ts b/assembly/__tests__/as-json.spec.ts index 6a85941..7a29cb7 100644 --- a/assembly/__tests__/as-json.spec.ts +++ b/assembly/__tests__/as-json.spec.ts @@ -53,6 +53,8 @@ class Vec3 { x: f64; y: f64; z: f64; + + static shouldIgnore: string = "should not be serialized"; } @json diff --git a/package.json b/package.json index 8722f5c..a861d7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-as", - "version": "0.8.5", + "version": "0.8.6", "description": "JSON encoder/decoder for AssemblyScript", "types": "assembly/index.ts", "author": "Jairus Tanaka", diff --git a/transform/lib/index.js b/transform/lib/index.js index 79a606f..5c6c5f9 100644 --- a/transform/lib/index.js +++ b/transform/lib/index.js @@ -22,7 +22,7 @@ class AsJSONTransform extends BaseVisitor { } visitMethodDeclaration() { } visitClassDeclaration(node) { - var _c, _d, _e, _f, _g, _h, _j, _k, _l; + var _c, _d; const className = node.name.text; if (!((_c = node.decorators) === null || _c === void 0 ? void 0 : _c.length)) return; @@ -80,96 +80,76 @@ class AsJSONTransform extends BaseVisitor { const member = mem; const lineText = toString(member); //console.log("Member: " + lineText) - if (lineText.startsWith("private ") && lineText.startsWith("static ")) - continue; - const omitnull = (_d = member.decorators) === null || _d === void 0 ? void 0 : _d.find(v => v.name.text == "omitnull"); - console.log((_e = member.decorators) === null || _e === void 0 ? void 0 : _e.find(v => v.name.text === "omitif")); - const omitif = (_k = (_j = (_h = (_g = (_f = member.decorators) === null || _f === void 0 ? void 0 : _f.find(v => v.name.text === "omitif")) === null || _g === void 0 ? void 0 : _g.args) === null || _h === void 0 ? void 0 : _h[0]) === null || _j === void 0 ? void 0 : _j.value) !== null && _k !== void 0 ? _k : null; - // @ts-ignore - let type = toString(member.type); - const name = member.name.text; - let aliasName = name; - // @ts-ignore - if (member.decorators && ((_l = member.decorators[0]) === null || _l === void 0 ? void 0 : _l.name.text) === "alias") { - if (member.decorators[0] && member.decorators[0].args[0]) { - // @ts-ignore - aliasName = member.decorators[0].args[0].value; - } - } - this.currentClass.keys.push(name); - // @ts-ignore - this.currentClass.types.push(type); - // @ts-ignore - if ([ - "u8", - "i8", - "u16", - "i16", - "u32", - "i32", - "u64", - "i64", - ].includes(type.toLowerCase())) { - if (omitif) { - this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`); - } - else { - this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${this.${name}}`); + if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) { + // @ts-ignore + let type = toString(member.type); + const name = member.name.text; + let aliasName = name; + // @ts-ignore + 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]) { + // @ts-ignore + aliasName = member.decorators[0].args[0].value; + } } + this.currentClass.keys.push(name); + // @ts-ignore + this.currentClass.types.push(type); // @ts-ignore - this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { + if ([ + "u8", + "i8", + "u16", + "i16", + "u32", + "i32", + "u64", + "i64", + ].includes(type.toLowerCase())) { + this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`); + // @ts-ignore + this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1); return; }`); - if (member.initializer) { - this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); + if (member.initializer) { + this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); + } } - } - else if ([ - "f32", - "f64", - ].includes(type.toLowerCase())) { - if (omitif) { - this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`); - } - else { - this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${this.${name}}`); - } - // @ts-ignore - this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { + else // @ts-ignore + if ([ + "f32", + "f64", + ].includes(type.toLowerCase())) { + this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`); + // @ts-ignore + this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues); return; }`); - if (member.initializer) { - this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); - } - } - else { - if (omitnull) { - this.currentClass.encodeStmts.push(`c\${changetype(this.${name}) !== 0 ? "" : \`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}\`}`); - console.log(`\${changetype(this.${name}) != 0 ? \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\` : ""}`); - } - else if (omitif) { - this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\`}`); + if (member.initializer) { + this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); + } } else { - this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}`); - } - // @ts-ignore - this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { + this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},`); + // @ts-ignore + this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues); return; }`); - if (member.initializer) { - this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); + if (member.initializer) { + this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`); + } } } } } let serializeFunc = ""; if (this.currentClass.encodeStmts.length > 0) { - const stmt = this.currentClass.encodeStmts[0]; - this.currentClass.encodeStmts[0] = stmt.slice(1); + const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1]; + this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] = + stmt.slice(0, stmt.length - 1); serializeFunc = ` __JSON_Serialize(): string { return \`{${this.currentClass.encodeStmts.join("")}}\`; @@ -208,7 +188,7 @@ class AsJSONTransform extends BaseVisitor { this.schemasList.push(this.currentClass); this.sources.add(node.name.range.source); // Uncomment to see the generated code for debugging. - console.log(serializeFunc); + //console.log(serializeFunc); //console.log(setKeyFunc); //console.log(initializeFunc); } diff --git a/transform/package.json b/transform/package.json index da99ba9..d94497c 100644 --- a/transform/package.json +++ b/transform/package.json @@ -1,6 +1,6 @@ { "name": "@json-as/transform", - "version": "0.8.5", + "version": "0.8.6", "description": "JSON encoder/decoder for AssemblyScript", "main": "./lib/index.js", "author": "Jairus Tanaka", diff --git a/transform/src/index.ts b/transform/src/index.ts index 0c88e6c..eb8e023 100644 --- a/transform/src/index.ts +++ b/transform/src/index.ts @@ -95,116 +95,91 @@ class AsJSONTransform extends BaseVisitor { const lineText = toString(member); //console.log("Member: " + lineText) - if (lineText.startsWith("private ") && lineText.startsWith("static ")) continue; + if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) { - const omitnull = member.decorators?.find(v => v.name.text == "omitnull"); - console.log(member.decorators?.find(v => v.name.text === "omitif")) - const omitif = member.decorators?.find(v => v.name.text === "omitif")?.args?.[0]?.value ?? null; - - // @ts-ignore - let type = toString(member.type); + // @ts-ignore + let type = toString(member.type); - const name = member.name.text; - let aliasName = name; + const name = member.name.text; + let aliasName = name; - // @ts-ignore - if (member.decorators && member.decorators[0]?.name.text === "alias") { - if (member.decorators[0] && member.decorators[0].args![0]) { - // @ts-ignore - aliasName = member.decorators[0].args![0].value; + // @ts-ignore + if (member.decorators && member.decorators[0]?.name.text === "alias") { + if (member.decorators[0] && member.decorators[0].args![0]) { + // @ts-ignore + aliasName = member.decorators[0].args![0].value; + } } - } - this.currentClass.keys.push(name); - // @ts-ignore - this.currentClass.types.push(type); - // @ts-ignore - if ( - [ - "u8", - "i8", - "u16", - "i16", - "u32", - "i32", - "u64", - "i64", - ].includes(type.toLowerCase()) - ) { - if (omitif) { - this.currentClass.encodeStmts.push( - `\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}` - ); - } else { + this.currentClass.keys.push(name); + // @ts-ignore + this.currentClass.types.push(type); + // @ts-ignore + if ( + [ + "u8", + "i8", + "u16", + "i16", + "u32", + "i32", + "u64", + "i64", + ].includes(type.toLowerCase()) + ) { this.currentClass.encodeStmts.push( - `,${encodeKey(aliasName)}:\${this.${name}}` + `${encodeKey(aliasName)}:\${this.${name}},` ); - } - // @ts-ignore - this.currentClass.setDataStmts.push( - `if (key.equals(${JSON.stringify(aliasName)})) { + // @ts-ignore + this.currentClass.setDataStmts.push( + `if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1); return; }` - ); - if (member.initializer) { - this.currentClass.initializeStmts.push( - `this.${name} = ${toString(member.initializer)}` - ); - } - } else if ( - [ - "f32", - "f64", - ].includes(type.toLowerCase()) - ) { - if (omitif) { - this.currentClass.encodeStmts.push( - `\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}` ); - } else { - this.currentClass.encodeStmts.push( - `,${encodeKey(aliasName)}:\${this.${name}}` - ); - } - // @ts-ignore - this.currentClass.setDataStmts.push( - `if (key.equals(${JSON.stringify(aliasName)})) { + if (member.initializer) { + this.currentClass.initializeStmts.push( + `this.${name} = ${toString(member.initializer)}` + ); + } + } else // @ts-ignore + if ( + [ + "f32", + "f64", + ].includes(type.toLowerCase()) + ) { + this.currentClass.encodeStmts.push( + `${encodeKey(aliasName)}:\${this.${name}},` + ); + // @ts-ignore + this.currentClass.setDataStmts.push( + `if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues); return; }` - ); - if (member.initializer) { - this.currentClass.initializeStmts.push( - `this.${name} = ${toString(member.initializer)}` - ); - } - } else { - if (omitnull) { - this.currentClass.encodeStmts.push( - `${comma}\${changetype(this.${name}) !== 0 ? "" : \`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}\`}` - ); - console.log(`\${changetype(this.${name}) != 0 ? \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\` : ""}`) - } else if (omitif) { - this.currentClass.encodeStmts.push( - `\${${omitif} ? "" : \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\`}` - ); - } else { - this.currentClass.encodeStmts.push( - `,${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}` - ); - } - // @ts-ignore - this.currentClass.setDataStmts.push( - `if (key.equals(${JSON.stringify(aliasName)})) { + ); + if (member.initializer) { + this.currentClass.initializeStmts.push( + `this.${name} = ${toString(member.initializer)}` + ); + } + } else { + this.currentClass.encodeStmts.push( + `${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},` + ); + // @ts-ignore + this.currentClass.setDataStmts.push( + `if (key.equals(${JSON.stringify(aliasName)})) { this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues); return; }` - ); - if (member.initializer) { - this.currentClass.initializeStmts.push( - `this.${name} = ${toString(member.initializer)}` - ); - } + ); + if (member.initializer) { + this.currentClass.initializeStmts.push( + `this.${name} = ${toString(member.initializer)}` + ); + } + } } } } @@ -213,8 +188,11 @@ class AsJSONTransform extends BaseVisitor { if (this.currentClass.encodeStmts.length > 0) { const stmt = - this.currentClass.encodeStmts[0]!; - this.currentClass.encodeStmts[0] = stmt!.slice(1); + this.currentClass.encodeStmts[ + this.currentClass.encodeStmts.length - 1 + ]!; + this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] = + stmt!.slice(0, stmt.length - 1); serializeFunc = ` __JSON_Serialize(): string { return \`{${this.currentClass.encodeStmts.join("")}}\`; @@ -258,7 +236,7 @@ class AsJSONTransform extends BaseVisitor { this.sources.add(node.name.range.source); // Uncomment to see the generated code for debugging. - console.log(serializeFunc); + //console.log(serializeFunc); //console.log(setKeyFunc); //console.log(initializeFunc); } @@ -331,4 +309,4 @@ export default class Transformer extends Transform { } } } -} +} \ No newline at end of file