mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-04-25 06:42:24 +00:00
Handle false return value from pushObject nad pushArray
This commit is contained in:
parent
41ae669077
commit
b036c86f5f
10
README.md
10
README.md
@ -1,4 +1,4 @@
|
||||
# assemblyscript-bson
|
||||
# assemblyscript-json
|
||||
|
||||
JSON encoder / decoder for AssemblyScript.
|
||||
|
||||
@ -67,7 +67,9 @@ class MyJSONEventsHandler extends JSONHandler {
|
||||
|
||||
pushArray(name: string): bool {
|
||||
// Handle array start
|
||||
return true; // true means that nested object needs to be traversed, false otherwise
|
||||
// true means that nested object needs to be traversed, false otherwise
|
||||
// Note that returning false means JSONDecoder.startIndex need to be updated by handler
|
||||
return true;
|
||||
}
|
||||
|
||||
popArray(): void {
|
||||
@ -76,7 +78,9 @@ class MyJSONEventsHandler extends JSONHandler {
|
||||
|
||||
pushObject(name: string): bool {
|
||||
// Handle object start
|
||||
return true; // true means that nested object needs to be traversed, false otherwise
|
||||
// true means that nested object needs to be traversed, false otherwise
|
||||
// Note that returning false means JSONDecoder.startIndex need to be updated by handler
|
||||
return true;
|
||||
}
|
||||
|
||||
popObject(): void {
|
||||
|
@ -120,21 +120,22 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
if (this.peekChar() != "{".charCodeAt(0)) {
|
||||
return false;
|
||||
}
|
||||
this.handler.pushObject(this.lastKey);
|
||||
this.readChar();
|
||||
this.skipWhitespace();
|
||||
if (this.handler.pushObject(this.lastKey)) {
|
||||
this.readChar();
|
||||
this.skipWhitespace();
|
||||
|
||||
let firstItem = true;
|
||||
while (this.peekChar() != "}".charCodeAt(0)) {
|
||||
if (!firstItem) {
|
||||
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
|
||||
} else {
|
||||
firstItem = false;
|
||||
let firstItem = true;
|
||||
while (this.peekChar() != "}".charCodeAt(0)) {
|
||||
if (!firstItem) {
|
||||
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
|
||||
} else {
|
||||
firstItem = false;
|
||||
}
|
||||
this.parseKey();
|
||||
this.parseValue();
|
||||
}
|
||||
this.parseKey();
|
||||
this.parseValue();
|
||||
assert(this.readChar() == "}".charCodeAt(0), "Unexpected end of object");
|
||||
}
|
||||
assert(this.readChar() == "}".charCodeAt(0), "Unexpected end of object");
|
||||
this.handler.popObject();
|
||||
return true;
|
||||
}
|
||||
@ -150,21 +151,22 @@ export class JSONDecoder<JSONHandlerT extends JSONHandler> {
|
||||
if (this.peekChar() != "[".charCodeAt(0)) {
|
||||
return false;
|
||||
}
|
||||
this.handler.pushArray(this.lastKey);
|
||||
this.lastKey = null;
|
||||
this.readChar();
|
||||
this.skipWhitespace();
|
||||
if (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;
|
||||
let firstItem = true;
|
||||
while (this.peekChar() != "]".charCodeAt(0)) {
|
||||
if (!firstItem) {
|
||||
assert(this.readChar() == ",".charCodeAt(0), "Expected ','");
|
||||
} else {
|
||||
firstItem = false;
|
||||
}
|
||||
this.parseValue();
|
||||
}
|
||||
this.parseValue();
|
||||
assert(this.readChar() == "]".charCodeAt(0), "Unexpected end of array");
|
||||
}
|
||||
assert(this.readChar() == "]".charCodeAt(0), "Unexpected end of array");
|
||||
this.handler.popArray();
|
||||
return true;;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user