Support arrays

This commit is contained in:
Vladimir Grichina 2019-01-06 03:10:20 -08:00
parent a4a11af1a8
commit 1567bb19c7
2 changed files with 36 additions and 13 deletions

View File

@ -20,9 +20,6 @@ export abstract class JSONHandler {
setInteger(name: string, value: i32): void { setInteger(name: string, value: i32): void {
} }
setUint8Array(name: string, value: Uint8Array): void {
}
pushArray(name: string): bool { pushArray(name: string): bool {
return true; return true;
} }
@ -61,10 +58,6 @@ export class ThrowingJSONHandler extends JSONHandler {
assert(false, 'Unexpected integer field ' + name + ' : ' + arr.toString()); assert(false, 'Unexpected integer field ' + name + ' : ' + arr.toString());
} }
setUint8Array(name: string, value: Uint8Array): void {
assert(false, 'Unexpected byte array field ' + name);
}
pushArray(name: string): bool { pushArray(name: string): bool {
assert(false, 'Unexpected array field' + name); assert(false, 'Unexpected array field' + name);
return true; return true;
@ -152,9 +145,27 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
} }
private parseArray(): bool { private parseArray(): bool {
// TODO if (this.peekChar() != "[".charCodeAt(0)) {
return false; return false;
} }
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;
}
this.parseValue();
}
assert(this.readChar() == "]".charCodeAt(0), "Unexpected end of array");
this.handler.popArray();
return true;;
}
private parseString(): bool { private parseString(): bool {
if (this.peekChar() != '"'.charCodeAt(0)) { if (this.peekChar() != '"'.charCodeAt(0)) {

View File

@ -30,10 +30,6 @@ class JSONTestHandler extends JSONHandler {
this.writeInteger(value); this.writeInteger(value);
} }
setUint8Array(name: string, value: Uint8Array): void {
assert(false, "Not implemented");
}
pushArray(name: string): bool { pushArray(name: string): bool {
this.writeKey(name); this.writeKey(name);
this.write("["); this.write("[");
@ -144,6 +140,22 @@ export class StringConversionTests {
return this.roundripTest('{"str":"foo","obj":{"a":1,"b":-123456}}'); return this.roundripTest('{"str":"foo","obj":{"a":1,"b":-123456}}');
} }
static shouldHandleEmptyArray(): bool {
return this.roundripTest('[]');
}
static shouldHandleArray(): bool {
return this.roundripTest('[1,2,3]');
}
static shouldHandleNestedArrays(): bool {
return this.roundripTest('[[1,2,3],[4,[5,6]]]');
}
static shouldHandleNestedObjectsAndArrays(): bool {
return this.roundripTest('{"str":"foo","arr":[{"obj":{"a":1,"b":-123456}}]}');
}
static shouldHandleWhitespace(): bool { static shouldHandleWhitespace(): bool {
return this.roundripTest( return this.roundripTest(
' { "str":"foo","obj": {"a":1, "b" :\n -123456} } ', ' { "str":"foo","obj": {"a":1, "b" :\n -123456} } ',