Merge branch 'develop' of https://github.com/ripple/ripple-lib into develop

This commit is contained in:
wltsmrz
2013-08-26 10:43:48 -07:00
3 changed files with 25 additions and 33 deletions

View File

@@ -92,7 +92,7 @@ Currency.prototype.parse_bytes = function (byte_array) {
} }
if (isZeroExceptInStandardPositions) { if (isZeroExceptInStandardPositions) {
var currencyCode = String.fromCharCode(byte_array[12]) + String.fromCharCode(byte_array[13]) + String.fromCharCode(byte_array[14]); var currencyCode = String.fromCharCode(byte_array[12]) + String.fromCharCode(byte_array[13]) + String.fromCharCode(byte_array[14]);
if (/^[A-Z]{3}$/.test(currencyCode) && currencyCode !== "XRP" ) { if (/^[A-Z0-9]{3}$/.test(currencyCode) && currencyCode !== "XRP" ) {
this._value = currencyCode; this._value = currencyCode;
} else if (currencyCode === "\0\0\0") { } else if (currencyCode === "\0\0\0") {
this._value = 0; this._value = 0;

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;

View File

@@ -254,10 +254,14 @@ var STCurrency = new SerializedType({
} }
}, },
parse: function (so) { parse: function (so) {
var currency = Currency.from_bytes(so.read(20)); var bytes = so.read(20);
if (!currency.is_valid()) { var currency = Currency.from_bytes(bytes);
throw new Error("Invalid currency"); // XXX Disabled check. Theoretically, the Currency class should support any
} // UInt160 value and consider it valid. But it doesn't, so for the
// deserialization to be usable, we need to allow invalid results for now.
//if (!currency.is_valid()) {
// throw new Error("Invalid currency: "+convert_bytes_to_hex(bytes));
//}
return currency; return currency;
} }
}); });