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. JSON encoder / decoder for AssemblyScript.
@ -67,7 +67,9 @@ class MyJSONEventsHandler extends JSONHandler {
pushArray(name: string): bool { pushArray(name: string): bool {
// Handle array start // 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 { popArray(): void {
@ -76,7 +78,9 @@ class MyJSONEventsHandler extends JSONHandler {
pushObject(name: string): bool { pushObject(name: string): bool {
// Handle object start // 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 { popObject(): void {

View File

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