diff --git a/assembly/decoder.ts b/assembly/decoder.ts index 105e66e..c5ee461 100644 --- a/assembly/decoder.ts +++ b/assembly/decoder.ts @@ -78,6 +78,7 @@ export class ThrowingJSONHandler extends JSONHandler { const TRUE_STR = "true"; const FALSE_STR = "false"; +const NULL_STR = "null"; export class JSONDecoder { @@ -206,16 +207,12 @@ export class JSONDecoder { private parseBoolean(): boolean { if (this.peekChar() == FALSE_STR.charCodeAt(0)) { - for (let i = 0; i < FALSE_STR.length; i++) { - assert(FALSE_STR.charCodeAt(i) == this.readChar(), "Expected false"); - } + this.readAndAssert(FALSE_STR); this.handler.setBoolean(this.lastKey, false); return true; } if (this.peekChar() == TRUE_STR.charCodeAt(0)) { - for (let i = 0; i < TRUE_STR.length; i++) { - assert(TRUE_STR.charCodeAt(i) == this.readChar(), "Expected true"); - } + this.readAndAssert(TRUE_STR); this.handler.setBoolean(this.lastKey, true); return true; } @@ -224,7 +221,17 @@ export class JSONDecoder { } private parseNull(): boolean { - assert(false, "Method not implemented."); + if (this.peekChar() == NULL_STR.charCodeAt(0)) { + this.readAndAssert(NULL_STR); + this.handler.setNull(this.lastKey); + return true; + } return false; } + + private readAndAssert(str: string): void { + for (let i = 0; i < str.length; i++) { + assert(str.charCodeAt(i) == this.readChar(), "Expected '" + str + "'"); + } + } } diff --git a/tests/assembly/decoder.spec.as.ts b/tests/assembly/decoder.spec.as.ts index e9e5aef..49445e3 100644 --- a/tests/assembly/decoder.spec.as.ts +++ b/tests/assembly/decoder.spec.as.ts @@ -124,6 +124,10 @@ export class StringConversionTests { return this.roundripTest('{"val":false}'); } + static shouldHandleNull(): bool { + return this.roundripTest('{"val":null}'); + } + static shouldHandleString(): bool { return this.roundripTest('{"str":"foo"}'); }