Parse null

This commit is contained in:
Vladimir Grichina 2019-01-06 02:31:38 -08:00
parent afaca3b128
commit e4757490a7
2 changed files with 18 additions and 7 deletions

View File

@ -78,6 +78,7 @@ export class ThrowingJSONHandler extends JSONHandler {
const TRUE_STR = "true"; const TRUE_STR = "true";
const FALSE_STR = "false"; const FALSE_STR = "false";
const NULL_STR = "null";
export class JSONDecoder<JSONHandlerT extends JSONHandler> { export class JSONDecoder<JSONHandlerT extends JSONHandler> {
@ -206,16 +207,12 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
private parseBoolean(): boolean { private parseBoolean(): boolean {
if (this.peekChar() == FALSE_STR.charCodeAt(0)) { if (this.peekChar() == FALSE_STR.charCodeAt(0)) {
for (let i = 0; i < FALSE_STR.length; i++) { this.readAndAssert(FALSE_STR);
assert(FALSE_STR.charCodeAt(i) == this.readChar(), "Expected false");
}
this.handler.setBoolean(this.lastKey, false); this.handler.setBoolean(this.lastKey, false);
return true; return true;
} }
if (this.peekChar() == TRUE_STR.charCodeAt(0)) { if (this.peekChar() == TRUE_STR.charCodeAt(0)) {
for (let i = 0; i < TRUE_STR.length; i++) { this.readAndAssert(TRUE_STR);
assert(TRUE_STR.charCodeAt(i) == this.readChar(), "Expected true");
}
this.handler.setBoolean(this.lastKey, true); this.handler.setBoolean(this.lastKey, true);
return true; return true;
} }
@ -224,7 +221,17 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
} }
private parseNull(): boolean { 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; return false;
} }
private readAndAssert(str: string): void {
for (let i = 0; i < str.length; i++) {
assert(str.charCodeAt(i) == this.readChar(), "Expected '" + str + "'");
}
}
} }

View File

@ -124,6 +124,10 @@ export class StringConversionTests {
return this.roundripTest('{"val":false}'); return this.roundripTest('{"val":false}');
} }
static shouldHandleNull(): bool {
return this.roundripTest('{"val":null}');
}
static shouldHandleString(): bool { static shouldHandleString(): bool {
return this.roundripTest('{"str":"foo"}'); return this.roundripTest('{"str":"foo"}');
} }