Comparing version
@@ -43,3 +43,3 @@ import { JSON } from ".."; | ||
canSerde<i64>(-101); | ||
/* | ||
canSerde<u128>(u128.from("0")) | ||
@@ -53,3 +53,3 @@ canSerde<u128>(u128.from("100")) | ||
/* canSerde<u256>(u256.fromU128(u128.from("0"))) | ||
canSerde<u256>(u256.fromU128(u128.from("0"))) | ||
canSerde<u256>(u256.fromU128(u128.from("100"))) | ||
@@ -70,5 +70,5 @@ canSerde<u256>(u256.fromU128(u128.from("101"))) | ||
canSerde<i128Safe>(i128Safe.from("-100")) | ||
canSerde<i128Safe>(i128Safe.from("-101")) | ||
canSerde<i128Safe>(i128Safe.from("-101"))*/ | ||
canSerde<i256Safe>(new i256Safe(10, 11, 500, 501))*/ | ||
//canSerde<i256Safe>(new i256Safe(10, 11, 500, 501)) | ||
}); | ||
@@ -75,0 +75,0 @@ |
@@ -168,4 +168,4 @@ import { u128, u128Safe, u256, u256Safe, i128, i128Safe, i256Safe } from "as-bignum/assembly"; | ||
@inline | ||
// @ts-ignore | ||
function parseBigNum<T>(data: string): T { | ||
// @ts-ignore | ||
function parseBigNum<T>(data: string): T { | ||
// @ts-ignore | ||
@@ -184,3 +184,3 @@ if (idof<T>() == idof<u128>()) return u128.fromString(data); | ||
// @ts-ignore | ||
if (idof<T>() == idof<i256Safe>()) return i256Safe.fromString(data); | ||
//if (idof<T>() == idof<i256Safe>()) return data. | ||
} | ||
@@ -190,3 +190,3 @@ | ||
@inline | ||
function parseString(data: string): string { | ||
function parseString(data: string): string { | ||
return data.slice(1, data.length - 1).replaceAll('\\"', '"'); | ||
@@ -197,3 +197,3 @@ } | ||
@inline | ||
function parseBoolean<T extends boolean>(data: string): T { | ||
function parseBoolean<T extends boolean>(data: string): T { | ||
if (data.length > 3 && data.startsWith("true")) return <T>true; | ||
@@ -206,3 +206,3 @@ else if (data.length > 4 && data.startsWith("false")) return <T>false; | ||
@inline | ||
function parseNumber<T>(data: string): T { | ||
function parseNumber<T>(data: string): T { | ||
let type: T; | ||
@@ -229,6 +229,6 @@ // @ts-ignore | ||
else if (type instanceof i8) return I8.parseInt(data); | ||
else | ||
throw new Error( | ||
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.` | ||
); | ||
throw new Error( | ||
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.` | ||
); | ||
} | ||
@@ -238,3 +238,3 @@ | ||
@inline | ||
export function parseObject<T>(data: string): T { | ||
export function parseObject<T>(data: string): T { | ||
let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())); | ||
@@ -261,2 +261,3 @@ let key = ""; | ||
++arrayValueIndex; | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, arrayValueIndex)); | ||
@@ -282,2 +283,3 @@ outerLoopIndex = arrayValueIndex; | ||
++objectValueIndex; | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, objectValueIndex)); | ||
@@ -305,2 +307,3 @@ outerLoopIndex = objectValueIndex; | ||
} else { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, stringValueIndex)); | ||
@@ -314,2 +317,3 @@ isKey = false; | ||
} else if (char == nCode) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, nullWord); | ||
@@ -323,2 +327,3 @@ isKey = false; | ||
) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, trueWord); | ||
@@ -333,2 +338,3 @@ isKey = false; | ||
) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, "false"); | ||
@@ -341,2 +347,3 @@ isKey = false; | ||
if (char === commaCode || char === rightBraceCode || isSpace(char)) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data.slice(outerLoopIndex - 1, numberValueIndex)); | ||
@@ -355,4 +362,4 @@ outerLoopIndex = numberValueIndex; | ||
@inline | ||
// @ts-ignore | ||
export function parseArray<T extends unknown[]>(data: string): T { | ||
// @ts-ignore | ||
export function parseArray<T extends unknown[]>(data: string): T { | ||
let type!: valueof<T>; | ||
@@ -379,3 +386,3 @@ if (type instanceof String) { | ||
@inline | ||
export function parseStringArray(data: string): string[] { | ||
export function parseStringArray(data: string): string[] { | ||
const result: string[] = []; | ||
@@ -400,3 +407,3 @@ let lastPos = 0; | ||
@inline | ||
export function parseBooleanArray<T extends boolean[]>(data: string): T { | ||
export function parseBooleanArray<T extends boolean[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -429,3 +436,3 @@ let lastPos = 1; | ||
@inline | ||
export function parseNumberArray<T extends number[]>(data: string): T { | ||
export function parseNumberArray<T extends number[]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -456,3 +463,3 @@ let lastPos = 0; | ||
@inline | ||
export function parseArrayArray<T extends unknown[][]>(data: string): T { | ||
export function parseArrayArray<T extends unknown[][]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -487,3 +494,3 @@ let char = 0; | ||
@inline | ||
export function parseObjectArray<T extends unknown[][]>(data: string): T { | ||
export function parseObjectArray<T extends unknown[][]>(data: string): T { | ||
const result = instantiate<T>(); | ||
@@ -490,0 +497,0 @@ let char = 0; |
@@ -1,2 +0,2 @@ | ||
import "wasi"; | ||
import { wasi_console } from "@assemblyscript/wasi-shim/assembly/wasi_console" | ||
import { u128 } from "as-bignum/assembly"; | ||
@@ -51,4 +51,4 @@ import { | ||
const serializedPlayer = JSON.stringify<Player>(player); | ||
console.log("Serialized Player: " + serializedPlayer); | ||
wasi_console.log("Serialized Player: " + serializedPlayer); | ||
const deserializedPlayer = JSON.parse<Player>(serializedPlayer); | ||
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); | ||
wasi_console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer)); |
{ | ||
"name": "json-as", | ||
"version": "0.5.6", | ||
"version": "0.5.7", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -14,3 +14,3 @@ "types": "assembly/index.ts", | ||
"bench:astral": "astral", | ||
"build:test": "asc assembly/test.ts --target test", | ||
"build:test": "asc assembly/test.ts --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json --transform \"./transform\" --exportStart _start -o ./build/test.wasm", | ||
"build:transform": "tsc -p ./transform", | ||
@@ -26,4 +26,5 @@ "test:wasmtime": "wasmtime ./build/test.wasm", | ||
"@assemblyscript/loader": "^0.21.3", | ||
"@assemblyscript/wasi-shim": "^0.1.0", | ||
"@serial-as/json": "^2.0.0", | ||
"assemblyscript": "^0.20.7", | ||
"assemblyscript": "^0.24.1", | ||
"assemblyscript-prettier": "^1.0.2", | ||
@@ -30,0 +31,0 @@ "prettier": "^2.7.1", |
import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js"; | ||
import { getName, toString } from "visitor-as/dist/utils.js"; | ||
import { SimpleParser } from "visitor-as/dist/index.js"; | ||
class SchemaData { | ||
constructor() { | ||
this.keys = []; | ||
this.values = []; | ||
this.types = []; | ||
this.name = ""; | ||
this.parent = ""; | ||
this.encodeStmts = []; | ||
this.setDataStmts = []; | ||
} | ||
} | ||
class AsJSONTransform extends ClassDecorator { | ||
constructor() { | ||
super(...arguments); | ||
this.schemasList = []; | ||
this.sources = []; | ||
this.encodeStmts = []; | ||
//public decodeStmts: string[] = []; | ||
this.setDataStmts = []; | ||
} | ||
//public checkDecodeStmts: string[] = []; | ||
visitMethodDeclaration() { } | ||
@@ -24,3 +32,3 @@ visitFieldDeclaration(node) { | ||
// @ts-ignore | ||
this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`); | ||
this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`); | ||
// @ts-ignore | ||
@@ -31,3 +39,3 @@ //this.decodeStmts.push( | ||
// @ts-ignore | ||
this.setDataStmts.push(`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) { | ||
this.currentClass.setDataStmts.push(`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) { | ||
this.${name} = JSON.parseObjectValue<${type}>(value); | ||
@@ -43,15 +51,39 @@ return; | ||
visitClassDeclaration(node) { | ||
var _a, _b, _c; | ||
if (!node.members) { | ||
return; | ||
} | ||
this.currentClass = node; | ||
this.currentClass = { | ||
name: toString(node.name), | ||
keys: [], | ||
values: [], | ||
types: [], | ||
parent: node.extendsType ? toString(node.extendsType) : "", | ||
node: node, | ||
encodeStmts: [], | ||
setDataStmts: [] | ||
}; | ||
if (this.currentClass.parent.length > 0) { | ||
const parentSchema = this.schemasList.map((v) => { | ||
if (v.name == this.currentClass.parent) { | ||
return v; | ||
} | ||
}); | ||
if (parentSchema.length > 0) { | ||
(_a = parentSchema[0]) === null || _a === void 0 ? void 0 : _a.encodeStmts.push(((_b = parentSchema[0]) === null || _b === void 0 ? void 0 : _b.encodeStmts.pop()) + ","); | ||
this.currentClass.encodeStmts.push(...(_c = parentSchema[0]) === null || _c === void 0 ? void 0 : _c.encodeStmts); | ||
} | ||
else { | ||
//console.log("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?") | ||
} | ||
} | ||
this.visit(node.members); | ||
const serializedProp = '__JSON_Serialized: string = "";'; | ||
let serializeFunc = ""; | ||
if (this.encodeStmts.length > 0) { | ||
const stmt = this.encodeStmts[this.encodeStmts.length - 1]; | ||
this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1); | ||
if (this.currentClass.encodeStmts.length > 0) { | ||
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.encodeStmts.join("")}}\`; | ||
return \`{${this.currentClass.encodeStmts.join("")}}\`; | ||
} | ||
@@ -67,17 +99,2 @@ `; | ||
} | ||
/*const deserializeFunc = ` | ||
__JSON_Deserialize<T>(values: Map<string, string>): T { | ||
${ | ||
process.argv.includes("--debugJSON") | ||
? this.checkDecodeStmts.join("else") | ||
: "" | ||
} | ||
return { | ||
${ | ||
// @ts-ignore | ||
this.decodeStmts.join("") | ||
} | ||
} | ||
} | ||
`;*/ | ||
const setKeyFunc = ` | ||
@@ -87,10 +104,6 @@ __JSON_Set_Key(key: string, value: string): void { | ||
// @ts-ignore | ||
this.setDataStmts.join("")} | ||
this.currentClass.setDataStmts.join("")} | ||
} | ||
`; | ||
//console.log(setKeyFunc, deserializeFunc, serializeFunc) | ||
this.encodeStmts = []; | ||
//this.decodeStmts = []; | ||
this.setDataStmts = []; | ||
//this.checkDecodeStmts = []; | ||
//console.log(serializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember(serializedProp, node); | ||
@@ -100,9 +113,5 @@ node.members.push(serializedProperty); | ||
node.members.push(serializeMethod); | ||
//const deserializeMethod = SimpleParser.parseClassMember( | ||
// deserializeFunc, | ||
// node | ||
//); | ||
//node.members.push(deserializeMethod); | ||
const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node); | ||
node.members.push(setDataMethod); | ||
this.schemasList.push(this.currentClass); | ||
} | ||
@@ -109,0 +118,0 @@ get name() { |
{ | ||
"name": "@json-as/transform", | ||
"version": "0.5.6", | ||
"version": "0.5.7", | ||
"description": "JSON encoder/decoder for AssemblyScript", | ||
@@ -13,3 +13,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"visitor-as": "^0.10.2" | ||
"visitor-as": "^0.11.3" | ||
}, | ||
@@ -16,0 +16,0 @@ "repository": { |
@@ -13,11 +13,18 @@ import { | ||
class SchemaData { | ||
public keys: string[] = []; | ||
public values: string[] = []; | ||
public types: string[] = []; | ||
public name: string = ""; | ||
public parent: string = ""; | ||
public node!: ClassDeclaration; | ||
public encodeStmts: string[] = []; | ||
public setDataStmts: string[] = []; | ||
} | ||
class AsJSONTransform extends ClassDecorator { | ||
public currentClass!: ClassDeclaration; | ||
public schemasList: SchemaData[] = []; | ||
public currentClass!: SchemaData; | ||
public sources: Source[] = []; | ||
public encodeStmts: string[] = []; | ||
//public decodeStmts: string[] = []; | ||
public setDataStmts: string[] = []; | ||
//public checkDecodeStmts: string[] = []; | ||
visitMethodDeclaration(): void {} | ||
visitMethodDeclaration(): void { } | ||
visitFieldDeclaration(node: FieldDeclaration): void { | ||
@@ -33,3 +40,3 @@ const lineText = toString(node); | ||
// @ts-ignore | ||
this.encodeStmts.push( | ||
this.currentClass.encodeStmts.push( | ||
`"${name}":\${JSON.stringify<${type}>(this.${name})},` | ||
@@ -40,7 +47,7 @@ ); | ||
//this.decodeStmts.push( | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
// `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n` | ||
//); | ||
// @ts-ignore | ||
this.setDataStmts.push( | ||
this.currentClass.setDataStmts.push( | ||
`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) { | ||
@@ -63,3 +70,26 @@ this.${name} = JSON.parseObjectValue<${type}>(value); | ||
this.currentClass = node; | ||
this.currentClass = { | ||
name: toString(node.name), | ||
keys: [], | ||
values: [], | ||
types: [], | ||
parent: node.extendsType ? toString(node.extendsType) : "", | ||
node: node, | ||
encodeStmts: [], | ||
setDataStmts: [] | ||
} | ||
if (this.currentClass.parent.length > 0) { | ||
const parentSchema = this.schemasList.map((v) => { | ||
if (v.name == this.currentClass.parent) { | ||
return v; | ||
} | ||
}); | ||
if (parentSchema.length > 0) { | ||
parentSchema[0]?.encodeStmts.push(parentSchema[0]?.encodeStmts.pop() + ",") | ||
this.currentClass.encodeStmts.push(...parentSchema[0]?.encodeStmts) | ||
} else { | ||
//console.log("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?") | ||
} | ||
} | ||
@@ -72,5 +102,5 @@ this.visit(node.members); | ||
if (this.encodeStmts.length > 0) { | ||
const stmt = this.encodeStmts[this.encodeStmts.length - 1]!; | ||
this.encodeStmts[this.encodeStmts.length - 1] = stmt!.slice( | ||
if (this.currentClass.encodeStmts.length > 0) { | ||
const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1]!; | ||
this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] = stmt!.slice( | ||
0, | ||
@@ -81,3 +111,3 @@ stmt.length - 1 | ||
__JSON_Serialize(): string { | ||
return \`{${this.encodeStmts.join("")}}\`; | ||
return \`{${this.currentClass.encodeStmts.join("")}}\`; | ||
} | ||
@@ -92,30 +122,12 @@ `; | ||
} | ||
/*const deserializeFunc = ` | ||
__JSON_Deserialize<T>(values: Map<string, string>): T { | ||
${ | ||
process.argv.includes("--debugJSON") | ||
? this.checkDecodeStmts.join("else") | ||
: "" | ||
} | ||
return { | ||
${ | ||
// @ts-ignore | ||
this.decodeStmts.join("") | ||
} | ||
} | ||
} | ||
`;*/ | ||
const setKeyFunc = ` | ||
__JSON_Set_Key(key: string, value: string): void { | ||
${ | ||
// @ts-ignore | ||
this.setDataStmts.join("") | ||
} | ||
// @ts-ignore | ||
this.currentClass.setDataStmts.join("") | ||
} | ||
} | ||
` | ||
//console.log(setKeyFunc, deserializeFunc, serializeFunc) | ||
this.encodeStmts = []; | ||
//this.decodeStmts = []; | ||
this.setDataStmts = []; | ||
//this.checkDecodeStmts = []; | ||
//console.log(serializeFunc) | ||
const serializedProperty = SimpleParser.parseClassMember( | ||
@@ -130,7 +142,2 @@ serializedProp, | ||
//const deserializeMethod = SimpleParser.parseClassMember( | ||
// deserializeFunc, | ||
// node | ||
//); | ||
//node.members.push(deserializeMethod); | ||
const setDataMethod = SimpleParser.parseClassMember( | ||
@@ -141,2 +148,4 @@ setKeyFunc, | ||
node.members.push(setDataMethod); | ||
this.schemasList.push(this.currentClass); | ||
} | ||
@@ -143,0 +152,0 @@ get name(): string { |
48946
-19.07%9
12.5%22
-4.35%1224
-25.46%