UNPKG

@loaders.gl/json

Version:

Framework-independent loader for JSON and streaming JSON formats

4 lines 84.9 kB
{ "version": 3, "sources": ["../src/index.ts", "../src/lib/parsers/parse-json.ts", "../src/lib/parsers/parse-json-in-batches.ts", "../src/lib/clarinet/clarinet.ts", "../src/lib/jsonpath/jsonpath.ts", "../src/lib/json-parser/json-parser.ts", "../src/lib/json-parser/streaming-json-parser.ts", "../src/json-loader.ts", "../src/lib/parsers/parse-ndjson.ts", "../src/lib/parsers/parse-ndjson-in-batches.ts", "../src/ndjson-loader.ts", "../src/lib/encoders/json-encoder.ts", "../src/json-writer.ts", "../src/geojson-loader.ts", "../src/geojson-writer.ts", "../src/lib/encoders/geojson-encoder.ts", "../src/lib/encoder-utils/encode-utils.ts", "../src/lib/encoder-utils/encode-table-row.ts", "../src/lib/encoder-utils/utf8-encoder.ts"], "sourcesContent": ["// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nexport type {JSONLoaderOptions} from './json-loader';\nexport {JSONLoader} from './json-loader';\nexport {NDJSONLoader} from './ndjson-loader';\n\nexport type {JSONWriterOptions} from './json-writer';\nexport {JSONWriter} from './json-writer';\n\n// EXPERIMENTAL EXPORTS - WARNING: MAY BE REMOVED WIHTOUT NOTICE IN FUTURE RELEASES\nexport type {GeoJSONLoaderOptions as _GeoJSONLoaderOptions} from './geojson-loader';\nexport {\n GeoJSONLoader as _GeoJSONLoader,\n GeoJSONWorkerLoader as _GeoJSONWorkerLoader\n} from './geojson-loader';\n\nexport type {GeoJSONWriterOptions as _GeoJSONWriterOptions} from './geojson-writer';\nexport {GeoJSONWriter as _GeoJSONWriter} from './geojson-writer';\n\nexport {default as _JSONPath} from './lib/jsonpath/jsonpath';\nexport {default as _ClarinetParser} from './lib/clarinet/clarinet';\n\nexport {rebuildJsonObject as _rebuildJsonObject} from './lib/parsers/parse-json-in-batches';\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {RowTable} from '@loaders.gl/schema';\nimport {makeTableFromData} from '@loaders.gl/schema-utils';\nimport type {JSONLoaderOptions} from '../../json-loader';\n\nexport function parseJSONSync(jsonText: string, options: JSONLoaderOptions): RowTable {\n try {\n const json = JSON.parse(jsonText);\n if (options.json?.table) {\n const data = getFirstArray(json) || json;\n return makeTableFromData(data);\n }\n return json;\n } catch (error) {\n throw new Error('JSONLoader: failed to parse JSON');\n }\n}\n\nfunction getFirstArray(json) {\n if (Array.isArray(json)) {\n return json;\n }\n if (json && typeof json === 'object') {\n for (const value of Object.values(json)) {\n const array = getFirstArray(value);\n if (array) {\n return array;\n }\n }\n }\n return null;\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {Schema, TableBatch} from '@loaders.gl/schema';\nimport type {JSONLoaderOptions, MetadataBatch, JSONBatch} from '../../json-loader';\n\nimport {TableBatchBuilder} from '@loaders.gl/schema-utils';\nimport {assert, makeTextDecoderIterator, toArrayBufferIterator} from '@loaders.gl/loader-utils';\nimport StreamingJSONParser from '../json-parser/streaming-json-parser';\nimport JSONPath from '../jsonpath/jsonpath';\n\n// TODO - support batch size 0 = no batching/single batch?\n// eslint-disable-next-line max-statements, complexity\nexport async function* parseJSONInBatches(\n binaryAsyncIterator:\n | AsyncIterable<ArrayBufferLike | ArrayBufferView>\n | Iterable<ArrayBufferLike | ArrayBufferView>,\n options: JSONLoaderOptions\n): AsyncIterable<TableBatch | MetadataBatch | JSONBatch> {\n const asyncIterator = makeTextDecoderIterator(toArrayBufferIterator(binaryAsyncIterator));\n\n const metadata = Boolean(options?.core?.metadata || (options as any)?.metadata);\n const {jsonpaths} = options.json || {};\n\n let isFirstChunk: boolean = true;\n\n // @ts-expect-error TODO fix Schema deduction\n const schema: Schema = null;\n const tableBatchBuilder = new TableBatchBuilder(schema, options?.core);\n\n const parser = new StreamingJSONParser({jsonpaths});\n\n for await (const chunk of asyncIterator) {\n const rows = parser.write(chunk);\n\n const jsonpath = rows.length > 0 && parser.getStreamingJsonPathAsString();\n\n if (rows.length > 0 && isFirstChunk) {\n if (metadata) {\n const initialBatch: TableBatch = {\n // Common fields\n shape: options?.json?.shape || 'array-row-table',\n batchType: 'partial-result',\n data: [],\n length: 0,\n bytesUsed: 0,\n // JSON additions\n container: parser.getPartialResult(),\n jsonpath\n };\n yield initialBatch;\n }\n isFirstChunk = false;\n // schema = deduceSchema(rows);\n }\n\n // Add the row\n for (const row of rows) {\n tableBatchBuilder.addRow(row);\n // If a batch has been completed, emit it\n const batch = tableBatchBuilder.getFullBatch({jsonpath});\n if (batch) {\n yield batch;\n }\n }\n\n tableBatchBuilder.chunkComplete(chunk);\n const batch = tableBatchBuilder.getFullBatch({jsonpath});\n if (batch) {\n yield batch;\n }\n }\n\n // yield final batch\n const jsonpath = parser.getStreamingJsonPathAsString();\n const batch = tableBatchBuilder.getFinalBatch({jsonpath});\n if (batch) {\n yield batch;\n }\n\n if (metadata) {\n const finalBatch: JSONBatch = {\n shape: 'json',\n batchType: 'final-result',\n container: parser.getPartialResult(),\n jsonpath: parser.getStreamingJsonPathAsString(),\n /** Data Just to avoid crashing? */\n data: [],\n length: 0\n // schema: null\n };\n yield finalBatch;\n }\n}\n\nexport function rebuildJsonObject(batch, data) {\n // Last batch will have this special type and will provide all the root object of the parsed file\n assert(batch.batchType === 'final-result');\n\n // The streamed JSON data is a top level array (jsonpath = '$'), just return the array of row objects\n if (batch.jsonpath === '$') {\n return data;\n }\n\n // (jsonpath !== '$') The streamed data is not a top level array, so stitch it back in to the top-level object\n if (batch.jsonpath && batch.jsonpath.length > 1) {\n const topLevelObject = batch.container;\n const streamingPath = new JSONPath(batch.jsonpath);\n streamingPath.setFieldAtPath(topLevelObject, data);\n return topLevelObject;\n }\n\n // No jsonpath, in this case nothing was streamed.\n return batch.container;\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT AND BSD\n// Copyright (c) vis.gl contributors\n// This is a fork of the clarinet library, originally BSD license (see LICENSE file)\n// loaders.gl changes:\n// - typescript port\n\nexport type ClarinetEvent =\n | 'onvalue'\n | 'onstring'\n | 'onkey'\n | 'onopenobject'\n | 'oncloseobject'\n | 'onopenarray'\n | 'onclosearray'\n | 'onerror'\n | 'onend'\n | 'onready';\n\n// Removes the MAX_BUFFER_LENGTH, originally set to 64 * 1024\nconst MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;\n// const DEBUG = false;\n\nenum STATE {\n BEGIN = 0,\n VALUE, // general stuff\n OPEN_OBJECT, // {\n CLOSE_OBJECT, // }\n OPEN_ARRAY, // [\n CLOSE_ARRAY, // ]\n TEXT_ESCAPE, // \\ stuff\n STRING, // \"\"\n BACKSLASH,\n END, // No more stack\n OPEN_KEY, // , \"a\"\n CLOSE_KEY, // :\n TRUE, // r\n TRUE2, // u\n TRUE3, // e\n FALSE, // a\n FALSE2, // l\n FALSE3, // s\n FALSE4, // e\n NULL, // u\n NULL2, // l\n NULL3, // l\n NUMBER_DECIMAL_POINT, // .\n NUMBER_DIGIT // [0-9]\n}\n\nconst Char = {\n tab: 0x09, // \\t\n lineFeed: 0x0a, // \\n\n carriageReturn: 0x0d, // \\r\n space: 0x20, // \" \"\n\n doubleQuote: 0x22, // \"\n plus: 0x2b, // +\n comma: 0x2c, // ,\n minus: 0x2d, // -\n period: 0x2e, // .\n\n _0: 0x30, // 0\n _9: 0x39, // 9\n\n colon: 0x3a, // :\n\n E: 0x45, // E\n\n openBracket: 0x5b, // [\n backslash: 0x5c, // \\\n closeBracket: 0x5d, // ]\n\n a: 0x61, // a\n b: 0x62, // b\n e: 0x65, // e\n f: 0x66, // f\n l: 0x6c, // l\n n: 0x6e, // n\n r: 0x72, // r\n s: 0x73, // s\n t: 0x74, // t\n u: 0x75, // u\n\n openBrace: 0x7b, // {\n closeBrace: 0x7d // }\n};\n\nconst stringTokenPattern = /[\\\\\"\\n]/g;\n\ntype ParserEvent = (parser: ClarinetParser, event: string, data?: any) => void;\n\nexport type ClarinetParserOptions = {\n onready?: ParserEvent;\n onopenobject?: ParserEvent;\n onkey?: ParserEvent;\n oncloseobject?: ParserEvent;\n onopenarray?: ParserEvent;\n onclosearray?: ParserEvent;\n onvalue?: ParserEvent;\n onerror?: ParserEvent;\n onend?: ParserEvent;\n onchunkparsed?: ParserEvent;\n};\n\nconst DEFAULT_OPTIONS: Required<ClarinetParserOptions> = {\n onready: () => {},\n onopenobject: () => {},\n onkey: () => {},\n oncloseobject: () => {},\n onopenarray: () => {},\n onclosearray: () => {},\n onvalue: () => {},\n onerror: () => {},\n onend: () => {},\n onchunkparsed: () => {}\n};\nexport default class ClarinetParser {\n protected options: Required<ClarinetParserOptions> = DEFAULT_OPTIONS;\n\n bufferCheckPosition = MAX_BUFFER_LENGTH;\n q = '';\n c = '';\n p = '';\n closed = false;\n closedRoot = false;\n sawRoot = false;\n // tag = null;\n error: Error | null = null;\n state = STATE.BEGIN;\n stack: STATE[] = [];\n // mostly just for error reporting\n position: number = 0;\n column: number = 0;\n line: number = 1;\n slashed: boolean = false;\n unicodeI: number = 0;\n unicodeS: string | null = null;\n depth: number = 0;\n\n textNode;\n numberNode;\n\n constructor(options: ClarinetParserOptions = {}) {\n this.options = {...DEFAULT_OPTIONS, ...options};\n this.textNode = undefined;\n this.numberNode = '';\n this.emit('onready');\n }\n\n end() {\n if (this.state !== STATE.VALUE || this.depth !== 0) this._error('Unexpected end');\n\n this._closeValue();\n this.c = '';\n this.closed = true;\n this.emit('onend');\n return this;\n }\n\n resume() {\n this.error = null;\n return this;\n }\n\n close() {\n return this.write(null);\n }\n\n // protected\n\n emit(event: string, data?: any): void {\n // if (DEBUG) console.log('-- emit', event, data);\n this.options[event]?.(data, this);\n }\n\n emitNode(event: string, data?: any): void {\n this._closeValue();\n this.emit(event, data);\n }\n\n /* eslint-disable no-continue */\n // eslint-disable-next-line complexity, max-statements\n write(chunk) {\n if (this.error) {\n throw this.error;\n }\n if (this.closed) {\n return this._error('Cannot write after close. Assign an onready handler.');\n }\n if (chunk === null) {\n return this.end();\n }\n let i = 0;\n let c = chunk.charCodeAt(0);\n let p = this.p;\n // if (DEBUG) console.log(`write -> [${ chunk }]`);\n while (c) {\n p = c;\n this.c = c = chunk.charCodeAt(i++);\n // if chunk doesnt have next, like streaming char by char\n // this way we need to check if previous is really previous\n // if not we need to reset to what the this says is the previous\n // from buffer\n if (p !== c) {\n this.p = p;\n } else {\n p = this.p;\n }\n\n if (!c) break;\n\n // if (DEBUG) console.log(i, c, STATE[this.state]);\n this.position++;\n if (c === Char.lineFeed) {\n this.line++;\n this.column = 0;\n } else this.column++;\n\n switch (this.state) {\n case STATE.BEGIN:\n if (c === Char.openBrace) this.state = STATE.OPEN_OBJECT;\n else if (c === Char.openBracket) this.state = STATE.OPEN_ARRAY;\n else if (!isWhitespace(c)) {\n this._error('Non-whitespace before {[.');\n }\n continue;\n\n case STATE.OPEN_KEY:\n case STATE.OPEN_OBJECT:\n if (isWhitespace(c)) continue;\n if (this.state === STATE.OPEN_KEY) this.stack.push(STATE.CLOSE_KEY);\n else if (c === Char.closeBrace) {\n this.emit('onopenobject');\n this.depth++;\n this.emit('oncloseobject');\n this.depth--;\n this.state = this.stack.pop() || STATE.VALUE;\n continue;\n } else this.stack.push(STATE.CLOSE_OBJECT);\n if (c === Char.doubleQuote) this.state = STATE.STRING;\n else this._error('Malformed object key should start with \"');\n continue;\n\n case STATE.CLOSE_KEY:\n case STATE.CLOSE_OBJECT:\n if (isWhitespace(c)) continue;\n // let event = this.state === STATE.CLOSE_KEY ? 'key' : 'object';\n if (c === Char.colon) {\n if (this.state === STATE.CLOSE_OBJECT) {\n this.stack.push(STATE.CLOSE_OBJECT);\n this._closeValue('onopenobject');\n this.depth++;\n } else this._closeValue('onkey');\n this.state = STATE.VALUE;\n } else if (c === Char.closeBrace) {\n this.emitNode('oncloseobject');\n this.depth--;\n this.state = this.stack.pop() || STATE.VALUE;\n } else if (c === Char.comma) {\n if (this.state === STATE.CLOSE_OBJECT) this.stack.push(STATE.CLOSE_OBJECT);\n this._closeValue();\n this.state = STATE.OPEN_KEY;\n } else this._error('Bad object');\n continue;\n\n case STATE.OPEN_ARRAY: // after an array there always a value\n case STATE.VALUE:\n if (isWhitespace(c)) continue;\n if (this.state === STATE.OPEN_ARRAY) {\n this.emit('onopenarray');\n this.depth++;\n this.state = STATE.VALUE;\n if (c === Char.closeBracket) {\n this.emit('onclosearray');\n this.depth--;\n this.state = this.stack.pop() || STATE.VALUE;\n continue;\n } else {\n this.stack.push(STATE.CLOSE_ARRAY);\n }\n }\n if (c === Char.doubleQuote) this.state = STATE.STRING;\n else if (c === Char.openBrace) this.state = STATE.OPEN_OBJECT;\n else if (c === Char.openBracket) this.state = STATE.OPEN_ARRAY;\n else if (c === Char.t) this.state = STATE.TRUE;\n else if (c === Char.f) this.state = STATE.FALSE;\n else if (c === Char.n) this.state = STATE.NULL;\n else if (c === Char.minus) {\n // keep and continue\n this.numberNode += '-';\n } else if (Char._0 <= c && c <= Char._9) {\n this.numberNode += String.fromCharCode(c);\n this.state = STATE.NUMBER_DIGIT;\n } else this._error('Bad value');\n continue;\n\n case STATE.CLOSE_ARRAY:\n if (c === Char.comma) {\n this.stack.push(STATE.CLOSE_ARRAY);\n this._closeValue('onvalue');\n this.state = STATE.VALUE;\n } else if (c === Char.closeBracket) {\n this.emitNode('onclosearray');\n this.depth--;\n this.state = this.stack.pop() || STATE.VALUE;\n } else if (isWhitespace(c)) continue;\n else this._error('Bad array');\n continue;\n\n case STATE.STRING:\n if (this.textNode === undefined) {\n this.textNode = '';\n }\n\n // thanks thejh, this is an about 50% performance improvement.\n let starti = i - 1;\n let slashed = this.slashed;\n let unicodeI = this.unicodeI;\n // eslint-disable-next-line no-constant-condition, no-labels\n STRING_BIGLOOP: while (true) {\n // if (DEBUG) console.log(i, c, STATE[this.state], slashed);\n // zero means \"no unicode active\". 1-4 mean \"parse some more\". end after 4.\n while (unicodeI > 0) {\n this.unicodeS += String.fromCharCode(c);\n c = chunk.charCodeAt(i++);\n this.position++;\n if (unicodeI === 4) {\n // TODO this might be slow? well, probably not used too often anyway\n this.textNode += String.fromCharCode(parseInt(this.unicodeS as string, 16));\n unicodeI = 0;\n starti = i - 1;\n } else {\n unicodeI++;\n }\n // we can just break here: no stuff we skipped that still has to be sliced out or so\n // eslint-disable-next-line no-labels\n if (!c) break STRING_BIGLOOP;\n }\n if (c === Char.doubleQuote && !slashed) {\n this.state = this.stack.pop() || STATE.VALUE;\n this.textNode += chunk.substring(starti, i - 1);\n this.position += i - 1 - starti;\n break;\n }\n if (c === Char.backslash && !slashed) {\n slashed = true;\n this.textNode += chunk.substring(starti, i - 1);\n this.position += i - 1 - starti;\n c = chunk.charCodeAt(i++);\n this.position++;\n if (!c) break;\n }\n if (slashed) {\n slashed = false;\n if (c === Char.n) {\n this.textNode += '\\n';\n } else if (c === Char.r) {\n this.textNode += '\\r';\n } else if (c === Char.t) {\n this.textNode += '\\t';\n } else if (c === Char.f) {\n this.textNode += '\\f';\n } else if (c === Char.b) {\n this.textNode += '\\b';\n } else if (c === Char.u) {\n // \\uxxxx. meh!\n unicodeI = 1;\n this.unicodeS = '';\n } else {\n this.textNode += String.fromCharCode(c);\n }\n c = chunk.charCodeAt(i++);\n this.position++;\n starti = i - 1;\n if (!c) break;\n else continue;\n }\n\n stringTokenPattern.lastIndex = i;\n const reResult = stringTokenPattern.exec(chunk);\n if (reResult === null) {\n i = chunk.length + 1;\n this.textNode += chunk.substring(starti, i - 1);\n this.position += i - 1 - starti;\n break;\n }\n i = reResult.index + 1;\n c = chunk.charCodeAt(reResult.index);\n if (!c) {\n this.textNode += chunk.substring(starti, i - 1);\n this.position += i - 1 - starti;\n break;\n }\n }\n this.slashed = slashed;\n this.unicodeI = unicodeI;\n continue;\n\n case STATE.TRUE:\n if (c === Char.r) this.state = STATE.TRUE2;\n else this._error(`Invalid true started with t${c}`);\n continue;\n\n case STATE.TRUE2:\n if (c === Char.u) this.state = STATE.TRUE3;\n else this._error(`Invalid true started with tr${c}`);\n continue;\n\n case STATE.TRUE3:\n if (c === Char.e) {\n this.emit('onvalue', true);\n this.state = this.stack.pop() || STATE.VALUE;\n } else this._error(`Invalid true started with tru${c}`);\n continue;\n\n case STATE.FALSE:\n if (c === Char.a) this.state = STATE.FALSE2;\n else this._error(`Invalid false started with f${c}`);\n continue;\n\n case STATE.FALSE2:\n if (c === Char.l) this.state = STATE.FALSE3;\n else this._error(`Invalid false started with fa${c}`);\n continue;\n\n case STATE.FALSE3:\n if (c === Char.s) this.state = STATE.FALSE4;\n else this._error(`Invalid false started with fal${c}`);\n continue;\n\n case STATE.FALSE4:\n if (c === Char.e) {\n this.emit('onvalue', false);\n this.state = this.stack.pop() || STATE.VALUE;\n } else this._error(`Invalid false started with fals${c}`);\n continue;\n\n case STATE.NULL:\n if (c === Char.u) this.state = STATE.NULL2;\n else this._error(`Invalid null started with n${c}`);\n continue;\n\n case STATE.NULL2:\n if (c === Char.l) this.state = STATE.NULL3;\n else this._error(`Invalid null started with nu${c}`);\n continue;\n\n case STATE.NULL3:\n if (c === Char.l) {\n this.emit('onvalue', null);\n this.state = this.stack.pop() || STATE.VALUE;\n } else this._error(`Invalid null started with nul${c}`);\n continue;\n\n case STATE.NUMBER_DECIMAL_POINT:\n if (c === Char.period) {\n this.numberNode += '.';\n this.state = STATE.NUMBER_DIGIT;\n } else this._error('Leading zero not followed by .');\n continue;\n\n case STATE.NUMBER_DIGIT:\n if (Char._0 <= c && c <= Char._9) this.numberNode += String.fromCharCode(c);\n else if (c === Char.period) {\n if (this.numberNode.indexOf('.') !== -1) this._error('Invalid number has two dots');\n this.numberNode += '.';\n } else if (c === Char.e || c === Char.E) {\n if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1)\n this._error('Invalid number has two exponential');\n this.numberNode += 'e';\n } else if (c === Char.plus || c === Char.minus) {\n // @ts-expect-error\n if (!(p === Char.e || p === Char.E)) this._error('Invalid symbol in number');\n this.numberNode += String.fromCharCode(c);\n } else {\n this._closeNumber();\n i--; // go back one\n this.state = this.stack.pop() || STATE.VALUE;\n }\n continue;\n\n default:\n this._error(`Unknown state: ${this.state}`);\n }\n }\n if (this.position >= this.bufferCheckPosition) {\n checkBufferLength(this);\n }\n\n this.emit('onchunkparsed');\n\n return this;\n }\n\n _closeValue(event: string = 'onvalue'): void {\n if (this.textNode !== undefined) {\n this.emit(event, this.textNode);\n }\n this.textNode = undefined;\n }\n\n _closeNumber(): void {\n if (this.numberNode) this.emit('onvalue', parseFloat(this.numberNode));\n this.numberNode = '';\n }\n\n _error(message: string = ''): void {\n this._closeValue();\n message += `\\nLine: ${this.line}\\nColumn: ${this.column}\\nChar: ${this.c}`;\n const error = new Error(message);\n this.error = error;\n this.emit('onerror', error);\n }\n}\n\nfunction isWhitespace(c): boolean {\n return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;\n}\n\nfunction checkBufferLength(parser) {\n const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);\n let maxActual = 0;\n\n for (const buffer of ['textNode', 'numberNode']) {\n const len = parser[buffer] === undefined ? 0 : parser[buffer].length;\n if (len > maxAllowed) {\n switch (buffer) {\n case 'text':\n // TODO - should this be closeValue?\n // closeText(parser);\n break;\n\n default:\n parser._error(`Max buffer length exceeded: ${buffer}`);\n }\n }\n maxActual = Math.max(maxActual, len);\n }\n parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\n/* eslint-disable no-continue */\n\n/**\n * A parser for a minimal subset of the jsonpath standard\n * Full JSON path parsers for JS exist but are quite large (bundle size)\n *\n * Supports\n *\n * `$.component.component.component`\n */\nexport default class JSONPath {\n path: string[];\n\n constructor(path: JSONPath | string[] | string | null = null) {\n this.path = parseJsonPath(path);\n }\n\n clone(): JSONPath {\n return new JSONPath(this);\n }\n\n toString(): string {\n return formatJsonPath(this.path);\n }\n\n push(name: string): void {\n this.path.push(name);\n }\n\n pop() {\n return this.path.pop();\n }\n\n set(name: string): void {\n this.path[this.path.length - 1] = name;\n }\n\n equals(other: JSONPath): boolean {\n if (!this || !other || this.path.length !== other.path.length) {\n return false;\n }\n\n for (let i = 0; i < this.path.length; ++i) {\n if (this.path[i] !== other.path[i]) {\n return false;\n }\n }\n\n return true;\n }\n\n /**\n * Sets the value pointed at by path\n * TODO - handle root path\n * @param object\n * @param value\n */\n setFieldAtPath(object, value) {\n const path = [...this.path];\n path.shift();\n const field = path.pop();\n for (const component of path) {\n object = object[component];\n }\n // @ts-ignore\n object[field] = value;\n }\n\n /**\n * Gets the value pointed at by path\n * TODO - handle root path\n * @param object\n */\n getFieldAtPath(object) {\n const path = [...this.path];\n path.shift();\n const field = path.pop();\n for (const component of path) {\n object = object[component];\n }\n // @ts-ignore\n return object[field];\n }\n}\n\ntype BracketSegment =\n | {type: 'property'; value: string; nextIndex: number}\n | {type: 'array-selector'; nextIndex: number};\n\nfunction parseJsonPath(path: JSONPath | string[] | string | null): string[] {\n if (path instanceof JSONPath) {\n return [...path.path];\n }\n\n if (Array.isArray(path)) {\n return ['$'].concat(path);\n }\n\n if (typeof path === 'string') {\n return parseJsonPathString(path);\n }\n\n return ['$'];\n}\n\n// eslint-disable-next-line complexity, max-statements\nfunction parseJsonPathString(pathString: string): string[] {\n const trimmedPath = pathString.trim();\n if (!trimmedPath.startsWith('$')) {\n throw new Error('JSONPath must start with $');\n }\n\n const segments: string[] = ['$'];\n let index = 1;\n let arrayElementSelectorEncountered = false;\n\n while (index < trimmedPath.length) {\n const character = trimmedPath[index];\n if (character === '.') {\n if (arrayElementSelectorEncountered) {\n throw new Error('JSONPath cannot select fields after array element selectors');\n }\n index += 1;\n if (trimmedPath[index] === '.') {\n throw new Error('JSONPath descendant selectors (..) are not supported');\n }\n const {value, nextIndex, isWildcard} = parseDotSegment(trimmedPath, index);\n if (isWildcard) {\n if (nextIndex < trimmedPath.length) {\n throw new Error('JSONPath wildcard selectors must terminate the path');\n }\n arrayElementSelectorEncountered = true;\n index = nextIndex;\n continue;\n }\n segments.push(value);\n index = nextIndex;\n continue;\n }\n\n if (character === '[') {\n const parsedSegment = parseBracketSegment(trimmedPath, index);\n if (parsedSegment.type === 'property') {\n if (arrayElementSelectorEncountered) {\n throw new Error('JSONPath cannot select fields after array element selectors');\n }\n segments.push(parsedSegment.value);\n } else {\n arrayElementSelectorEncountered = true;\n }\n index = parsedSegment.nextIndex;\n continue;\n }\n\n if (character === '@') {\n throw new Error('JSONPath current node selector (@) is not supported');\n }\n\n if (character.trim() === '') {\n index += 1;\n continue;\n }\n\n throw new Error(`Unexpected character \"${character}\" in JSONPath`);\n }\n\n return segments;\n}\n\nfunction parseDotSegment(\n pathString: string,\n startIndex: number\n): {\n value: string;\n nextIndex: number;\n isWildcard: boolean;\n} {\n if (startIndex >= pathString.length) {\n throw new Error('JSONPath cannot end with a period');\n }\n\n if (pathString[startIndex] === '*') {\n return {value: '*', nextIndex: startIndex + 1, isWildcard: true};\n }\n\n const firstCharacter = pathString[startIndex];\n if (firstCharacter === '@') {\n throw new Error('JSONPath current node selector (@) is not supported');\n }\n if (!isIdentifierStartCharacter(firstCharacter)) {\n throw new Error('JSONPath property names after period must start with a letter, $ or _');\n }\n\n let endIndex = startIndex + 1;\n while (endIndex < pathString.length && isIdentifierCharacter(pathString[endIndex])) {\n endIndex++;\n }\n\n if (endIndex === startIndex) {\n throw new Error('JSONPath is missing a property name after period');\n }\n\n return {\n value: pathString.slice(startIndex, endIndex),\n nextIndex: endIndex,\n isWildcard: false\n };\n}\n\nfunction parseBracketSegment(pathString: string, startIndex: number): BracketSegment {\n const contentStartIndex = startIndex + 1;\n if (contentStartIndex >= pathString.length) {\n throw new Error('JSONPath has unterminated bracket');\n }\n\n const firstCharacter = pathString[contentStartIndex];\n if (firstCharacter === \"'\" || firstCharacter === '\"') {\n const {value, nextIndex} = parseBracketProperty(pathString, contentStartIndex);\n return {type: 'property', value, nextIndex};\n }\n\n const closingBracketIndex = pathString.indexOf(']', contentStartIndex);\n if (closingBracketIndex === -1) {\n throw new Error('JSONPath has unterminated bracket');\n }\n\n const content = pathString.slice(contentStartIndex, closingBracketIndex).trim();\n const unsupportedSelectorMessage = getUnsupportedBracketSelectorMessage(content);\n if (unsupportedSelectorMessage) {\n throw new Error(unsupportedSelectorMessage);\n }\n if (content === '*') {\n return {type: 'array-selector', nextIndex: closingBracketIndex + 1};\n }\n\n if (/^\\d+$/.test(content)) {\n throw new Error('JSONPath array index selectors are not supported');\n }\n\n if (/^\\d*\\s*:\\s*\\d*(\\s*:\\s*\\d*)?$/.test(content)) {\n return {type: 'array-selector', nextIndex: closingBracketIndex + 1};\n }\n\n throw new Error(`Unsupported bracket selector \"[${content}]\" in JSONPath`);\n}\n\nfunction getUnsupportedBracketSelectorMessage(content: string): string | null {\n if (!content.length) {\n return 'JSONPath bracket selectors cannot be empty';\n }\n if (content.startsWith('(')) {\n return 'JSONPath script selectors are not supported';\n }\n if (content.startsWith('?')) {\n return 'JSONPath filter selectors are not supported';\n }\n if (content.includes(',')) {\n return 'JSONPath union selectors are not supported';\n }\n if (content.startsWith('@') || content.includes('@.')) {\n return 'JSONPath current node selector (@) is not supported';\n }\n return null;\n}\n\n// eslint-disable-next-line complexity, max-statements\nfunction parseBracketProperty(\n pathString: string,\n startIndex: number\n): {\n value: string;\n nextIndex: number;\n} {\n const quoteCharacter = pathString[startIndex];\n let index = startIndex + 1;\n let value = '';\n let terminated = false;\n\n while (index < pathString.length) {\n const character = pathString[index];\n if (character === '\\\\') {\n index += 1;\n if (index >= pathString.length) {\n break;\n }\n value += pathString[index];\n index += 1;\n continue;\n }\n\n if (character === quoteCharacter) {\n terminated = true;\n index += 1;\n break;\n }\n\n value += character;\n index += 1;\n }\n\n if (!terminated) {\n throw new Error('JSONPath string in bracket property selector is unterminated');\n }\n\n while (index < pathString.length && pathString[index].trim() === '') {\n index += 1;\n }\n\n if (pathString[index] !== ']') {\n throw new Error('JSONPath property selectors must end with ]');\n }\n\n if (!value.length) {\n throw new Error('JSONPath property selectors cannot be empty');\n }\n\n return {value, nextIndex: index + 1};\n}\n\nfunction isIdentifierCharacter(character: string): boolean {\n return /[a-zA-Z0-9$_]/.test(character);\n}\n\nfunction isIdentifierStartCharacter(character: string): boolean {\n return /[a-zA-Z_$]/.test(character);\n}\n\nfunction isIdentifierSegment(segment: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(segment);\n}\n\nfunction formatJsonPath(path: string[]): string {\n return path\n .map((segment, index) => {\n if (index === 0) {\n return segment;\n }\n if (segment === '*') {\n return '.*';\n }\n if (isIdentifierSegment(segment)) {\n return `.${segment}`;\n }\n const escapedSegment = segment.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\");\n return `['${escapedSegment}']`;\n })\n .join('');\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\n// @ts-nocheck\n\nimport ClarinetParser, {ClarinetParserOptions} from '../clarinet/clarinet';\nimport JSONPath from '../jsonpath/jsonpath';\n\n// JSONParser builds a JSON object using the events emitted by the Clarinet parser\n\nexport default class JSONParser {\n readonly parser: ClarinetParser;\n result = undefined;\n previousStates = [];\n currentState = Object.freeze({container: [], key: null});\n jsonpath: JSONPath = new JSONPath();\n\n constructor(options: ClarinetParserOptions) {\n this.reset();\n this.parser = new ClarinetParser({\n onready: () => {\n this.jsonpath = new JSONPath();\n this.previousStates.length = 0;\n this.currentState.container.length = 0;\n },\n\n onopenobject: (name) => {\n this._openObject({});\n if (typeof name !== 'undefined') {\n this.parser.emit('onkey', name);\n }\n },\n\n onkey: (name) => {\n this.jsonpath.set(name);\n this.currentState.key = name;\n },\n\n oncloseobject: () => {\n this._closeObject();\n },\n\n onopenarray: () => {\n this._openArray();\n },\n\n onclosearray: () => {\n this._closeArray();\n },\n\n onvalue: (value) => {\n this._pushOrSet(value);\n },\n\n onerror: (error) => {\n throw error;\n },\n\n onend: () => {\n this.result = this.currentState.container.pop();\n },\n\n ...options\n });\n }\n\n reset(): void {\n this.result = undefined;\n this.previousStates = [];\n this.currentState = Object.freeze({container: [], key: null});\n this.jsonpath = new JSONPath();\n }\n\n write(chunk): void {\n this.parser.write(chunk);\n }\n\n close(): void {\n this.parser.close();\n }\n\n // PRIVATE METHODS\n\n _pushOrSet(value): void {\n const {container, key} = this.currentState;\n if (key !== null) {\n container[key] = value;\n this.currentState.key = null;\n } else {\n container.push(value);\n }\n }\n\n _openArray(newContainer = []): void {\n this.jsonpath.push(null);\n this._pushOrSet(newContainer);\n this.previousStates.push(this.currentState);\n this.currentState = {container: newContainer, isArray: true, key: null};\n }\n\n _closeArray(): void {\n this.jsonpath.pop();\n this.currentState = this.previousStates.pop();\n }\n\n _openObject(newContainer = {}): void {\n this.jsonpath.push(null);\n this._pushOrSet(newContainer);\n this.previousStates.push(this.currentState);\n this.currentState = {container: newContainer, isArray: false, key: null};\n }\n\n _closeObject(): void {\n this.jsonpath.pop();\n this.currentState = this.previousStates.pop();\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport {default as JSONParser} from './json-parser';\nimport JSONPath from '../jsonpath/jsonpath';\n\n/**\n * The `StreamingJSONParser` looks for the first array in the JSON structure.\n * and emits an array of chunks\n */\nexport default class StreamingJSONParser extends JSONParser {\n private jsonPaths: JSONPath[];\n private streamingJsonPath: JSONPath | null = null;\n private streamingArray: any[] | null = null;\n private topLevelObject: object | null = null;\n\n constructor(options: {[key: string]: any} = {}) {\n super({\n onopenarray: () => {\n if (!this.streamingArray) {\n if (this._matchJSONPath()) {\n // @ts-ignore\n this.streamingJsonPath = this.getJsonPath().clone();\n this.streamingArray = [];\n this._openArray(this.streamingArray as []);\n return;\n }\n }\n\n this._openArray();\n },\n\n // Redefine onopenarray to inject value for top-level object\n onopenobject: (name) => {\n if (!this.topLevelObject) {\n this.topLevelObject = {};\n this._openObject(this.topLevelObject);\n } else {\n this._openObject({});\n }\n if (typeof name !== 'undefined') {\n this.parser.emit('onkey', name);\n }\n }\n });\n const jsonpaths = options.jsonpaths || [];\n this.jsonPaths = jsonpaths.map((jsonpath) => new JSONPath(jsonpath));\n }\n\n /**\n * write REDEFINITION\n * - super.write() chunk to parser\n * - get the contents (so far) of \"topmost-level\" array as batch of rows\n * - clear top-level array\n * - return the batch of rows\\\n */\n write(chunk) {\n super.write(chunk);\n let array: any[] = [];\n if (this.streamingArray) {\n array = [...this.streamingArray];\n this.streamingArray.length = 0;\n }\n return array;\n }\n\n /**\n * Returns a partially formed result object\n * Useful for returning the \"wrapper\" object when array is not top level\n * e.g. GeoJSON\n */\n getPartialResult() {\n return this.topLevelObject;\n }\n\n getStreamingJsonPath() {\n return this.streamingJsonPath;\n }\n\n getStreamingJsonPathAsString() {\n return this.streamingJsonPath && this.streamingJsonPath.toString();\n }\n\n getJsonPath() {\n return this.jsonpath;\n }\n\n // PRIVATE METHODS\n\n /**\n * Checks is this.getJsonPath matches the jsonpaths provided in options\n */\n _matchJSONPath() {\n const currentPath = this.getJsonPath();\n // console.debug(`Testing JSONPath`, currentPath);\n\n // Backwards compatibility, match any array\n // TODO implement using wildcard once that is supported\n if (this.jsonPaths.length === 0) {\n return true;\n }\n\n for (const jsonPath of this.jsonPaths) {\n if (jsonPath.equals(currentPath)) {\n return true;\n }\n }\n\n return false;\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {Table, TableBatch, Batch} from '@loaders.gl/schema';\nimport type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';\nimport {parseJSONSync} from './lib/parsers/parse-json';\nimport {parseJSONInBatches} from './lib/parsers/parse-json-in-batches';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nexport type MetadataBatch = Batch & {\n shape: 'metadata';\n};\n\nexport type JSONBatch = Batch & {\n shape: 'json';\n /** JSON data */\n container: any;\n};\n\n/**\n * @param table -\n * @param jsonpaths -\n */\nexport type JSONLoaderOptions = LoaderOptions & {\n json?: {\n /** Not specifying shape leaves avoids changes */\n shape?: 'object-row-table' | 'array-row-table';\n table?: boolean;\n jsonpaths?: string[];\n };\n};\n\nexport const JSONLoader = {\n dataType: null as unknown as Table,\n batchType: null as unknown as TableBatch | MetadataBatch | JSONBatch,\n\n name: 'JSON',\n id: 'json',\n module: 'json',\n version: VERSION,\n extensions: ['json', 'geojson'],\n mimeTypes: ['application/json'],\n category: 'table',\n text: true,\n options: {\n json: {\n shape: undefined,\n table: false,\n jsonpaths: []\n // batchSize: 'auto'\n }\n },\n parse,\n parseTextSync,\n parseInBatches\n} as const satisfies LoaderWithParser<\n Table,\n TableBatch | MetadataBatch | JSONBatch,\n JSONLoaderOptions\n>;\n\nasync function parse(arrayBuffer: ArrayBuffer, options?: JSONLoaderOptions) {\n return parseTextSync(new TextDecoder().decode(arrayBuffer), options);\n}\n\nfunction parseTextSync(text: string, options?: JSONLoaderOptions) {\n const jsonOptions = {...options, json: {...JSONLoader.options.json, ...options?.json}};\n return parseJSONSync(text, jsonOptions as JSONLoaderOptions);\n}\n\nfunction parseInBatches(\n asyncIterator:\n | AsyncIterable<ArrayBufferLike | ArrayBufferView>\n | Iterable<ArrayBufferLike | ArrayBufferView>,\n options?: JSONLoaderOptions\n): AsyncIterable<TableBatch | MetadataBatch | JSONBatch> {\n const jsonOptions = {...options, json: {...JSONLoader.options.json, ...options?.json}};\n return parseJSONInBatches(asyncIterator, jsonOptions as JSONLoaderOptions);\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {ArrayRowTable, ObjectRowTable} from '@loaders.gl/schema';\nimport {makeTableFromData} from '@loaders.gl/schema-utils';\n\nexport function parseNDJSONSync(ndjsonText: string): ArrayRowTable | ObjectRowTable {\n const lines = ndjsonText.trim().split('\\n');\n const parsedLines = lines.map((line, counter) => {\n try {\n return JSON.parse(line);\n } catch (error) {\n throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter + 1}`);\n }\n });\n\n return makeTableFromData(parsedLines);\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {TableBatch} from '@loaders.gl/schema';\nimport {TableBatchBuilder} from '@loaders.gl/schema-utils';\nimport {\n LoaderOptions,\n makeLineIterator,\n makeNumberedLineIterator,\n makeTextDecoderIterator,\n toArrayBufferIterator\n} from '@loaders.gl/loader-utils';\n\nexport async function* parseNDJSONInBatches(\n binaryAsyncIterator:\n | AsyncIterable<ArrayBufferLike | ArrayBufferView>\n | Iterable<ArrayBufferLike | ArrayBufferView>,\n options?: LoaderOptions\n): AsyncIterable<TableBatch> {\n const textIterator = makeTextDecoderIterator(toArrayBufferIterator(binaryAsyncIterator));\n const lineIterator = makeLineIterator(textIterator);\n const numberedLineIterator = makeNumberedLineIterator(lineIterator);\n\n const schema = null;\n const shape = 'row-table';\n // @ts-ignore\n const tableBatchBuilder = new TableBatchBuilder(schema, {\n ...(options?.core || options),\n shape\n });\n\n for await (const {counter, line} of numberedLineIterator) {\n try {\n const row = JSON.parse(line);\n tableBatchBuilder.addRow(row);\n tableBatchBuilder.chunkComplete(line);\n const batch = tableBatchBuilder.getFullBatch();\n if (batch) {\n yield batch;\n }\n } catch (error) {\n throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter}`);\n }\n }\n\n const batch = tableBatchBuilder.getFinalBatch();\n if (batch) {\n yield batch;\n }\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';\nimport {ObjectRowTable, ArrayRowTable, TableBatch} from '@loaders.gl/schema';\nimport {parseNDJSONSync} from './lib/parsers/parse-ndjson';\nimport {parseNDJSONInBatches} from './lib/parsers/parse-ndjson-in-batches';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nexport const NDJSONLoader = {\n dataType: null as unknown as ArrayRowTable | ObjectRowTable,\n batchType: null as unknown as TableBatch,\n\n name: 'NDJSON',\n id: 'ndjson',\n module: 'json',\n version: VERSION,\n extensions: ['ndjson', 'jsonl'],\n mimeTypes: [\n 'application/x-ndjson',\n 'application/jsonlines', // https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-inference.html#cm-batch\n 'application/json-seq'\n ],\n category: 'table',\n text: true,\n parse: async (arrayBuffer: ArrayBuffer) => parseNDJSONSync(new TextDecoder().decode(arrayBuffer)),\n parseTextSync: parseNDJSONSync,\n parseInBatches: parseNDJSONInBatches,\n options: {}\n} as const satisfies LoaderWithParser<ObjectRowTable | ArrayRowTable, TableBatch, LoaderOptions>;\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n// Copyright 2022 Foursquare Labs, Inc.\n\nimport type {Table} from '@loaders.gl/schema';\nimport {makeRowIterator} from '@loaders.gl/schema-utils';\nimport type {JSONWriterOptions} from '../../json-writer';\n\n/**\n * Encode a table as a JSON string\n */\nexport function encodeTableAsJSON(table: Table, options?: JSONWriterOptions): string {\n const shape = options?.json?.shape || 'object-row-table';\n\n const strings: string[] = [];\n const rowIterator = makeRowIterator(table, shape);\n for (const row of rowIterator) {\n // Round elements etc\n // processRow(wrappedRow, table.schema);\n // const wrappedRow = options.wrapper ? options.wrapper(row) : row;\n strings.push(JSON.stringify(row));\n }\n return `[${strings.join(',')}]`;\n}\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n// Copyright 2022 Foursquare Labs, Inc.\n\n/* global TextEncoder */\nimport type {WriterWithEncoder, WriterOptions} from '@loaders.gl/loader-utils';\nimport type {Table, TableBatch} from '@loaders.gl/schema';\nimport {encodeTableAsJSON} from './lib/encoders/json-encoder';\n\nexport type JSONWriterOptions = WriterOptions & {\n json?: {\n shape?: 'object-row-table' | 'array-row-table';\n wrapper?: (table: TableJSON) => unknown;\n };\n};\n\ntype RowArray = unknown[];\ntype RowObject = {[key: string]: unknown};\ntype TableJSON = RowArray[] | RowObject[];\n\nexport const JSONWriter = {\n id: 'json',\n version: 'latest',\n module: 'json',\n name: 'JSON',\n extensions: ['json'],\n mimeTypes: ['application/json'],\n options: {},\n text: true,\n encode: async (table: Table, options: JSONWriterOptions) =>\n new TextEncoder().encode(encodeTableAsJSON(table, options)).buffer,\n encodeTextSync: (table: Table, options: JSONWriterOptions) => encodeTableAsJSON(table, options)\n} as const satisfies WriterWithEncoder<Table, TableBatch, JSONWriterOptions>;\n", "// loaders.gl\n// SPDX-License-Identifier: MIT\n// Copyright (c) vis.gl contributors\n\nimport type {Loader, LoaderWithParser} from '@loaders.gl/loader-utils';\nimport type {BinaryFeatureCollection, GeoJSONTable, TableBatch} from '@loaders.gl/schema';\nimport type {JSONLoaderOptions} from './json-loader';\nimport {geojsonToBinary} from '@loaders.gl/gis';\n// import {parseJSONSync} from './lib/parsers/parse-json';\nimport {parseJSONInBatches} from './lib/parsers/parse-json-in-batches';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nexport type GeoJSONLoaderOptions = JSONLoaderOptions & {\n geojson?: {\n shape?: 'geojson-table';\n };\n gis?: {\n format?: 'geojson' | 'binary';\n };\n};\n\n/**\n * GeoJSON loader\n */\nexport const GeoJSONWorkerLoader = {\n dataType: null as unknown as GeoJSONTable,\n batchType: null as unknown as TableBatch,\n\n name: 'GeoJSON',\n id: 'geojson',\n module: 'geojson',\n version: VERSION,\n worker: true,\n extensions: ['geojson'],\n mimeTypes: ['application/geo+json'],\n category: 'geometry',\n text: true,\n options: {\n geojson: {\n shape: 'geojson-table'\n },\n json: {\n shape: 'object-row-table',\n jsonpaths: ['$', '$.features']\n },\n gis: {\n format: 'geojson'\n }\n }\n} as const satisfies Loader<GeoJSONTable, TableBatch, GeoJSONLoaderOptions>;\n\nexport const GeoJSONLoader = {\n ...GeoJSONWorkerLoader,\n // @ts-expect-error\n parse,\n // @ts-expect-error\n parseTextSync,\n parseInBatches\n} as const satisfies LoaderWithParser<GeoJSONTable, TableBatch, GeoJSONLoaderOptions>;\n\nasync function parse(\n arrayBuffer: ArrayBuffer,\n options?: GeoJSONLoaderOptions\n): Promise<GeoJSONTable | BinaryFeatureCollection> {\n return parseTextSync(new TextDecoder().decode(arrayBuffer), options);\n}\n\nfunction parseTextSync(\n text: string,\n options?: GeoJSONLoaderOptions\n): GeoJSONTable | BinaryFeatureCollection {\n // Apps can call the parse method directly, we so apply default options here\n options = {...GeoJSONLoader.options, ...options};\n options.geojson = {...GeoJSONLoader.options.geojson, ...options.geojson};\n options.gis = options.gis || {};\n\n let geojson;\n try {\n geojson = JSON.parse(text);\n } catch {\n geojson = {};\n }\n\n const table: GeoJSONTable = {\n shape: 'geojson-table',\n // TODO - deduce schema from geojson\n // TODO check that parsed data is of type FeatureCollection\n type: 'FeatureCollection',\n features: geojson?.features || []\n };\n\n switch (options.gis.format) {\n case 'binary':\n return geojsonToBinary(table.features);\n default:\n return table;\n }\n}\n\nfunction parseInBatches(asyncIterator, options): AsyncIterable<TableBatch> {\n // Apps can call the parse method directly, we so apply default options here\n options = {...GeoJSONLoader.options, ...options};\n options.json = {...GeoJSONLoader.options.json, ...options.json};\n options.geojson = {...GeoJSONLoader.options.geojson, ...options.geojson};\n\n const geojsonIterator = parseJSONInBatches(asyncI