Handle false return value from pushObject nad pushArray

This commit is contained in:
Vladimir Grichina 2019-01-06 18:51:59 -08:00
parent 41ae669077
commit b036c86f5f
2 changed files with 33 additions and 27 deletions

View File

@ -1,4 +1,4 @@
# assemblyscript-bson
# assemblyscript-json
JSON encoder / decoder for AssemblyScript.
@ -67,7 +67,9 @@ class MyJSONEventsHandler extends JSONHandler {
pushArray(name: string): bool {
// Handle array start
return true; // true means that nested object needs to be traversed, false otherwise
// true means that nested object needs to be traversed, false otherwise
// Note that returning false means JSONDecoder.startIndex need to be updated by handler
return true;
}
popArray(): void {
@ -76,7 +78,9 @@ class MyJSONEventsHandler extends JSONHandler {
pushObject(name: string): bool {
// Handle object start
return true; // true means that nested object needs to be traversed, false otherwise
// true means that nested object needs to be traversed, false otherwise
// Note that returning false means JSONDecoder.startIndex need to be updated by handler
return true;
}
popObject(): void {

View File

@ -120,21 +120,22 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
if (this.peekChar() != "{".charCodeAt(0)) {
return false;
}
this.handler.pushObject(this.lastKey);
this.readChar();
this.skipWhitespace();
if (this.handler.pushObject(this.lastKey)) {
this.readChar();
this.skipWhitespace();
let firstItem = true;
while (this.peekChar() != "}".charCodeAt(0)) {
if (!firstItem) {
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
} else {
firstItem = false;
let firstItem = true;
while (this.peekChar() != "}".charCodeAt(0)) {
if (!firstItem) {
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
} else {
firstItem = false;
}
this.parseKey();
this.parseValue();
}
this.parseKey();
this.parseValue();
assert(this.readChar() == "}".charCodeAt(0), "Unexpected end of object");
}
assert(this.readChar() == "}".charCodeAt(0), "Unexpected end of object");
this.handler.popObject();
return true;
}
@ -150,21 +151,22 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
if (this.peekChar() != "[".charCodeAt(0)) {
return false;
}
this.handler.pushArray(this.lastKey);
this.lastKey = null;
this.readChar();
this.skipWhitespace();
if (this.handler.pushArray(this.lastKey)) {
this.lastKey = null;
this.readChar();
this.skipWhitespace();
let firstItem = true;
while (this.peekChar() != "]".charCodeAt(0)) {
if (!firstItem) {
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
} else {
firstItem = false;
let firstItem = true;
while (this.peekChar() != "]".charCodeAt(0)) {
if (!firstItem) {
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
} else {
firstItem = false;
}
this.parseValue();
}
this.parseValue();
assert(this.readChar() == "]".charCodeAt(0), "Unexpected end of array");
}
assert(this.readChar() == "]".charCodeAt(0), "Unexpected end of array");
this.handler.popArray();
return true;;
}