Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

json: check the top of stack for type on array/object end tokens

  • Loading branch information...
commit 8f8f438eadccb44f479cdcb2a146cf08e12bda00 1 parent b24f047
@chjj chjj authored
Showing with 19 additions and 12 deletions.
  1. +19 −12 lib/json.js
View
31 lib/json.js
@@ -383,16 +383,18 @@ Parser.prototype.assemble = function(type, val) {
this.stack.push(object);
break;
case 'object key':
- if (this.key) {
- return this._unexpected(type);
+ if (this.key != null) {
+ return this._unexpected(':');
}
this.key = val;
break;
case 'object end':
- if (this.key) {
- return this._unexpected(type);
+ var top = this.stack.pop();
+ if (!top || Array.isArray(top)
+ || this.key != null) {
+ return this._unexpected('}');
}
- this.emit('object', this.stack.pop());
+ this.emit('object', top);
break;
case 'array start':
var array = [];
@@ -400,7 +402,11 @@ Parser.prototype.assemble = function(type, val) {
this.stack.push(array);
break;
case 'array end':
- this.emit('array', this.stack.pop());
+ var top = this.stack.pop();
+ if (!Array.isArray(top)) {
+ return this._unexpected(']');
+ }
+ this.emit('array', top);
break;
case 'number':
case 'string':
@@ -409,29 +415,30 @@ Parser.prototype.assemble = function(type, val) {
this.emit(type, val);
this.assemble('value', val);
break;
- case 'value':
+ case 'value': {
var top = this.stack[this.stack.length-1];
if (!top) {
- if (this.key) {
- return this._error('Unexpected key.');
+ if (this.key != null) {
+ return this._error('Unexpected `:`.');
}
this.data = val;
this.stack.push(val);
return;
}
- if (!this.key) {
+ if (this.key == null) {
if (!Array.isArray(top)) {
- return this._error('Expected key.');
+ return this._error('Unexpected `value`.');
}
top.push(val);
} else {
if (Array.isArray(top)) {
- return this._error('Unexpected key.');
+ return this._error('Unexpected `:`.');
}
top[this.key] = val;
this.key = null;
}
break;
+ }
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.