diff --git a/assembly/encoder.ts b/assembly/encoder.ts index eba06e5..538641c 100644 --- a/assembly/encoder.ts +++ b/assembly/encoder.ts @@ -3,13 +3,13 @@ declare function logF64(val: f64): void; export class JSONEncoder { private isFirstKey: boolean = true - private inObject: Array = [false] - private result: string = "" + private result: string[] = new Array(); serialize(): Uint8Array { // TODO: Write directly to UTF8 bytes - let utf8ptr = this.result.toUTF8(); - let buffer = new Uint8Array(this.result.lengthUTF8); + let result = this.toString(); + let utf8ptr = result.toUTF8(); + let buffer = new Uint8Array(result.lengthUTF8); for (let i = 0; i < buffer.length; i++) { buffer[i] = load(utf8ptr + i); } @@ -17,7 +17,7 @@ export class JSONEncoder { } toString(): String { - return this.result; + return this.result.join(""); } setString(name: string, value: string): void { @@ -44,7 +44,6 @@ export class JSONEncoder { this.writeKey(name); this.write("["); this.isFirstKey = true - this.inObject.push(false); return true; } @@ -56,7 +55,6 @@ export class JSONEncoder { this.writeKey(name); this.write("{"); this.isFirstKey = true - this.inObject.push(true); return true; } @@ -116,6 +114,6 @@ export class JSONEncoder { } private write(str: string): void { - this.result += str; + this.result.push(str); } } \ No newline at end of file