import { BRACE_LEFT, BRACKET_LEFT, BRACKET_RIGHT } from "../../../custom/chars"; import { JSON } from "../../.."; import { isSpace } from "util/string"; import { parseObjectBody } from "../object"; export function deserializeObjectArray( srcStart: usize, srcEnd: usize, dst: usize, ): T { const out = changetype>( dst || changetype(instantiate()), ); while (srcEnd > srcStart && isSpace(load(srcEnd - 2))) srcEnd -= 2; if (srcStart - srcEnd == 0) throw new Error("Input string had zero length or was all whitespace"); if (load(srcStart) != BRACKET_LEFT) throw new Error( "Expected '[' at start of object at position " + (srcEnd - srcStart).toString(), ); if (load(srcEnd - 2) != BRACKET_RIGHT) throw new Error( "Expected ']' at end of object at position " + (srcEnd - srcStart).toString(), ); // Each `{...}` element is parsed in a single pass via parseObjectBody, which // reports where it ended - no separate scan to find the closing brace. while (srcStart < srcEnd) { if (load(srcStart) == BRACE_LEFT) { const obj = new JSON.Obj(); srcStart = parseObjectBody(obj, srcStart + 2, srcEnd); out.push(changetype>(changetype(obj))); } else { srcStart += 2; } } return out; }