@loaders.gl/json 4.2.0-alpha.4 → 4.2.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/dist.dev.js +319 -366
  2. package/dist/dist.min.js +21 -0
  3. package/dist/geojson-loader.d.ts +1 -1
  4. package/dist/geojson-loader.d.ts.map +1 -1
  5. package/dist/geojson-loader.js +75 -73
  6. package/dist/geojson-worker.js +78 -54
  7. package/dist/geojson-writer.js +23 -20
  8. package/dist/index.cjs +155 -121
  9. package/dist/index.cjs.map +7 -0
  10. package/dist/index.d.ts +12 -12
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -1
  13. package/dist/json-loader.js +30 -37
  14. package/dist/json-writer.js +14 -11
  15. package/dist/lib/clarinet/clarinet.js +513 -391
  16. package/dist/lib/encoder-utils/encode-table-row.d.ts +1 -1
  17. package/dist/lib/encoder-utils/encode-table-row.d.ts.map +1 -1
  18. package/dist/lib/encoder-utils/encode-table-row.js +46 -37
  19. package/dist/lib/encoder-utils/encode-utils.js +38 -25
  20. package/dist/lib/encoder-utils/utf8-encoder.js +26 -27
  21. package/dist/lib/encoders/geojson-encoder.d.ts +1 -1
  22. package/dist/lib/encoders/geojson-encoder.d.ts.map +1 -1
  23. package/dist/lib/encoders/geojson-encoder.js +43 -40
  24. package/dist/lib/encoders/json-encoder.d.ts +1 -1
  25. package/dist/lib/encoders/json-encoder.d.ts.map +1 -1
  26. package/dist/lib/encoders/json-encoder.js +17 -9
  27. package/dist/lib/json-parser/json-parser.d.ts +2 -2
  28. package/dist/lib/json-parser/json-parser.d.ts.map +1 -1
  29. package/dist/lib/json-parser/json-parser.js +86 -102
  30. package/dist/lib/json-parser/streaming-json-parser.d.ts +2 -2
  31. package/dist/lib/json-parser/streaming-json-parser.d.ts.map +1 -1
  32. package/dist/lib/json-parser/streaming-json-parser.js +87 -63
  33. package/dist/lib/jsonpath/jsonpath.js +78 -57
  34. package/dist/lib/parsers/parse-json-in-batches.d.ts +1 -1
  35. package/dist/lib/parsers/parse-json-in-batches.d.ts.map +1 -1
  36. package/dist/lib/parsers/parse-json-in-batches.js +76 -75
  37. package/dist/lib/parsers/parse-json.d.ts +1 -1
  38. package/dist/lib/parsers/parse-json.d.ts.map +1 -1
  39. package/dist/lib/parsers/parse-json.js +21 -22
  40. package/dist/lib/parsers/parse-ndjson-in-batches.js +26 -28
  41. package/dist/lib/parsers/parse-ndjson.js +10 -10
  42. package/dist/ndgeoson-loader.js +28 -20
  43. package/dist/ndjson-loader.js +22 -14
  44. package/dist/workers/geojson-worker.js +0 -1
  45. package/package.json +11 -7
  46. package/dist/geojson-loader.js.map +0 -1
  47. package/dist/geojson-writer.js.map +0 -1
  48. package/dist/index.js.map +0 -1
  49. package/dist/json-loader.js.map +0 -1
  50. package/dist/json-writer.js.map +0 -1
  51. package/dist/lib/clarinet/LICENSE +0 -28
  52. package/dist/lib/clarinet/clarinet.js.map +0 -1
  53. package/dist/lib/encoder-utils/encode-table-row.js.map +0 -1
  54. package/dist/lib/encoder-utils/encode-utils.js.map +0 -1
  55. package/dist/lib/encoder-utils/utf8-encoder.js.map +0 -1
  56. package/dist/lib/encoders/geojson-encoder.js.map +0 -1
  57. package/dist/lib/encoders/json-encoder.js.map +0 -1
  58. package/dist/lib/json-parser/json-parser.js.map +0 -1
  59. package/dist/lib/json-parser/streaming-json-parser.js.map +0 -1
  60. package/dist/lib/jsonpath/jsonpath.js.map +0 -1
  61. package/dist/lib/parsers/parse-json-in-batches.js.map +0 -1
  62. package/dist/lib/parsers/parse-json.js.map +0 -1
  63. package/dist/lib/parsers/parse-ndjson-in-batches.js.map +0 -1
  64. package/dist/lib/parsers/parse-ndjson.js.map +0 -1
  65. package/dist/ndgeoson-loader.js.map +0 -1
  66. package/dist/ndjson-loader.js.map +0 -1
  67. package/dist/workers/geojson-worker.js.map +0 -1
@@ -1,71 +1,95 @@
1
1
  import { default as JSONParser } from "./json-parser.js";
2
2
  import JSONPath from "../jsonpath/jsonpath.js";
3
+ /**
4
+ * The `StreamingJSONParser` looks for the first array in the JSON structure.
5
+ * and emits an array of chunks
6
+ */
3
7
  export default class StreamingJSONParser extends JSONParser {
4
- constructor() {
5
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
6
- super({
7
- onopenarray: () => {
8
- if (!this.streamingArray) {
9
- if (this._matchJSONPath()) {
10
- this.streamingJsonPath = this.getJsonPath().clone();
11
- this.streamingArray = [];
12
- this._openArray(this.streamingArray);
13
- return;
14
- }
15
- }
16
- this._openArray();
17
- },
18
- onopenobject: name => {
19
- if (!this.topLevelObject) {
20
- this.topLevelObject = {};
21
- this._openObject(this.topLevelObject);
22
- } else {
23
- this._openObject({});
24
- }
25
- if (typeof name !== 'undefined') {
26
- this.parser.emit('onkey', name);
8
+ jsonPaths;
9
+ streamingJsonPath = null;
10
+ streamingArray = null;
11
+ topLevelObject = null;
12
+ constructor(options = {}) {
13
+ super({
14
+ onopenarray: () => {
15
+ if (!this.streamingArray) {
16
+ if (this._matchJSONPath()) {
17
+ // @ts-ignore
18
+ this.streamingJsonPath = this.getJsonPath().clone();
19
+ this.streamingArray = [];
20
+ this._openArray(this.streamingArray);
21
+ return;
22
+ }
23
+ }
24
+ this._openArray();
25
+ },
26
+ // Redefine onopenarray to inject value for top-level object
27
+ onopenobject: (name) => {
28
+ if (!this.topLevelObject) {
29
+ this.topLevelObject = {};
30
+ this._openObject(this.topLevelObject);
31
+ }
32
+ else {
33
+ this._openObject({});
34
+ }
35
+ if (typeof name !== 'undefined') {
36
+ this.parser.emit('onkey', name);
37
+ }
38
+ }
39
+ });
40
+ const jsonpaths = options.jsonpaths || [];
41
+ this.jsonPaths = jsonpaths.map((jsonpath) => new JSONPath(jsonpath));
42
+ }
43
+ /**
44
+ * write REDEFINITION
45
+ * - super.write() chunk to parser
46
+ * - get the contents (so far) of "topmost-level" array as batch of rows
47
+ * - clear top-level array
48
+ * - return the batch of rows\
49
+ */
50
+ write(chunk) {
51
+ super.write(chunk);
52
+ let array = [];
53
+ if (this.streamingArray) {
54
+ array = [...this.streamingArray];
55
+ this.streamingArray.length = 0;
27
56
  }
28
- }
29
- });
30
- this.jsonPaths = void 0;
31
- this.streamingJsonPath = null;
32
- this.streamingArray = null;
33
- this.topLevelObject = null;
34
- const jsonpaths = options.jsonpaths || [];
35
- this.jsonPaths = jsonpaths.map(jsonpath => new JSONPath(jsonpath));
36
- }
37
- write(chunk) {
38
- super.write(chunk);
39
- let array = [];
40
- if (this.streamingArray) {
41
- array = [...this.streamingArray];
42
- this.streamingArray.length = 0;
57
+ return array;
58
+ }
59
+ /**
60
+ * Returns a partially formed result object
61
+ * Useful for returning the "wrapper" object when array is not top level
62
+ * e.g. GeoJSON
63
+ */
64
+ getPartialResult() {
65
+ return this.topLevelObject;
43
66
  }
44
- return array;
45
- }
46
- getPartialResult() {
47
- return this.topLevelObject;
48
- }
49
- getStreamingJsonPath() {
50
- return this.streamingJsonPath;
51
- }
52
- getStreamingJsonPathAsString() {
53
- return this.streamingJsonPath && this.streamingJsonPath.toString();
54
- }
55
- getJsonPath() {
56
- return this.jsonpath;
57
- }
58
- _matchJSONPath() {
59
- const currentPath = this.getJsonPath();
60
- if (this.jsonPaths.length === 0) {
61
- return true;
67
+ getStreamingJsonPath() {
68
+ return this.streamingJsonPath;
62
69
  }
63
- for (const jsonPath of this.jsonPaths) {
64
- if (jsonPath.equals(currentPath)) {
65
- return true;
66
- }
70
+ getStreamingJsonPathAsString() {
71
+ return this.streamingJsonPath && this.streamingJsonPath.toString();
72
+ }
73
+ getJsonPath() {
74
+ return this.jsonpath;
75
+ }
76
+ // PRIVATE METHODS
77
+ /**
78
+ * Checks is this.getJsonPath matches the jsonpaths provided in options
79
+ */
80
+ _matchJSONPath() {
81
+ const currentPath = this.getJsonPath();
82
+ // console.debug(`Testing JSONPath`, currentPath);
83
+ // Backwards compatibility, match any array
84
+ // TODO implement using wildcard once that is supported
85
+ if (this.jsonPaths.length === 0) {
86
+ return true;
87
+ }
88
+ for (const jsonPath of this.jsonPaths) {
89
+ if (jsonPath.equals(currentPath)) {
90
+ return true;
91
+ }
92
+ }
93
+ return false;
67
94
  }
68
- return false;
69
- }
70
95
  }
71
- //# sourceMappingURL=streaming-json-parser.js.map
@@ -1,66 +1,87 @@
1
+ /**
2
+ * A parser for a minimal subset of the jsonpath standard
3
+ * Full JSON path parsers for JS exist but are quite large (bundle size)
4
+ *
5
+ * Supports
6
+ *
7
+ * `$.component.component.component`
8
+ */
1
9
  export default class JSONPath {
2
- constructor() {
3
- let path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
4
- this.path = void 0;
5
- this.path = ['$'];
6
- if (path instanceof JSONPath) {
7
- this.path = [...path.path];
8
- return;
10
+ path;
11
+ constructor(path = null) {
12
+ this.path = ['$'];
13
+ if (path instanceof JSONPath) {
14
+ // @ts-ignore
15
+ this.path = [...path.path];
16
+ return;
17
+ }
18
+ if (Array.isArray(path)) {
19
+ this.path.push(...path);
20
+ return;
21
+ }
22
+ // Parse a string as a JSONPath
23
+ if (typeof path === 'string') {
24
+ this.path = path.split('.');
25
+ if (this.path[0] !== '$') {
26
+ throw new Error('JSONPaths must start with $');
27
+ }
28
+ }
9
29
  }
10
- if (Array.isArray(path)) {
11
- this.path.push(...path);
12
- return;
30
+ clone() {
31
+ return new JSONPath(this);
13
32
  }
14
- if (typeof path === 'string') {
15
- this.path = path.split('.');
16
- if (this.path[0] !== '$') {
17
- throw new Error('JSONPaths must start with $');
18
- }
33
+ toString() {
34
+ return this.path.join('.');
19
35
  }
20
- }
21
- clone() {
22
- return new JSONPath(this);
23
- }
24
- toString() {
25
- return this.path.join('.');
26
- }
27
- push(name) {
28
- this.path.push(name);
29
- }
30
- pop() {
31
- return this.path.pop();
32
- }
33
- set(name) {
34
- this.path[this.path.length - 1] = name;
35
- }
36
- equals(other) {
37
- if (!this || !other || this.path.length !== other.path.length) {
38
- return false;
36
+ push(name) {
37
+ this.path.push(name);
39
38
  }
40
- for (let i = 0; i < this.path.length; ++i) {
41
- if (this.path[i] !== other.path[i]) {
42
- return false;
43
- }
39
+ pop() {
40
+ return this.path.pop();
44
41
  }
45
- return true;
46
- }
47
- setFieldAtPath(object, value) {
48
- const path = [...this.path];
49
- path.shift();
50
- const field = path.pop();
51
- for (const component of path) {
52
- object = object[component];
42
+ set(name) {
43
+ this.path[this.path.length - 1] = name;
53
44
  }
54
- object[field] = value;
55
- }
56
- getFieldAtPath(object) {
57
- const path = [...this.path];
58
- path.shift();
59
- const field = path.pop();
60
- for (const component of path) {
61
- object = object[component];
45
+ equals(other) {
46
+ if (!this || !other || this.path.length !== other.path.length) {
47
+ return false;
48
+ }
49
+ for (let i = 0; i < this.path.length; ++i) {
50
+ if (this.path[i] !== other.path[i]) {
51
+ return false;
52
+ }
53
+ }
54
+ return true;
55
+ }
56
+ /**
57
+ * Sets the value pointed at by path
58
+ * TODO - handle root path
59
+ * @param object
60
+ * @param value
61
+ */
62
+ setFieldAtPath(object, value) {
63
+ const path = [...this.path];
64
+ path.shift();
65
+ const field = path.pop();
66
+ for (const component of path) {
67
+ object = object[component];
68
+ }
69
+ // @ts-ignore
70
+ object[field] = value;
71
+ }
72
+ /**
73
+ * Gets the value pointed at by path
74
+ * TODO - handle root path
75
+ * @param object
76
+ */
77
+ getFieldAtPath(object) {
78
+ const path = [...this.path];
79
+ path.shift();
80
+ const field = path.pop();
81
+ for (const component of path) {
82
+ object = object[component];
83
+ }
84
+ // @ts-ignore
85
+ return object[field];
62
86
  }
63
- return object[field];
64
- }
65
87
  }
66
- //# sourceMappingURL=jsonpath.js.map
@@ -1,5 +1,5 @@
1
1
  import type { TableBatch } from '@loaders.gl/schema';
2
- import type { JSONLoaderOptions, MetadataBatch, JSONBatch } from '../../json-loader';
2
+ import type { JSONLoaderOptions, MetadataBatch, JSONBatch } from "../../json-loader.js";
3
3
  export declare function parseJSONInBatches(binaryAsyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>, options: JSONLoaderOptions): AsyncIterable<TableBatch | MetadataBatch | JSONBatch>;
4
4
  export declare function rebuildJsonObject(batch: any, data: any): any;
5
5
  //# sourceMappingURL=parse-json-in-batches.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-json-in-batches.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-json-in-batches.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAC,iBAAiB,EAAE,aAAa,EAAE,SAAS,EAAC,MAAM,mBAAmB,CAAC;AAQnF,wBAAuB,kBAAkB,CACvC,mBAAmB,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,EACvE,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,UAAU,GAAG,aAAa,GAAG,SAAS,CAAC,CA2EvD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,KAAA,EAAE,IAAI,KAAA,OAmB5C"}
1
+ {"version":3,"file":"parse-json-in-batches.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-json-in-batches.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAS,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAC,iBAAiB,EAAE,aAAa,EAAE,SAAS,EAAC,6BAA0B;AAQnF,wBAAuB,kBAAkB,CACvC,mBAAmB,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,EACvE,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,UAAU,GAAG,aAAa,GAAG,SAAS,CAAC,CA2EvD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,KAAA,EAAE,IAAI,KAAA,OAmB5C"}
@@ -2,86 +2,87 @@ import { TableBatchBuilder } from '@loaders.gl/schema';
2
2
  import { assert, makeTextDecoderIterator } from '@loaders.gl/loader-utils';
3
3
  import StreamingJSONParser from "../json-parser/streaming-json-parser.js";
4
4
  import JSONPath from "../jsonpath/jsonpath.js";
5
+ // TODO - support batch size 0 = no batching/single batch?
6
+ // eslint-disable-next-line max-statements, complexity
5
7
  export async function* parseJSONInBatches(binaryAsyncIterator, options) {
6
- const asyncIterator = makeTextDecoderIterator(binaryAsyncIterator);
7
- const {
8
- metadata
9
- } = options;
10
- const {
11
- jsonpaths
12
- } = options.json || {};
13
- let isFirstChunk = true;
14
- const schema = null;
15
- const tableBatchBuilder = new TableBatchBuilder(schema, options);
16
- const parser = new StreamingJSONParser({
17
- jsonpaths
18
- });
19
- for await (const chunk of asyncIterator) {
20
- const rows = parser.write(chunk);
21
- const jsonpath = rows.length > 0 && parser.getStreamingJsonPathAsString();
22
- if (rows.length > 0 && isFirstChunk) {
23
- if (metadata) {
24
- var _options$json;
25
- const initialBatch = {
26
- shape: (options === null || options === void 0 ? void 0 : (_options$json = options.json) === null || _options$json === void 0 ? void 0 : _options$json.shape) || 'array-row-table',
27
- batchType: 'partial-result',
28
- data: [],
29
- length: 0,
30
- bytesUsed: 0,
31
- container: parser.getPartialResult(),
32
- jsonpath
33
- };
34
- yield initialBatch;
35
- }
36
- isFirstChunk = false;
8
+ const asyncIterator = makeTextDecoderIterator(binaryAsyncIterator);
9
+ const { metadata } = options;
10
+ const { jsonpaths } = options.json || {};
11
+ let isFirstChunk = true;
12
+ // @ts-expect-error TODO fix Schema deduction
13
+ const schema = null;
14
+ const tableBatchBuilder = new TableBatchBuilder(schema, options);
15
+ const parser = new StreamingJSONParser({ jsonpaths });
16
+ for await (const chunk of asyncIterator) {
17
+ const rows = parser.write(chunk);
18
+ const jsonpath = rows.length > 0 && parser.getStreamingJsonPathAsString();
19
+ if (rows.length > 0 && isFirstChunk) {
20
+ if (metadata) {
21
+ const initialBatch = {
22
+ // Common fields
23
+ shape: options?.json?.shape || 'array-row-table',
24
+ batchType: 'partial-result',
25
+ data: [],
26
+ length: 0,
27
+ bytesUsed: 0,
28
+ // JSON additions
29
+ container: parser.getPartialResult(),
30
+ jsonpath
31
+ };
32
+ yield initialBatch;
33
+ }
34
+ isFirstChunk = false;
35
+ // schema = deduceSchema(rows);
36
+ }
37
+ // Add the row
38
+ for (const row of rows) {
39
+ tableBatchBuilder.addRow(row);
40
+ // If a batch has been completed, emit it
41
+ const batch = tableBatchBuilder.getFullBatch({ jsonpath });
42
+ if (batch) {
43
+ yield batch;
44
+ }
45
+ }
46
+ tableBatchBuilder.chunkComplete(chunk);
47
+ const batch = tableBatchBuilder.getFullBatch({ jsonpath });
48
+ if (batch) {
49
+ yield batch;
50
+ }
37
51
  }
38
- for (const row of rows) {
39
- tableBatchBuilder.addRow(row);
40
- const batch = tableBatchBuilder.getFullBatch({
41
- jsonpath
42
- });
43
- if (batch) {
52
+ // yield final batch
53
+ const jsonpath = parser.getStreamingJsonPathAsString();
54
+ const batch = tableBatchBuilder.getFinalBatch({ jsonpath });
55
+ if (batch) {
44
56
  yield batch;
45
- }
46
57
  }
47
- tableBatchBuilder.chunkComplete(chunk);
48
- const batch = tableBatchBuilder.getFullBatch({
49
- jsonpath
50
- });
51
- if (batch) {
52
- yield batch;
58
+ if (metadata) {
59
+ const finalBatch = {
60
+ shape: 'json',
61
+ batchType: 'final-result',
62
+ container: parser.getPartialResult(),
63
+ jsonpath: parser.getStreamingJsonPathAsString(),
64
+ /** Data Just to avoid crashing? */
65
+ data: [],
66
+ length: 0
67
+ // schema: null
68
+ };
69
+ yield finalBatch;
53
70
  }
54
- }
55
- const jsonpath = parser.getStreamingJsonPathAsString();
56
- const batch = tableBatchBuilder.getFinalBatch({
57
- jsonpath
58
- });
59
- if (batch) {
60
- yield batch;
61
- }
62
- if (metadata) {
63
- const finalBatch = {
64
- shape: 'json',
65
- batchType: 'final-result',
66
- container: parser.getPartialResult(),
67
- jsonpath: parser.getStreamingJsonPathAsString(),
68
- data: [],
69
- length: 0
70
- };
71
- yield finalBatch;
72
- }
73
71
  }
74
72
  export function rebuildJsonObject(batch, data) {
75
- assert(batch.batchType === 'final-result');
76
- if (batch.jsonpath === '$') {
77
- return data;
78
- }
79
- if (batch.jsonpath && batch.jsonpath.length > 1) {
80
- const topLevelObject = batch.container;
81
- const streamingPath = new JSONPath(batch.jsonpath);
82
- streamingPath.setFieldAtPath(topLevelObject, data);
83
- return topLevelObject;
84
- }
85
- return batch.container;
73
+ // Last batch will have this special type and will provide all the root object of the parsed file
74
+ assert(batch.batchType === 'final-result');
75
+ // The streamed JSON data is a top level array (jsonpath = '$'), just return the array of row objects
76
+ if (batch.jsonpath === '$') {
77
+ return data;
78
+ }
79
+ // (jsonpath !== '$') The streamed data is not a top level array, so stitch it back in to the top-level object
80
+ if (batch.jsonpath && batch.jsonpath.length > 1) {
81
+ const topLevelObject = batch.container;
82
+ const streamingPath = new JSONPath(batch.jsonpath);
83
+ streamingPath.setFieldAtPath(topLevelObject, data);
84
+ return topLevelObject;
85
+ }
86
+ // No jsonpath, in this case nothing was streamed.
87
+ return batch.container;
86
88
  }
87
- //# sourceMappingURL=parse-json-in-batches.js.map
@@ -1,4 +1,4 @@
1
1
  import type { RowTable } from '@loaders.gl/schema';
2
- import type { JSONLoaderOptions } from '../../json-loader';
2
+ import type { JSONLoaderOptions } from "../../json-loader.js";
3
3
  export declare function parseJSONSync(jsonText: string, options: JSONLoaderOptions): RowTable;
4
4
  //# sourceMappingURL=parse-json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-json.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-json.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAEjD,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAEzD,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,GAAG,QAAQ,CAWpF"}
1
+ {"version":3,"file":"parse-json.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-json.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,oBAAoB,CAAC;AAEjD,OAAO,KAAK,EAAC,iBAAiB,EAAC,6BAA0B;AAEzD,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,GAAG,QAAQ,CAWpF"}
@@ -1,29 +1,28 @@
1
1
  import { makeTableFromData } from '@loaders.gl/schema';
2
2
  export function parseJSONSync(jsonText, options) {
3
- try {
4
- var _options$json;
5
- const json = JSON.parse(jsonText);
6
- if ((_options$json = options.json) !== null && _options$json !== void 0 && _options$json.table) {
7
- const data = getFirstArray(json) || json;
8
- return makeTableFromData(data);
3
+ try {
4
+ const json = JSON.parse(jsonText);
5
+ if (options.json?.table) {
6
+ const data = getFirstArray(json) || json;
7
+ return makeTableFromData(data);
8
+ }
9
+ return json;
10
+ }
11
+ catch (error) {
12
+ throw new Error('JSONLoader: failed to parse JSON');
9
13
  }
10
- return json;
11
- } catch (error) {
12
- throw new Error('JSONLoader: failed to parse JSON');
13
- }
14
14
  }
15
15
  function getFirstArray(json) {
16
- if (Array.isArray(json)) {
17
- return json;
18
- }
19
- if (json && typeof json === 'object') {
20
- for (const value of Object.values(json)) {
21
- const array = getFirstArray(value);
22
- if (array) {
23
- return array;
24
- }
16
+ if (Array.isArray(json)) {
17
+ return json;
18
+ }
19
+ if (json && typeof json === 'object') {
20
+ for (const value of Object.values(json)) {
21
+ const array = getFirstArray(value);
22
+ if (array) {
23
+ return array;
24
+ }
25
+ }
25
26
  }
26
- }
27
- return null;
27
+ return null;
28
28
  }
29
- //# sourceMappingURL=parse-json.js.map
@@ -1,34 +1,32 @@
1
1
  import { TableBatchBuilder } from '@loaders.gl/schema';
2
2
  import { makeLineIterator, makeNumberedLineIterator, makeTextDecoderIterator } from '@loaders.gl/loader-utils';
3
3
  export async function* parseNDJSONInBatches(binaryAsyncIterator, options) {
4
- const textIterator = makeTextDecoderIterator(binaryAsyncIterator);
5
- const lineIterator = makeLineIterator(textIterator);
6
- const numberedLineIterator = makeNumberedLineIterator(lineIterator);
7
- const schema = null;
8
- const shape = 'row-table';
9
- const tableBatchBuilder = new TableBatchBuilder(schema, {
10
- ...options,
11
- shape
12
- });
13
- for await (const {
14
- counter,
15
- line
16
- } of numberedLineIterator) {
17
- try {
18
- const row = JSON.parse(line);
19
- tableBatchBuilder.addRow(row);
20
- tableBatchBuilder.chunkComplete(line);
21
- const batch = tableBatchBuilder.getFullBatch();
22
- if (batch) {
4
+ const textIterator = makeTextDecoderIterator(binaryAsyncIterator);
5
+ const lineIterator = makeLineIterator(textIterator);
6
+ const numberedLineIterator = makeNumberedLineIterator(lineIterator);
7
+ const schema = null;
8
+ const shape = 'row-table';
9
+ // @ts-ignore
10
+ const tableBatchBuilder = new TableBatchBuilder(schema, {
11
+ ...options,
12
+ shape
13
+ });
14
+ for await (const { counter, line } of numberedLineIterator) {
15
+ try {
16
+ const row = JSON.parse(line);
17
+ tableBatchBuilder.addRow(row);
18
+ tableBatchBuilder.chunkComplete(line);
19
+ const batch = tableBatchBuilder.getFullBatch();
20
+ if (batch) {
21
+ yield batch;
22
+ }
23
+ }
24
+ catch (error) {
25
+ throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter}`);
26
+ }
27
+ }
28
+ const batch = tableBatchBuilder.getFinalBatch();
29
+ if (batch) {
23
30
  yield batch;
24
- }
25
- } catch (error) {
26
- throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter}`);
27
31
  }
28
- }
29
- const batch = tableBatchBuilder.getFinalBatch();
30
- if (batch) {
31
- yield batch;
32
- }
33
32
  }
34
- //# sourceMappingURL=parse-ndjson-in-batches.js.map
@@ -1,13 +1,13 @@
1
1
  import { makeTableFromData } from '@loaders.gl/schema';
2
2
  export function parseNDJSONSync(ndjsonText) {
3
- const lines = ndjsonText.trim().split('\n');
4
- const parsedLines = lines.map((line, counter) => {
5
- try {
6
- return JSON.parse(line);
7
- } catch (error) {
8
- throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter + 1}`);
9
- }
10
- });
11
- return makeTableFromData(parsedLines);
3
+ const lines = ndjsonText.trim().split('\n');
4
+ const parsedLines = lines.map((line, counter) => {
5
+ try {
6
+ return JSON.parse(line);
7
+ }
8
+ catch (error) {
9
+ throw new Error(`NDJSONLoader: failed to parse JSON on line ${counter + 1}`);
10
+ }
11
+ });
12
+ return makeTableFromData(parsedLines);
12
13
  }
13
- //# sourceMappingURL=parse-ndjson.js.map