diff --git a/README.md b/README.md index e3c5683..c43f942 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/assembly/decoder.ts b/assembly/decoder.ts index 3e9fd63..9756031 100644 --- a/assembly/decoder.ts +++ b/assembly/decoder.ts @@ -120,21 +120,22 @@ export class JSONDecoder { 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 { 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;; }