@cobol-ts/cursor-loader-json 0.0.0-stage → 0.0.2

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.
package/README.md CHANGED
@@ -1,3 +1,144 @@
1
- # Temporary Holding Version
1
+ # `@cobol-ts/cursor-loader-json`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ JSON Lines (JSONL) parsing and projection support for the pooled cursor framework. This is the **reference implementation of recursive projected objects and arrays**: the parser has native JavaScript objects to select, while the shared projection framework controls the application interface, view identity, lifetimes and reuse.
4
+
5
+ JSONL is **not** byte-backed late materialisation of its values. `JSON.parse()` constructs the full per-record JSON representation. The value of this adapter is its clean format boundary and the way it exercises generic projections and cursor lifetimes.
6
+
7
+ ## The public parser and state
8
+
9
+ ```ts
10
+ import {
11
+ jsonParser,
12
+ createJsonRecordState
13
+ } from "@cobol-ts/cursor-loader-json";
14
+
15
+ // The JsonParser type is:
16
+ // Parser<JsonValue, undefined, undefined, JsonRecordState, readonly string[]>
17
+ ```
18
+
19
+ There is no parser configuration, bootstrap header or prepared source detail. `JsonRecordState` is **no longer `undefined`**: it is a `ProjectionRecordState`, created as `{projectionViews: new Map()}` once for each pooled physical-record holder. Register it wherever the JSONL parser is installed:
20
+
21
+ ```ts
22
+ json: {
23
+ parser: jsonParser,
24
+ parserConfig: () => undefined,
25
+ createRecordState: createJsonRecordState
26
+ }
27
+ ```
28
+
29
+ That factory is necessary for first-class root projected arrays. The physical-record implementation itself is unchanged.
30
+
31
+ ## How JSONL records are read
32
+
33
+ ```text
34
+ File → pooled Uint8Array buffers → physical record cursor
35
+ → jsonParser.parse()
36
+ ├─ scan until LF (or final EOF)
37
+ ├─ readMore() across buffers
38
+ ├─ establish endByte
39
+ ├─ UTF-8 decode current record
40
+ └─ JSON.parse()
41
+ → parsed JsonValue
42
+ → bound application projection
43
+ ```
44
+
45
+ The JSONL parser recognises its **own** record boundaries; there is no generic line parser. It scans from `startByte` to LF (`0x0A`) using `lastValidByte` as the limit of real input, calling `readMore()` as necessary. LF remains in the completed physical range; CRLF works because the CR is JSON whitespace. A last line may end directly at EOF. Records may begin partway through a pooled buffer or span several buffers.
46
+
47
+ On JSON syntax failure, parsing returns recoverable row `Errors` including source, record number and JSON text; `cursor-file` applies the configured row-error policy. A successful record is any JSON value (`object`, `array`, scalar or `null`).
48
+
49
+ ## First-class projected arrays and nested objects
50
+
51
+ ```ts
52
+ import type {ProjectedArray} from "@cobol-ts/arrays";
53
+ import {defineProjection} from "@cobol-ts/cursor-projection";
54
+
55
+ interface Item { sku(): string; quantity(): number }
56
+ interface Order { orderId(): number; readonly items: ProjectedArray<Item> }
57
+ interface Address { town(): string }
58
+ interface Customer {
59
+ name(): string;
60
+ readonly address: Address;
61
+ readonly orders: ProjectedArray<Order>;
62
+ }
63
+
64
+ const definition = defineProjection<Customer>({
65
+ name: {type: "string"},
66
+ address: {type: "object", fields: {
67
+ town: {type: "string"}
68
+ }},
69
+ orders: {type: "array", element: {
70
+ orderId: {type: "integer"},
71
+ items: {type: "array", element: {
72
+ sku: {type: "string"},
73
+ quantity: {type: "integer"}
74
+ }}
75
+ }}
76
+ });
77
+
78
+ // While a customer record lease is live:
79
+ customer.orders.length;
80
+ customer.orders[0].items[1].sku();
81
+ customer.orders.map(order => order.orderId());
82
+ customer.address.town();
83
+ ```
84
+
85
+ Members default to paths named after their properties; explicit `path: ["externalName"]` may select a different key. Nested member paths are **relative to the currently selected JSON object**. The factory compiles every path once and creates nested projected child objects only as indexes are actually read.
86
+
87
+ For `array()`, JSONL returns a `ProjectedArraySource<JsonValue>` whose `length()` and `element(i)` delegate to the actual parsed JSON array. The generic `@cobol-ts/arrays` view exposes readonly `length`, bracket indexing and native-style array operations. Standard operations such as `map()` materialise their returned result, not a second backing array of JSON record data.
88
+
89
+ ## Record-scoped reuse: length is not capacity
90
+
91
+ Root array runtimes are retained under `physicalRecord.recordState.projectionViews` and keyed by the compiled member object; each nested projected object retains its own child arrays. An array keeps **active logical length** from the *current* JSON value separately from reusable projection slots allocated during earlier accesses.
92
+
93
+ For records with 2, then 5, then 1 orders:
94
+
95
+ ```text
96
+ Record A: logical length = 2; retained element projections up to accessed indexes
97
+ Record B: logical length = 5; slots may grow as new indexes are read
98
+ Record C: logical length = 1; previously allocated higher slots remain but are hidden
99
+ ```
100
+
101
+ For a nested array, **each selected order has its own item array** with its own active count and retained slots. Two concurrently open `FileCursorResult` leases keep independent projected views. Closing a lease clears their current JSON bindings, but reusable child projection objects remain in pooled record state. Do not use child objects after close; they may later be rebound to another row.
102
+
103
+ The array cache has debugging site labels such as `root.orders`. Labels help inspection, but identity of the compiled member—not its string name—is the cache key.
104
+
105
+ ## Scalar projections (including legacy indexed scalar access)
106
+
107
+ Scalar accessors remain supported:
108
+
109
+ ```ts
110
+ interface CustomerScalar {
111
+ name(): string;
112
+ phone(index: number): string;
113
+ secondPhone(): string;
114
+ }
115
+ const scalar = defineProjection<CustomerScalar>({
116
+ name: {type: "string", path: ["name"]},
117
+ phone: {type: "string", path: ["phones"]},
118
+ secondPhone: {type: "string", path: ["phones", 1]}
119
+ });
120
+ ```
121
+
122
+ String-only JSON paths are retained unchanged when compiled; literal numeric path components are converted to strings at setup. With the older indexed-scalar style, JSONL discovers arrays from the actual parsed record and consumes numeric accessor arguments as it encounters arrays without an explicit selector. It supports nested and mixed literal/runtime selection. This is **a scalar convenience**, distinct from `readonly phones: ProjectedArray<Phone>`.
123
+
124
+ `integer` accepts a JavaScript integer, `float` a number, `character` a one-character string and `string` any string; coercion from strings to numbers is not performed. Missing properties, wrong shapes, missing/extra indexes and bad types cause lazy projection errors with physical provenance.
125
+
126
+ ## Error semantics and limits
127
+
128
+ - **Physical JSON syntax error:** returned as recoverable parser `Errors`; the file cursor's row policy applies.
129
+ - **Projection shape or type mismatch:** thrown when the member is accessed and enriched with the live record's source and context.
130
+ - **No JSON schema:** static compilation does not prove a property is always an object/array or that element shapes agree across rows. Validation happens when selected.
131
+ - **No lazy JSON parsing:** the complete JSON text becomes a JavaScript value graph once per record, unlike byte-backed CSV/COBOL.
132
+
133
+ ## Tests
134
+
135
+ - `src/cursor.loader.json.parser.spec.ts`: physical JSONL framing, multi-buffer data and parse errors.
136
+ - `src/json.projection.support.spec.ts`: scalar types, path traversal, literal and runtime indexes, errors.
137
+ - `src/json.projected.array.spec.ts`: existing indexed scalar accessors and cursor integration.
138
+ - `src/json.projected.array.spec.ts`: first-class arrays, nested objects, iteration, high-water reuse and simultaneous live rows through a real file cursor.
139
+
140
+ The cross-package `@cobol-ts/cursor-loader-test` fixture registers `createJsonRecordState`. Run Jest and a TypeScript build as separate verification steps where the monorepo scripts require it.
141
+
142
+ ## Rebinding and indexed diagnostics
143
+
144
+ Generic projected object and array views are retained and rebound rather than rematerialised as domain objects. A projected array uses sparse retained element slots plus an `ArrayWithSize` prefix of the child references bound during the current lease, so `clear()` does not scan its historical highest array index. Parser-native JSON values **are still newly allocated** by `JSON.parse()` for each record. Deep lazy failures can report a fully indexed application path assembled only on the error path; never retain a projected child beyond `FileCursorResult.close()`.
@@ -0,0 +1 @@
1
+ export * from './src/cursor.loader.json.parser';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./src/cursor.loader.json.parser"), exports);
18
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
1
  {
2
2
  "name": "@cobol-ts/cursor-loader-json",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "description": "parser for json to support the cursor loader package",
4
+ "version": "0.0.2",
5
+ "main": "dist/index",
6
+ "types": "dist/index",
7
+ "browserslist": {
8
+ "development": [
9
+ "last 1 chrome version",
10
+ "last 1 firefox version",
11
+ "last 1 safari version"
12
+ ],
13
+ "production": [
14
+ ">0.2%",
15
+ "not dead",
16
+ "not op_mini all"
17
+ ]
18
+ },
19
+ "dependencies": {
20
+ "@cobol-ts/cursor-loader-types": "0.0.2",
21
+ "@cobol-ts/errors": "0.0.2",
22
+ "@cobol-ts/cursor-record": "0.0.2",
23
+ "@cobol-ts/cursor-file": "0.0.2",
24
+ "@cobol-ts/arrays": "0.0.2"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "^5.2.2",
28
+ "@testing-library/jest-dom": "^6.1.4",
29
+ "@types/jest": "^29.0.0",
30
+ "@types/node": "^14.0.0",
31
+ "jest": "^29.0.0",
32
+ "ts-jest": "^29.0.0"
33
+ },
34
+ "eslintConfig": {
35
+ "extends": [
36
+ "react-app",
37
+ "react-app/jest"
38
+ ],
39
+ "overrides": [
40
+ {
41
+ "files": [
42
+ "**/*.stories.*"
43
+ ],
44
+ "rules": {
45
+ "import/no-anonymous-default-export": "off"
46
+ }
47
+ }
48
+ ]
49
+ },
50
+ "files": [
51
+ "dist/index.js",
52
+ "dist/index.d.ts",
53
+ "dist/src/",
54
+ "build/"
55
+ ],
56
+ "keywords": [
57
+ "indexing"
58
+ ],
59
+ "license": "MIT",
60
+ "repository": "git@github.com:intellimaintain/intellimaintain.git",
61
+ "scripts": {
62
+ "test": "jest --config jest.config.json --env node",
63
+ "test:watch": "npm run test -- --watch",
64
+ "start": "react-scripts start"
65
+ }
6
66
  }