mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-04-25 06:42:24 +00:00
Support arrays
This commit is contained in:
parent
a4a11af1a8
commit
1567bb19c7
@ -20,9 +20,6 @@ export abstract class JSONHandler {
|
||||
setInteger(name: string, value: i32): void {
|
||||
}
|
||||
|
||||
setUint8Array(name: string, value: Uint8Array): void {
|
||||
}
|
||||
|
||||
pushArray(name: string): bool {
|
||||
return true;
|
||||
}
|
||||
@ -61,10 +58,6 @@ export class ThrowingJSONHandler extends JSONHandler {
|
||||
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 {
|
||||
assert(false, 'Unexpected array field' + name);
|
||||
return true;
|
||||
@ -152,9 +145,27 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
}
|
||||
|
||||
private parseArray(): bool {
|
||||
// TODO
|
||||
if (this.peekChar() != "[".charCodeAt(0)) {
|
||||
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 {
|
||||
if (this.peekChar() != '"'.charCodeAt(0)) {
|
||||
|
@ -30,10 +30,6 @@ class JSONTestHandler extends JSONHandler {
|
||||
this.writeInteger(value);
|
||||
}
|
||||
|
||||
setUint8Array(name: string, value: Uint8Array): void {
|
||||
assert(false, "Not implemented");
|
||||
}
|
||||
|
||||
pushArray(name: string): bool {
|
||||
this.writeKey(name);
|
||||
this.write("[");
|
||||
@ -144,6 +140,22 @@ export class StringConversionTests {
|
||||
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 {
|
||||
return this.roundripTest(
|
||||
' { "str":"foo","obj": {"a":1, "b" :\n -123456} } ',
|
||||
|
Loading…
x
Reference in New Issue
Block a user