Merge pull request #3 from nearprotocol/better-stable

Fix missing comma after nested arrays and objects
This commit is contained in:
Vladimir Grichina 2019-03-08 16:14:10 -08:00 committed by GitHub
commit 1798ae8368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,9 +2,13 @@ declare function logStr(str: string): void;
declare function logF64(val: f64): void;
export class JSONEncoder {
private isFirstKey: boolean = true
private isFirstKey: bool[] = new Array<bool>(1);
private result: string[] = new Array<string>();
constructor() {
this.isFirstKey[0] = true;
}
serialize(): Uint8Array {
// TODO: Write directly to UTF8 bytes
let result = this.toString();
@ -43,30 +47,32 @@ export class JSONEncoder {
pushArray(name: string): bool {
this.writeKey(name);
this.write("[");
this.isFirstKey = true
this.isFirstKey.push(true);
return true;
}
popArray(): void {
this.write("]");
this.isFirstKey.pop();
}
pushObject(name: string): bool {
this.writeKey(name);
this.write("{");
this.isFirstKey = true
this.isFirstKey.push(true);
return true;
}
popObject(): void {
this.write("}");
this.isFirstKey.pop();
}
private writeKey(str: string): void {
if (!this.isFirstKey ) {
if (!this.isFirstKey[this.isFirstKey.length - 1]) {
this.write(",");
} else {
this.isFirstKey = false;
this.isFirstKey[this.isFirstKey.length - 1] = false;
}
if (str != null) {
this.writeString(str);
@ -116,4 +122,4 @@ export class JSONEncoder {
private write(str: string): void {
this.result.push(str);
}
}
}