Dump transformations only if Serializable.enableDump is true.

This commit is contained in:
László Monda
2016-04-07 02:51:21 +02:00
parent 9645d70df8
commit 56aa1982b1

View File

@@ -6,6 +6,7 @@ abstract class Serializable<T> {
private static depth = 0; private static depth = 0;
private static maxDisplayedJsonLength = 160; private static maxDisplayedJsonLength = 160;
private static enableDump = true;
strintifyJsObject(jsObject: any): string { strintifyJsObject(jsObject: any): string {
let json = JSON.stringify(jsObject); let json = JSON.stringify(jsObject);
@@ -17,12 +18,12 @@ abstract class Serializable<T> {
fromJsObject(jsObject: any): Serializable<T> { fromJsObject(jsObject: any): Serializable<T> {
let indentation = new Array(Serializable.depth + 1).join(' '); let indentation = new Array(Serializable.depth + 1).join(' ');
let isArray = this instanceof ClassArray; let isArray = this instanceof ClassArray;
process.stdout.write(`${indentation}${this.constructor.name}.fromJsObject: ${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `)); this.dump(`${indentation}${this.constructor.name}.fromJsObject: ${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `));
Serializable.depth++; Serializable.depth++;
let value = this._fromJsObject(jsObject); let value = this._fromJsObject(jsObject);
Serializable.depth--; Serializable.depth--;
if (!isArray) { if (!isArray) {
process.stdout.write(`${value}\n`); this.dump(`${value}\n`);
} }
return value; return value;
} }
@@ -30,14 +31,14 @@ abstract class Serializable<T> {
fromBinary(buffer: UhkBuffer): Serializable<T> { fromBinary(buffer: UhkBuffer): Serializable<T> {
let indentation = new Array(Serializable.depth + 1).join(' '); let indentation = new Array(Serializable.depth + 1).join(' ');
let isArray = this instanceof ClassArray; let isArray = this instanceof ClassArray;
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary: [`); this.dump(`${indentation}${this.constructor.name}.fromBinary: [`);
Serializable.depth++; Serializable.depth++;
buffer.enableDump = true; buffer.enableDump = Serializable.enableDump;
let value = this._fromBinary(buffer); let value = this._fromBinary(buffer);
buffer.enableDump = false; buffer.enableDump = false;
Serializable.depth--; Serializable.depth--;
if (!isArray) { if (!isArray) {
process.stdout.write(`] => ${value}\n`); this.dump(`] => ${value}\n`);
} }
return value; return value;
} }
@@ -45,12 +46,12 @@ abstract class Serializable<T> {
toJsObject(): any { toJsObject(): any {
let indentation = new Array(Serializable.depth + 1).join(' '); let indentation = new Array(Serializable.depth + 1).join(' ');
let isArray = this instanceof ClassArray; let isArray = this instanceof ClassArray;
process.stdout.write(`${indentation}${this.constructor.name}.toJsObject: ${this}` + (isArray ? '\n' : ` => `)); this.dump(`${indentation}${this.constructor.name}.toJsObject: ${this}` + (isArray ? '\n' : ` => `));
Serializable.depth++; Serializable.depth++;
let value = this._toJsObject(); let value = this._toJsObject();
Serializable.depth--; Serializable.depth--;
if (!isArray) { if (!isArray) {
process.stdout.write(`${this.strintifyJsObject(value)}\n`); this.dump(`${this.strintifyJsObject(value)}\n`);
} }
return value; return value;
} }
@@ -58,14 +59,14 @@ abstract class Serializable<T> {
toBinary(buffer: UhkBuffer): void { toBinary(buffer: UhkBuffer): void {
let indentation = new Array(Serializable.depth + 1).join(' '); let indentation = new Array(Serializable.depth + 1).join(' ');
let isArray = this instanceof ClassArray; let isArray = this instanceof ClassArray;
process.stdout.write(`${indentation}${this.constructor.name}.toBinary: ${this} => ['`); this.dump(`${indentation}${this.constructor.name}.toBinary: ${this} => ['`);
Serializable.depth++; Serializable.depth++;
buffer.enableDump = true; buffer.enableDump = Serializable.enableDump;
let value = this._toBinary(buffer); let value = this._toBinary(buffer);
buffer.enableDump = false; buffer.enableDump = false;
Serializable.depth--; Serializable.depth--;
if (!isArray) { if (!isArray) {
process.stdout.write(`]\n`); this.dump(`]\n`);
} }
return value; return value;
} }
@@ -74,4 +75,10 @@ abstract class Serializable<T> {
abstract _fromBinary(buffer: UhkBuffer): Serializable<T>; abstract _fromBinary(buffer: UhkBuffer): Serializable<T>;
abstract _toJsObject(): any; abstract _toJsObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract _toBinary(buffer: UhkBuffer): void;
dump(value) {
if (Serializable.enableDump) {
process.stdout.write(value);
}
}
} }