mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-04-25 06:42:24 +00:00
Parse null
This commit is contained in:
parent
afaca3b128
commit
e4757490a7
@ -78,6 +78,7 @@ export class ThrowingJSONHandler extends JSONHandler {
|
||||
|
||||
const TRUE_STR = "true";
|
||||
const FALSE_STR = "false";
|
||||
const NULL_STR = "null";
|
||||
|
||||
export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
|
||||
@ -206,16 +207,12 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
|
||||
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<JSONHandlerT extends JSONHandler> {
|
||||
}
|
||||
|
||||
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 + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"}');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user