dirac-json 0.1.0

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-OLD.md ADDED
@@ -0,0 +1,109 @@
1
+ # dirac-json
2
+
3
+ JSON utility library for DIRAC - provides declarative JSON parsing and access through subroutines.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install dirac-json
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```xml
14
+ <dirac>
15
+ <!-- Import the library -->
16
+ <import path="dirac-json/lib/index.di"/>
17
+
18
+ <!-- Parse JSON string -->
19
+ <defvar name="json_text">{"name":"Alice","age":30,"address":{"city":"NYC"}}</defvar>
20
+ <call name="parse" output="person">
21
+ <param><variable name="json_text"/></param>
22
+ </call>
23
+
24
+ <!-- Extract nested values -->
25
+ <call name="get" output="name">
26
+ <param><variable name="person"/></param>
27
+ <param>name</param>
28
+ </call>
29
+
30
+ <call name="get" output="city">
31
+ <param><variable name="person"/></param>
32
+ <param>address.city</param>
33
+ </call>
34
+
35
+ <output>Name: <variable name="name"/></output>
36
+ <output>City: <variable name="city"/></output>
37
+
38
+ <!-- Convert back to JSON -->
39
+ <call name="stringify" output="json_out">
40
+ <param><variable name="person"/></param>
41
+ <param>true</param><!-- pretty print -->
42
+ </call>
43
+
44
+ <output><variable name="json_out"/></output>
45
+ </dirac>
46
+ ```
47
+
48
+ ## API
49
+
50
+ ### parse(json_string)
51
+ Parses a JSON string into an object.
52
+
53
+ **Parameters:**
54
+ - `json_string`: JSON string to parse
55
+
56
+ **Returns:** Parsed JavaScript object
57
+
58
+ ### get(object, path)
59
+ Extracts a value from an object using dot notation path.
60
+
61
+ **Parameters:**
62
+ - `object`: JavaScript object
63
+ - `path`: Dot-separated path (e.g., "address.city")
64
+
65
+ **Returns:** Value at the specified path, or `undefined` if not found
66
+
67
+ ### stringify(object, pretty)
68
+ Converts an object to JSON string.
69
+
70
+ **Parameters:**
71
+ - `object`: JavaScript object to stringify
72
+ - `pretty`: "true" or true for pretty-printed output (optional)
73
+
74
+ **Returns:** JSON string
75
+
76
+ ## Example: Processing JSON from stdin
77
+
78
+ ```xml
79
+ <dirac>
80
+ <import path="dirac-json/lib/index.di"/>
81
+
82
+ <!-- Read JSON from stdin -->
83
+ <input name="message" mode="all"/>
84
+
85
+ <!-- Parse it -->
86
+ <call name="parse" output="msg">
87
+ <param><variable name="message"/></param>
88
+ </call>
89
+
90
+ <!-- Extract fields -->
91
+ <call name="get" output="image">
92
+ <param><variable name="msg"/></param>
93
+ <param>image</param>
94
+ </call>
95
+
96
+ <call name="get" output="timestamp">
97
+ <param><variable name="msg"/></param>
98
+ <param>timestamp</param>
99
+ </call>
100
+
101
+ <!-- Use the values -->
102
+ <output>Image: <variable name="image"/></output>
103
+ <output>Time: <variable name="timestamp"/></output>
104
+ </dirac>
105
+ ```
106
+
107
+ ## License
108
+
109
+ MIT
package/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # dirac-json
2
+
3
+ JSON and array operations for Dirac language.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install dirac-json
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```xml
14
+ <import src="dirac-json" />
15
+ ```
16
+
17
+ ## JSON Operations
18
+
19
+ ### Parse JSON String
20
+
21
+ ```xml
22
+ <defvar name="obj">
23
+ <parse json_string='{"name":"John","age":30}' />
24
+ </defvar>
25
+ ```
26
+
27
+ ### Get Nested Value
28
+
29
+ ```xml
30
+ <defvar name="userName">
31
+ <get object="$obj" path="name" />
32
+ </defvar>
33
+ ```
34
+
35
+ ### Stringify Object
36
+
37
+ ```xml
38
+ <defvar name="jsonStr">
39
+ <stringify object="$obj" pretty="true" />
40
+ </defvar>
41
+ ```
42
+
43
+ ## Array Operations
44
+
45
+ The `<array>` tag provides array manipulation with nested operation tags.
46
+
47
+ ### Create an Array
48
+
49
+ ```xml
50
+ <defvar name="myArray">["hello", "world"]</defvar>
51
+ ```
52
+
53
+ ### Get Array Length
54
+
55
+ ```xml
56
+ <array name="myArray"><length /></array>
57
+ <!-- Output: 2 -->
58
+ ```
59
+
60
+ ### Access by Index
61
+
62
+ ```xml
63
+ <array name="myArray"><get index="0" /></array>
64
+ <!-- Output: hello -->
65
+
66
+ <array name="myArray"><get index="1" /></array>
67
+ <!-- Output: world -->
68
+ ```
69
+
70
+ ### Push (Add to End)
71
+
72
+ ```xml
73
+ <array name="myArray"><push>test</push></array>
74
+ <!-- myArray is now ["hello", "world", "test"] -->
75
+ ```
76
+
77
+ ### Pop (Remove from End)
78
+
79
+ ```xml
80
+ <array name="myArray"><pop /></array>
81
+ <!-- Returns: test -->
82
+ <!-- myArray is now ["hello", "world"] -->
83
+ ```
84
+
85
+ ### Shift (Remove from Beginning)
86
+
87
+ ```xml
88
+ <array name="myArray"><shift /></array>
89
+ <!-- Returns: hello -->
90
+ <!-- myArray is now ["world"] -->
91
+ ```
92
+
93
+ ### Unshift (Add to Beginning)
94
+
95
+ ```xml
96
+ <array name="myArray"><unshift>first</unshift></array>
97
+ <!-- myArray is now ["first", "world"] -->
98
+ ```
99
+
100
+ ## Complete Example
101
+
102
+ ```xml
103
+ <dirac>
104
+ <import src="dirac-json" />
105
+
106
+ <!-- Create and manipulate an array -->
107
+ <defvar name="items">["apple", "banana"]</defvar>
108
+
109
+ <output>Initial: </output>
110
+ <output><variable name="items" /></output>
111
+
112
+ <!-- Add items -->
113
+ <array name="items"><push>cherry</push></array>
114
+ <array name="items"><unshift>avocado</unshift></array>
115
+
116
+ <output>After adding: </output>
117
+ <output><variable name="items" /></output>
118
+ <!-- Output: ["avocado","apple","banana","cherry"] -->
119
+
120
+ <!-- Get length -->
121
+ <output>Length: </output>
122
+ <array name="items"><length /></array>
123
+ <!-- Output: 4 -->
124
+
125
+ <!-- Remove items -->
126
+ <output>Popped: </output>
127
+ <array name="items"><pop /></array>
128
+ <!-- Output: cherry -->
129
+
130
+ <output>Shifted: </output>
131
+ <array name="items"><shift /></array>
132
+ <!-- Output: avocado -->
133
+
134
+ <output>Final: </output>
135
+ <output><variable name="items" /></output>
136
+ <!-- Output: ["apple","banana"] -->
137
+ </dirac>
138
+ ```
139
+
140
+ ## API Reference
141
+
142
+ ### JSON Operations
143
+
144
+ - **`<parse json_string="..." />`** - Parse JSON string into object
145
+ - **`<get object="..." path="..." />`** - Extract value using dot notation (e.g., "user.address.city")
146
+ - **`<stringify object="..." pretty="true|false" />`** - Convert object to JSON string
147
+
148
+ ### Array Operations
149
+
150
+ All array operations use the pattern: `<array name="variableName"><operation /></array>`
151
+
152
+ - **`<length />`** - Get array length (returns number)
153
+ - **`<get index="n" />`** - Get item at index n (returns item)
154
+ - **`<push>value</push>`** - Add item to end (mutates array)
155
+ - **`<pop />`** - Remove and return last item (mutates array)
156
+ - **`<shift />`** - Remove and return first item (mutates array)
157
+ - **`<unshift>value</unshift>`** - Add item to beginning (mutates array)
158
+
159
+ **Important Notes:**
160
+ - Arrays are stored as JSON strings in DIRAC variables
161
+ - Array operations automatically parse/serialize during execution
162
+ - Mutating operations (push, pop, shift, unshift) modify the variable in place
163
+ - Non-mutating operations (length, get) only read the array
164
+
165
+ ## License
166
+
167
+ MIT
package/TODO.md ADDED
@@ -0,0 +1,59 @@
1
+ # DIRAC JSON - TODO
2
+
3
+ **Project**: JSON utility library for DIRAC
4
+ **Location**: `/Users/zhiwang/diraclang/dirac-json/`
5
+ **Parent TODO**: See `/Users/zhiwang/diraclang/dirac/TODO.md`
6
+
7
+ ## 🔴 High Priority
8
+
9
+ ### Pending
10
+ - [ ] **Path query syntax**: Implement dot notation access
11
+ - Target syntax: `<json path="a.b.c">{"a":{"b":{"c":"value"}}}</json>` → "value"
12
+ - Should work with nested objects and arrays
13
+ - Handle edge cases: missing keys, null values, array indices
14
+
15
+ ## 🟡 Medium Priority
16
+
17
+ ### Pending
18
+ - [ ] **Array operations**: Support array indexing and iteration
19
+ - Syntax: `path="items[0].name"` for array access
20
+ - Consider: array length, map, filter operations
21
+
22
+ - [ ] **JSON validation**: Check if string is valid JSON before parsing
23
+ - Return error message on invalid JSON
24
+ - Optionally validate against schema
25
+
26
+ - [ ] **Testing**: Create test suite for all operations
27
+ - Test parse, stringify, path queries
28
+ - Test edge cases and error handling
29
+
30
+ ## 🟢 Low Priority / Future
31
+
32
+ ### Pending
33
+ - [ ] **JSONPath support**: Full JSONPath query language
34
+ - Beyond simple dot notation
35
+ - Support wildcards, filters, recursive descent
36
+
37
+ - [ ] **Transform operations**: Merge, filter, map
38
+ - Combine multiple JSON objects
39
+ - Filter by conditions
40
+ - Transform structure
41
+
42
+ - [ ] **npm publishing**: Publish as standalone package
43
+ - Currently local project
44
+ - Version 0.1.0 target
45
+
46
+ ## ✅ Completed
47
+
48
+ - [x] **Basic library structure**
49
+ - Created lib/index.di
50
+ - Basic README with examples
51
+ - package.json setup
52
+
53
+ ---
54
+
55
+ ## Notes
56
+ - **Last updated**: 2026-02-14
57
+ - **Current status**: Initial implementation
58
+ - **Priority**: Low overall, Mid desirability
59
+ - **Related**: Will integrate with dirac-lang stdlib system
@@ -0,0 +1,77 @@
1
+ <dirac>
2
+ <import src="../lib/index.di" />
3
+
4
+ <!-- Create an array -->
5
+ <defvar name="myArray">["hello", "world"]</defvar>
6
+
7
+ <output>Initial array: </output>
8
+ <output><variable name="myArray" /></output>
9
+ <output>
10
+
11
+ </output>
12
+
13
+ <!-- Test length -->
14
+ <output>Length: </output>
15
+ <array name="myArray"><length /></array>
16
+ <output>
17
+
18
+ </output>
19
+
20
+ <!-- Test get -->
21
+ <output>Item at index 0: </output>
22
+ <array name="myArray"><get index="0" /></array>
23
+ <output>
24
+
25
+ </output>
26
+
27
+ <output>Item at index 1: </output>
28
+ <array name="myArray"><get index="1" /></array>
29
+ <output>
30
+
31
+ </output>
32
+
33
+ <!-- Test push -->
34
+ <output>Pushing 'test'...</output>
35
+ <output>
36
+
37
+ </output>
38
+ <array name="myArray"><push>test</push></array>
39
+ <output>Array after push: </output>
40
+ <output><variable name="myArray" /></output>
41
+ <output>
42
+
43
+ </output>
44
+
45
+ <!-- Test pop -->
46
+ <output>Popped: </output>
47
+ <array name="myArray"><pop /></array>
48
+ <output>
49
+
50
+ </output>
51
+ <output>Array after pop: </output>
52
+ <output><variable name="myArray" /></output>
53
+ <output>
54
+
55
+ </output>
56
+
57
+ <!-- Test shift -->
58
+ <output>Shifted: </output>
59
+ <array name="myArray"><shift /></array>
60
+ <output>
61
+
62
+ </output>
63
+ <output>Array after shift: </output>
64
+ <output><variable name="myArray" /></output>
65
+ <output>
66
+
67
+ </output>
68
+
69
+ <!-- Test unshift -->
70
+ <output>Unshifting 'first'...</output>
71
+ <output>
72
+
73
+ </output>
74
+ <array name="myArray"><unshift>first</unshift></array>
75
+ <output>Array after unshift: </output>
76
+ <output><variable name="myArray" /></output>
77
+ </dirac>
@@ -0,0 +1,65 @@
1
+ <dirac>
2
+ <import src="../lib/index.di" />
3
+
4
+ <!-- Create an array -->
5
+ <defvar name="myArray">[]</defvar>
6
+ <echo message="Initial array: " />
7
+ <output><variable name="myArray" /></output>
8
+
9
+ <!-- Push some items -->
10
+ <echo message="Pushing 'hello'..." />
11
+ <array name="myArray"><push>hello</push></array>
12
+ <output><variable name="myArray" /></output>
13
+
14
+ <echo message="Pushing 'world'..." />
15
+ <array name="myArray"><push>world</push></array>
16
+ <output><variable name="myArray" /></output>
17
+
18
+ <echo message="Pushing 42..." />
19
+ <array name="myArray"><push>42</push></array>
20
+ <output><variable name="myArray" /></output>
21
+
22
+ <!-- Get length -->
23
+ <defvar name="len">
24
+ <array name="myArray"><length /></array>
25
+ </defvar>
26
+ <echo message="Array length: " />
27
+ <output><variable name="len" /></output>
28
+
29
+ <!-- Access by index -->
30
+ <defvar name="item0">
31
+ <array name="myArray"><get index="0" /></array>
32
+ </defvar>
33
+ <echo message="Item at index 0: " />
34
+ <output><variable name="item0" /></output>
35
+
36
+ <defvar name="item1">
37
+ <array name="myArray"><get index="1" /></array>
38
+ </defvar>
39
+ <echo message="Item at index 1: " />
40
+ <output><variable name="item1" /></output>
41
+
42
+ <!-- Pop an item -->
43
+ <defvar name="popped">
44
+ <array name="myArray"><pop /></array>
45
+ </defvar>
46
+ <echo message="Popped item: " />
47
+ <output><variable name="popped" /></output>
48
+ <echo message="Array after pop: " />
49
+ <output><variable name="myArray" /></output>
50
+
51
+ <!-- Shift an item -->
52
+ <defvar name="shifted">
53
+ <array name="myArray"><shift /></array>
54
+ </defvar>
55
+ <echo message="Shifted item: " />
56
+ <output><variable name="shifted" /></output>
57
+ <echo message="Array after shift: " />
58
+ <output><variable name="myArray" /></output>
59
+
60
+ <!-- Unshift an item -->
61
+ <echo message="Unshifting 'first'..." />
62
+ <array name="myArray"><unshift>first</unshift></array>
63
+ <echo message="Array after unshift: " />
64
+ <output><variable name="myArray" /></output>
65
+ </dirac>
@@ -0,0 +1,9 @@
1
+ <dirac>
2
+ <import src="../lib/index.di" />
3
+
4
+ <defvar name="myArray">["a", "b", "c"]</defvar>
5
+
6
+ <array name="myArray">
7
+ <length />
8
+ </array>
9
+ </dirac>
@@ -0,0 +1,13 @@
1
+ <dirac>
2
+ <import src="../lib/index.di" />
3
+
4
+ <defvar name="myArray">["a", "b", "c"]</defvar>
5
+
6
+ <output>Before array call</output>
7
+
8
+ <array name="myArray">
9
+ <output>Inside array - before length call</output>
10
+ </array>
11
+
12
+ <output>After array call</output>
13
+ </dirac>
package/lib/index.di ADDED
@@ -0,0 +1,196 @@
1
+ <!--
2
+ dirac-json library
3
+ Provides declarative JSON parsing and access
4
+ -->
5
+ <dirac>
6
+ <!-- parse: Parse JSON string into object -->
7
+ <subroutine name="parse">
8
+ <parameters>
9
+ <parameter name="json_string"/>
10
+ </parameters>
11
+ <eval>JSON.parse(json_string)</eval>
12
+ </subroutine>
13
+
14
+ <!-- get: Extract value from JSON object by path (dot notation) -->
15
+ <subroutine name="get">
16
+ <parameters>
17
+ <parameter name="object"/>
18
+ <parameter name="path"/>
19
+ </parameters>
20
+ <eval>
21
+ const parts = path.split('.');
22
+ let result = object;
23
+ for (const part of parts) {
24
+ if (result === null || result === undefined) {
25
+ return undefined;
26
+ }
27
+ result = result[part];
28
+ }
29
+ return result;
30
+ </eval>
31
+ </subroutine>
32
+
33
+ <!-- stringify: Convert object to JSON string -->
34
+ <subroutine name="stringify">
35
+ <parameters>
36
+ <parameter name="object"/>
37
+ <parameter name="pretty"/>
38
+ </parameters>
39
+ <eval>
40
+ if (pretty === 'true' || pretty === true) {
41
+ return JSON.stringify(object, null, 2);
42
+ }
43
+ return JSON.stringify(object);
44
+ </eval>
45
+ </subroutine>
46
+
47
+ <!-- array: Array operations with nested operation tags -->
48
+ <subroutine name="array" param-name="string:required:array variable name">
49
+
50
+ <!-- get: Get item at index -->
51
+ <subroutine name="get" param-index="number:required:array index">
52
+ <eval name="result">
53
+ let arrValue;
54
+ for (let i = session.variables.length - 1; i >= 0; i--) {
55
+ if (session.variables[i].name === name) {
56
+ arrValue = session.variables[i].value;
57
+ break;
58
+ }
59
+ }
60
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
61
+ if (!Array.isArray(arr)) {
62
+ throw new Error(`Variable ${name} is not an array`);
63
+ }
64
+ const idx = parseInt(index);
65
+ return arr[idx];
66
+ </eval>
67
+ <output><variable name="result" /></output>
68
+ </subroutine>
69
+
70
+ <!-- push: Add item to end of array -->
71
+ <subroutine name="push">
72
+ <defvar name="value"><parameters select="*" /></defvar>
73
+ <eval>
74
+ let arrValue;
75
+ let varIndex = -1;
76
+ for (let i = session.variables.length - 1; i >= 0; i--) {
77
+ if (session.variables[i].name === name) {
78
+ arrValue = session.variables[i].value;
79
+ varIndex = i;
80
+ break;
81
+ }
82
+ }
83
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
84
+ if (!Array.isArray(arr)) {
85
+ throw new Error(`Variable ${name} is not an array`);
86
+ }
87
+ arr.push(value);
88
+ // Update the variable in place
89
+ if (varIndex >= 0) {
90
+ session.variables[varIndex].value = JSON.stringify(arr);
91
+ }
92
+ return arr.length;
93
+ </eval>
94
+ </subroutine>
95
+
96
+ <!-- pop: Remove and return last item from array -->
97
+ <subroutine name="pop">
98
+ <eval name="result">
99
+ let arrValue;
100
+ let varIndex = -1;
101
+ for (let i = session.variables.length - 1; i >= 0; i--) {
102
+ if (session.variables[i].name === name) {
103
+ arrValue = session.variables[i].value;
104
+ varIndex = i;
105
+ break;
106
+ }
107
+ }
108
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
109
+ if (!Array.isArray(arr)) {
110
+ throw new Error(`Variable ${name} is not an array`);
111
+ }
112
+ const result = arr.pop();
113
+ // Update the variable in place
114
+ if (varIndex >= 0) {
115
+ session.variables[varIndex].value = JSON.stringify(arr);
116
+ }
117
+ return result;
118
+ </eval>
119
+ <output><variable name="result" /></output>
120
+ </subroutine>
121
+
122
+ <!-- shift: Remove and return first item from array -->
123
+ <subroutine name="shift">
124
+ <eval name="result">
125
+ let arrValue;
126
+ let varIndex = -1;
127
+ for (let i = session.variables.length - 1; i >= 0; i--) {
128
+ if (session.variables[i].name === name) {
129
+ arrValue = session.variables[i].value;
130
+ varIndex = i;
131
+ break;
132
+ }
133
+ }
134
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
135
+ if (!Array.isArray(arr)) {
136
+ throw new Error(`Variable ${name} is not an array`);
137
+ }
138
+ const result = arr.shift();
139
+ // Update the variable in place
140
+ if (varIndex >= 0) {
141
+ session.variables[varIndex].value = JSON.stringify(arr);
142
+ }
143
+ return result;
144
+ </eval>
145
+ <output><variable name="result" /></output>
146
+ </subroutine>
147
+
148
+ <!-- unshift: Add item to beginning of array -->
149
+ <subroutine name="unshift">
150
+ <defvar name="value"><parameters select="*" /></defvar>
151
+ <eval>
152
+ let arrValue;
153
+ let varIndex = -1;
154
+ for (let i = session.variables.length - 1; i >= 0; i--) {
155
+ if (session.variables[i].name === name) {
156
+ arrValue = session.variables[i].value;
157
+ varIndex = i;
158
+ break;
159
+ }
160
+ }
161
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
162
+ if (!Array.isArray(arr)) {
163
+ throw new Error(`Variable ${name} is not an array`);
164
+ }
165
+ arr.unshift(value);
166
+ // Update the variable in place
167
+ if (varIndex >= 0) {
168
+ session.variables[varIndex].value = JSON.stringify(arr);
169
+ }
170
+ return arr.length;
171
+ </eval>
172
+ </subroutine>
173
+
174
+ <!-- length: Get array length -->
175
+ <subroutine name="length">
176
+ <eval name="result">
177
+ let arrValue;
178
+ for (let i = session.variables.length - 1; i >= 0; i--) {
179
+ if (session.variables[i].name === name) {
180
+ arrValue = session.variables[i].value;
181
+ break;
182
+ }
183
+ }
184
+ let arr = typeof arrValue === 'string' ? JSON.parse(arrValue) : arrValue;
185
+ if (!Array.isArray(arr)) {
186
+ throw new Error(`Variable ${name} is not an array`);
187
+ }
188
+ return arr.length;
189
+ </eval>
190
+ <output><variable name="result" /></output>
191
+ </subroutine>
192
+
193
+ <!-- Execute nested operations -->
194
+ <parameters select="*" />
195
+ </subroutine>
196
+ </dirac>
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "dirac-json",
3
+ "version": "0.1.0",
4
+ "description": "JSON utility library for DIRAC - declarative JSON parsing and access",
5
+ "type": "module",
6
+ "main": "lib/index.di",
7
+ "keywords": [
8
+ "dirac",
9
+ "json",
10
+ "utility"
11
+ ],
12
+ "author": "",
13
+ "license": "MIT"
14
+ }