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 {
|
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,8 +145,26 @@ 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 {
|
||||||
|
@ -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} } ',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user