Fix missing comma after nested arrays and objects

This commit is contained in:
Evgeny Kuzyakov 2019-03-08 16:06:52 -08:00 committed by Vladimir Grichina
parent e564e7171c
commit b720d03687

View File

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