Fix array and inner-object parsing

This commit is contained in:
jatchili
2013-08-20 17:36:12 -07:00
parent 083377651f
commit 7a5631a2ce
3 changed files with 10 additions and 7 deletions

View File

@@ -6,7 +6,7 @@ var binformat = require('./binformat'),
var UInt256 = require('./uint256').UInt256;
var SerializedObject = function (buf) {
if (Array.isArray(buf)) {
if (Array.isArray(buf) || (Buffer && Buffer.isBuffer(buf)) ) {
this.buffer = buf;
} else if ("string" === typeof buf) {
this.buffer = sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(buf));
@@ -157,7 +157,7 @@ function jsonify_structure(thing,field_name) {
} else if (typeof_thing === "string") {
output = "\"" + thing + "\"";
} else if ( "function" === typeof thing.to_json ) {
output = "\"" + thing.to_json() + "\"";
output = JSON.stringify(thing.to_json());
} else if (Array.isArray(thing)) {
//console.log("here2");
//iterate over array []
@@ -183,6 +183,7 @@ function jsonify_structure(thing,field_name) {
output = output.slice(0,-1);
output += "}";
}
return output;
}

View File

@@ -383,7 +383,7 @@ var STAccount = exports.Account = new SerializedType({
throw new Error("Non-standard-length account ID");
}
var result = UInt160.from_bytes(so.read(len));
if (!result.is_valid()) {
if (false && !result.is_valid()) {
throw new Error("Invalid Account");
}
return result;
@@ -549,12 +549,10 @@ var parse_whatever = exports.parse_whatever = function(so) {
if (field_bits === 0) {
field_name = FIELDS_MAP[type_bits][so.read(1)[0]];
} else {
//console.log("!!!!!!!!!!IJOIOJIOJO", type_bits, field_bits);
field_name = FIELDS_MAP[type_bits][field_bits];
}
//console.log("PARSING WHATEVER!!!!", type_bits, type, field_name);
if ("undefined" === typeof field_name) {
throw Error("Unknown field");
throw Error("Unknown field "+tag_byte);
} else {
return [field_name, type.parse(so)]; //key, value
}
@@ -596,6 +594,7 @@ var STObject = exports.Object = new SerializedType({
var output = {};
while (true) {
if (so.peek(1)[0] === 0xe1) { //ending marker
so.read(1);
break;
} else {
//console.log("WTF M8");
@@ -626,6 +625,7 @@ var STArray = exports.Array = new SerializedType({
var output = [];
while (true) {
if (so.peek(1)[0] === 0xf1) { //ending marker
so.read(1);
break;
} else {
var key_and_value = parse_whatever(so);

View File

@@ -172,6 +172,7 @@ UInt.prototype.parse_bits = function (j) {
return this;
};
UInt.prototype.parse_bytes = function (j) {
if (!Array.isArray(j) || j.length !== this.constructor.width) {
this._value = NaN;
@@ -182,6 +183,7 @@ UInt.prototype.parse_bytes = function (j) {
return this;
};
UInt.prototype.parse_json = UInt.prototype.parse_hex;
UInt.prototype.parse_bn = function (j) {