No need to turn the JS object into a string and back again.

This commit is contained in:
Stefan Thomas
2013-08-23 15:50:15 -07:00
parent d5fb086b51
commit 74da0c8a52

View File

@@ -116,13 +116,12 @@ var TRANSACTION_TYPES = {
SerializedObject.prototype.to_json = function() { SerializedObject.prototype.to_json = function() {
var old_pointer = this.pointer; var old_pointer = this.pointer;
this.resetPointer(); this.resetPointer();
var output = "{"; var output = {};
while (true) { while (true) {
var key_and_value = stypes.parse_whatever(this); var key_and_value = stypes.parse_whatever(this);
var key = key_and_value[0]; var key = key_and_value[0];
var value = key_and_value[1]; var value = key_and_value[1];
output += ("\"" + key + "\":" + jsonify_structure(value,key)); output[key] = jsonify_structure(value,key);
output += ",";
if (this.pointer == this.buffer.length) { if (this.pointer == this.buffer.length) {
break; break;
} else if (this.pointer > this.buffer.length) { } else if (this.pointer > this.buffer.length) {
@@ -130,58 +129,47 @@ SerializedObject.prototype.to_json = function() {
break; break;
} }
} }
output = output.slice(0,-1);
output += "}";
this.pointer = old_pointer; this.pointer = old_pointer;
output = JSON.parse(output);
return output; return output;
} }
function jsonify_structure(thing,field_name) { function jsonify_structure(thing,field_name) {
var output; var output;
var typeof_thing = typeof thing; var typeof_thing = typeof thing;
if (typeof_thing === "number") { //Special codes if ("number" === typeof thing) { //Special codes
if (field_name) { if (field_name) {
if (field_name === "LedgerEntryType") { if (field_name === "LedgerEntryType") {
output = thing; //TODO: Do we have special codes for LedgerEntryType? output = thing; //TODO: Do we have special codes for LedgerEntryType?
} else if (field_name === "TransactionType") { } else if (field_name === "TransactionType") {
output = "\""+TRANSACTION_TYPES[thing]+"\"" || thing; output = TRANSACTION_TYPES[thing] || thing;
} else { } else {
output = thing; output = thing;
} }
} else { } else {
output = thing; output = thing;
} }
} else if (typeof_thing === "boolean") { } else if ("object" === typeof thing &&
output = thing; "function" === typeof thing.to_json) {
} else if (typeof_thing === "string") { output = thing.to_json();
output = "\"" + thing + "\"";
} else if ( "function" === typeof thing.to_json ) {
output = JSON.stringify(thing.to_json());
} else if (Array.isArray(thing)) { } else if (Array.isArray(thing)) {
//console.log("here2"); //console.log("here2");
//iterate over array [] //iterate over array []
output = "["; output = [];
for (var i=0; i< thing.length; i++) { for (var i=0; i< thing.length; i++) {
output += jsonify_structure(thing[i]); output.push(jsonify_structure(thing[i]));
output += ",";
} }
output = output.slice(0,-1); } else if ("object" === typeof thing) {
output += "]";
} else {
//console.log("here1", thing); //console.log("here1", thing);
//iterate over object {} //iterate over object {}
output = "{"; output = {};
var keys = Object.keys(thing); var keys = Object.keys(thing);
//console.log(keys);
for (var i=0; i<keys.length; i++) { for (var i=0; i<keys.length; i++) {
var key = keys[i]; var key = keys[i];
var value = thing[key] var value = thing[key];
output += "\""+ key + "\":" + jsonify_structure(value); output[key] = jsonify_structure(value);
output += ","
} }
output = output.slice(0,-1); } else {
output += "}"; output = thing;
} }
return output; return output;