flex-json 0.0.5 → 0.0.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.
package/README.md CHANGED
@@ -1,226 +1,369 @@
1
- # flex-json
2
-
3
- ## Flexible JSON manipulation library for JavaScript
4
-
5
- ## Maintainers
6
-
7
- | Maintainer | GitHub | LinkedIn |
8
- | --------------- | ------------------------------------------- | ------------------------------------------------------ |
9
- | Ted Tyree | [GitHub](https://github.com/tedtyree) |[LinkedIn](https://www.linkedin.com/in/tedtyree/) |
10
- | Michael Njuguna | [GitHub](https://github.com/michaelnjuguna) |[LinkedIn](https://www.linkedin.com/in/michael-njuguna/)|
11
-
12
- ## Table of Contents
13
-
14
- - [Why flex-json](#why-flex-json)
15
- - [How the library works](#how-the-library-works)
16
- - [Install](#install)
17
- - [Usage](#usage)
18
-
19
- ## Why flex-json
20
-
21
- It is simply Json with comments! FlexJson was written to make JSON config files easy to manage and allow for comments. The library also makes it super easy to read a json file (often a config file), modify a single value, and save the file back to the file system without messing up the comments.
22
-
23
- - Easy config file formatting
24
- - Includes comments in both /* */ and // notation
25
- - Simple to edit Json files
26
- - Allows for other JavaScript like features such as using either single quotes or double quotes.
27
- - Can also be used within Node.js apps for other uses such as reading/writing JSON to/from database records and parsing loosely formatted Json in web page content.
28
-
29
- ## How the library works
30
-
31
- ### Flex-json syntax
32
-
33
- BTW flex-json as a standard of syntax is not really all that new - it is very much in existence within JavaScript and other syntax standards. Here we just make it available in a library and to facilitate config file parsing and editing.
34
-
35
- ### Strict Mode
36
-
37
- When in strict mode, the flex-json library reads JSON files in standard JSON format. Comments are not valid and double quotes are required around strings.
38
-
39
- Note: If the library is flagged to preserve spacing, Json that has been read in from a file will be written with the same formatting. In other words, the carriage returns and white space are captured during the parsing process and used to re-format the output during the write process.
40
-
41
- ### Flex Mode
42
-
43
- When in flex mode, the flex-json library has the following features:
44
-
45
- - Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)
46
-
47
- - Like JavaScript, when a "//" is encountered, the remainder of the line is considered to be a comment
48
-
49
- - Strings do not require quotes unless they contain special characters
50
-
51
- - Strings can be quoted using double quotes or single quotes
52
-
53
- When in flex mode, all of the following examples of Json are valid:
54
-
55
- __example 1:__
56
-
57
- ```javascript
58
- {apple: red, banana: yellow, 'sky': 'blue'}
59
- ```
60
-
61
- __example 2:__
62
-
63
- ```javascript
64
- {"apple": "red"
65
- ,'banana': 'yellow'
66
- // ,'sky': 'blue' - this line is commented out
67
- }
68
- ```
69
-
70
- __example 3:__
71
-
72
- ```javascript
73
- [ "one, is first"
74
- ,'two, is next'
75
- /* comment out remainder of array
76
- ,"three, is third"
77
- ,'four', is last"
78
- */
79
- ]
80
- ```
81
-
82
- __Note__ that {number:"2"} is not the same as {number:2} because flex-json will see that the 2 without quotes is a valid number and load it as a numeric.
83
-
84
- ## Install
85
-
86
- ```bash
87
- # NPM
88
- npm install flex-json
89
-
90
- #PNPM
91
- pnpm install flex-json
92
-
93
- # Yarn
94
- Yarn install flex-json
95
-
96
- ```
97
-
98
- ## Usage
99
-
100
- ```javascript
101
- const FlexJson = require('flex-json');
102
-
103
- // Create a FlexJson object
104
- const myJson = new FlexJson('{"key": "value"}', true);
105
-
106
- // Access properties
107
- console.log(myJson.jsonString); // Get JSON string representation
108
-
109
- // Modify properties
110
- myJson.keepSpacing = true; // Preserve spacing during deserialization
111
-
112
- // Check status and type
113
- console.log(myJson.Status); // Get status
114
- console.log(myJson.jsonType); // Get JSON type
115
-
116
- // Manipulate JSON object
117
- myJson.i('key').thisValue = 'new value'; // Set a new value for a key
118
-
119
- // Convert JSON object to array
120
- myJson.ConvertToArray();
121
-
122
- // Access array elements
123
- console.log(myJson.item(0).thisValue); // Access first element in the array
124
-
125
- // Use new methods
126
- myJson.forEach(item => {
127
- console.log(item.jsonString); // Iterate through each item and log JSON string
128
- });
129
-
130
- myJson.add('new item', 'newKey'); // Add a new item to the JSON object
131
-
132
- console.log(myJson.indexOfKey('newKey')); // Get the index of a key in the JSON object
133
-
134
- console.log(myJson.contains('key')); // Check if a key exists in the JSON object
135
-
136
- console.log(myJson.getStr('key', 'default')); // Get string value by key with a default value
137
-
138
- console.log(myJson.getNum('count', 0)); // Get numeric value by key with a default value
139
-
140
- console.log(myJson.getBool('flag', false)); // Get boolean value by key with a default value
141
-
142
- ```
143
-
144
- Serialization and Deserialization examples
145
-
146
- ```javascript
147
- const FlexJson = require("flex-json");
148
-
149
- // Create an instance of FlexJson
150
- const flexJson = new FlexJson();
151
-
152
- // Example: Deserialize JSON
153
- const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
154
- flexJson.Deserialize(jsonString);
155
-
156
- // Example: Serialize to JSON
157
- flexJson.SerializeMe();
158
-
159
- // Access serialized JSON string
160
- const serializedJson = flexJson.SerializeMe();
161
- console.log(serializedJson);
162
- ```
163
-
164
- Config file example (this is the best part!)
165
-
166
- First create a json config file for example a text file c:/temp/my-config.json containing the following text…
167
- ```javascript
168
- /* my-config
169
- ** this is an example of parsing
170
- ** and updating a json config file
171
- */
172
- {
173
- ParameterA: 'Apple',
174
- ParameterB:'Banana'
175
- }
176
- ```
177
-
178
- In your node.js app run these commands…
179
-
180
- ```javascript
181
- // setup parameters
182
- let defaultCounter = 0;
183
- let myConfigPath = "c:/temp/my-config.json";
184
-
185
- // read json config file
186
- let myConfig = new FlexJson();
187
- myConfig.DeserializeFlexFile(myConfigPath);
188
-
189
- // read CounterA and increment it by 1
190
- // use default value to create CounterA if it does not exist
191
- let counter = myConfig.getNum("CounterA",defaultCounter);
192
- counter = counter + 1;
193
- myConfig.add(counter,"CounterA");
194
-
195
- // write config file back to file system
196
- myConfig.WriteToFile(myConfigPath);
197
- ```
198
-
199
- The first time this is run the output my-config.json will have a new parameter…
200
- CounterA:1
201
-
202
- And each new time this is run the counter will increase
203
- CounterA:2
204
-
205
- ### Contributing
206
-
207
- 1. Fork this repository.
208
- 2. Create new branch with feature name.
209
- 3. Create your feature.
210
- 4. Commit and set commit message with feature name.
211
- 5. Push your code to your fork repository.
212
- 6. Create pull request.
213
-
214
- ### Support
215
-
216
- If you like this project, You can support us with starring ⭐ this repository or donate to [uO.heartofkenya.com](https://u0.heartofkenya.com/).
217
-
218
- ### Acknowledgements
219
-
220
- Special thanks to [u0.heartofKenya.com](https://u0.heartofkenya.com/) and [ebiashararahisi](https://ebiashararahisi.com/) for their work in Machakos, Kenya.
221
-
222
- ### License
223
-
224
- [MIT](LICENSE.txt)
225
-
226
- Made with 💙
1
+ # flex-json
2
+
3
+ ## Flexible JSON manipulation library for JavaScript
4
+
5
+ ## Maintainers
6
+
7
+ | Maintainer | GitHub | LinkedIn |
8
+ | --------------- | ------------------------------------------- | ------------------------------------------------------ |
9
+ | Ted Tyree | [GitHub](https://github.com/tedtyree) |[LinkedIn](https://www.linkedin.com/in/tedtyree/) |
10
+ | Michael Njuguna | [GitHub](https://github.com/michaelnjuguna) |[LinkedIn](https://www.linkedin.com/in/michael-njuguna/)|
11
+
12
+ ## Table of Contents
13
+
14
+ - [Why flex-json](#why-flex-json)
15
+ - [How the library works](#how-the-library-works)
16
+ - [Install](#install)
17
+ - [Usage](#usage)
18
+ - [Error Handling](#error-handling)
19
+ - [JFX File Type](#jfx-file-type)
20
+ - [Editor Support](#editor-support)
21
+
22
+ ## Why flex-json
23
+
24
+ It is simply Json with comments! FlexJson was written to make JSON config files easy to manage and allow for comments. The library also makes it super easy to read a json file (often a config file), modify a single value, and save the file back to the file system without messing up the comments.
25
+
26
+ - Easy config file formatting
27
+ - Includes comments in both /* */ and // notation
28
+ - Simple to edit Json files
29
+ - Allows for other JavaScript like features such as using either single quotes or double quotes.
30
+ - Can also be used within Node.js apps for other uses such as reading/writing JSON to/from database records and parsing loosely formatted Json in web page content.
31
+
32
+ ## How the library works
33
+
34
+ ### Flex-json syntax
35
+
36
+ BTW flex-json as a standard of syntax is not really all that new - it is very much in existence within JavaScript and other syntax standards. Here we just make it available in a library and to facilitate config file parsing and editing.
37
+
38
+ ### Strict Mode
39
+
40
+ When in strict mode, the flex-json library reads JSON files in standard JSON format. Comments are not valid and double quotes are required around strings.
41
+
42
+ Note: If the library is flagged to preserve spacing, Json that has been read in from a file will be written with the same formatting. In other words, the carriage returns and white space are captured during the parsing process and used to re-format the output during the write process.
43
+
44
+ ### Flex Mode
45
+
46
+ When in flex mode, the flex-json library has the following features:
47
+
48
+ - Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)
49
+
50
+ - Like JavaScript, when a "//" is encountered, the remainder of the line is considered to be a comment
51
+
52
+ - Strings do not require quotes unless they contain special characters
53
+
54
+ - Strings can be quoted using double quotes or single quotes
55
+
56
+ When in flex mode, all of the following examples of Json are valid:
57
+
58
+ __example 1:__
59
+
60
+ ```javascript
61
+ {apple: red, banana: yellow, 'sky': 'blue'}
62
+ ```
63
+
64
+ __example 2:__
65
+
66
+ ```javascript
67
+ {"apple": "red"
68
+ ,'banana': 'yellow'
69
+ // ,'sky': 'blue' - this line is commented out
70
+ }
71
+ ```
72
+
73
+ __example 3:__
74
+
75
+ ```javascript
76
+ [ "one, is first"
77
+ ,'two, is next'
78
+ /* comment out remainder of array
79
+ ,"three, is third"
80
+ ,'four', is last"
81
+ */
82
+ ]
83
+ ```
84
+
85
+ __Note__ that {number:"2"} is not the same as {number:2} because flex-json will see that the 2 without quotes is a valid number and load it as a numeric.
86
+
87
+ ## Install
88
+
89
+ ```bash
90
+ # NPM
91
+ npm install flex-json
92
+
93
+ #PNPM
94
+ pnpm install flex-json
95
+
96
+ # Yarn
97
+ Yarn install flex-json
98
+
99
+ ```
100
+
101
+ ## Usage
102
+
103
+ ```javascript
104
+ const FlexJson = require('flex-json');
105
+
106
+ // Create a FlexJson object
107
+ const myJson = new FlexJson('{"key": "value"}', true);
108
+
109
+ // Access properties
110
+ console.log(myJson.jsonString); // Get JSON string representation
111
+
112
+ // Modify properties
113
+ myJson.keepSpacing = true; // Preserve spacing during deserialization
114
+
115
+ // Check status and type
116
+ console.log(myJson.Status); // Get status
117
+ console.log(myJson.jsonType); // Get JSON type
118
+
119
+ // Manipulate JSON object
120
+ myJson.i('key').thisValue = 'new value'; // Set a new value for a key
121
+
122
+ // Convert JSON object to array
123
+ myJson.ConvertToArray();
124
+
125
+ // Access array elements
126
+ console.log(myJson.item(0).thisValue); // Access first element in the array
127
+
128
+ // Use new methods
129
+ myJson.forEach(item => {
130
+ console.log(item.jsonString); // Iterate through each item and log JSON string
131
+ });
132
+
133
+ myJson.add('new item', 'newKey'); // Add a new item to the JSON object
134
+
135
+ console.log(myJson.indexOfKey('newKey')); // Get the index of a key in the JSON object
136
+
137
+ console.log(myJson.contains('key')); // Check if a key exists in the JSON object
138
+
139
+ console.log(myJson.getStr('key', 'default')); // Get string value by key with a default value
140
+
141
+ console.log(myJson.getNum('count', 0)); // Get numeric value by key with a default value
142
+
143
+ console.log(myJson.getBool('flag', false)); // Get boolean value by key with a default value
144
+
145
+ ```
146
+
147
+ Serialization and Deserialization examples
148
+
149
+ ```javascript
150
+ const FlexJson = require("flex-json");
151
+
152
+ // Create an instance of FlexJson
153
+ const flexJson = new FlexJson();
154
+
155
+ // Example: Deserialize JSON
156
+ const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
157
+ flexJson.Deserialize(jsonString);
158
+
159
+ // Example: Serialize to JSON
160
+ flexJson.SerializeMe();
161
+
162
+ // Access serialized JSON string
163
+ const serializedJson = flexJson.SerializeMe();
164
+ console.log(serializedJson);
165
+ ```
166
+
167
+ Config file example (this is the best part!)
168
+
169
+ First create a json config file for example a text file c:/temp/my-config.json containing the following text…
170
+ ```javascript
171
+ /* my-config
172
+ ** this is an example of parsing
173
+ ** and updating a json config file
174
+ */
175
+ {
176
+ ParameterA: 'Apple',
177
+ ParameterB:'Banana'
178
+ }
179
+ ```
180
+
181
+ In your node.js app run these commands…
182
+
183
+ ```javascript
184
+ // setup parameters
185
+ let defaultCounter = 0;
186
+ let myConfigPath = "c:/temp/my-config.json";
187
+
188
+ // read json config file
189
+ let myConfig = new FlexJson();
190
+ myConfig.DeserializeFlexFile(myConfigPath);
191
+
192
+ // read CounterA and increment it by 1
193
+ // use default value to create CounterA if it does not exist
194
+ let counter = myConfig.getNum("CounterA",defaultCounter);
195
+ counter = counter + 1;
196
+ myConfig.add(counter,"CounterA");
197
+
198
+ // write config file back to file system
199
+ myConfig.WriteToFile(myConfigPath);
200
+ ```
201
+
202
+ The first time this is run the output my-config.json will have a new parameter
203
+ CounterA:1
204
+
205
+ And each new time this is run the counter will increase…
206
+ CounterA:2
207
+
208
+ ## Error Handling
209
+
210
+ By default, FlexJson throws a `FlexJsonError` when parsing errors occur. This makes it easy to catch and handle errors using standard try/catch:
211
+
212
+ ```javascript
213
+ const FlexJson = require('flex-json');
214
+
215
+ try {
216
+ const fj = new FlexJson();
217
+ fj.DeserializeFlex('{invalid json!!!}');
218
+ } catch (err) {
219
+ console.log(err.name); // "FlexJsonError"
220
+ console.log(err.status); // -159 (error code)
221
+ console.log(err.message); // "Invalid character..."
222
+ }
223
+ ```
224
+
225
+ ### Silent Mode
226
+
227
+ If you prefer the legacy behavior where errors don't throw (useful for validation scenarios), set `throwOnError` to `false`:
228
+
229
+ ```javascript
230
+ const fj = new FlexJson();
231
+ fj.throwOnError = false; // Disable throwing
232
+
233
+ fj.DeserializeFlex('{invalid json}');
234
+
235
+ if (fj.Status !== 0) {
236
+ console.log('Parse failed:', fj.statusMsg);
237
+ console.log('Error code:', fj.Status);
238
+ }
239
+ ```
240
+
241
+ ### Error Properties
242
+
243
+ | Property | Description |
244
+ |----------|-------------|
245
+ | `Status` | Error code (0 = success, negative = error) |
246
+ | `statusMsg` | Human-readable error message |
247
+ | `throwOnError` | `true` (default) = throw errors, `false` = silent mode |
248
+
249
+ ## JFX File Type
250
+
251
+ FlexJson files can use the `.jfx` file extension to distinguish them from standard JSON files. This allows editors to provide proper syntax highlighting for FlexJson's extended features.
252
+
253
+ ### Example `.jfx` file
254
+
255
+ ```jfx
256
+ /* Application Configuration
257
+ ** Version 1.0
258
+ */
259
+ {
260
+ // Database settings
261
+ database: {
262
+ host: localhost,
263
+ port: 5432,
264
+ name: "my-app-db"
265
+ },
266
+
267
+ // Feature flags
268
+ features: {
269
+ 'dark-mode': true,
270
+ beta: false
271
+ },
272
+
273
+ logLevel: info // unquoted string value
274
+ }
275
+ ```
276
+
277
+ ### Reading `.jfx` files
278
+
279
+ ```javascript
280
+ const FlexJson = require('flex-json');
281
+
282
+ const config = new FlexJson();
283
+ config.DeserializeFlexFile('config.jfx');
284
+
285
+ console.log(config.getStr('database.host')); // "localhost"
286
+ ```
287
+
288
+ ## Editor Support
289
+
290
+ The FlexJson library parses `.jfx` files at runtime, but your code editor needs a separate extension for syntax highlighting. Editor extensions are **not** installed automatically with `npm install`.
291
+
292
+ ### VS Code
293
+
294
+ Install the **JFX - FlexJson Language Support** extension:
295
+
296
+ **Option 1: From VSIX (manual install)**
297
+ 1. Download the `.vsix` file from [releases](https://github.com/tedtyree/FlexJson/releases)
298
+ 2. In VS Code, open Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`)
299
+ 3. Run `Extensions: Install from VSIX...`
300
+ 4. Select the downloaded `.vsix` file
301
+
302
+ **Option 2: Build from source**
303
+ ```bash
304
+ cd extensions/vscode-jfx
305
+ npm install
306
+ npm run package
307
+ # Then install the generated .vsix file as above
308
+ ```
309
+
310
+ The extension provides:
311
+ - Syntax highlighting for `.jfx` files
312
+ - Comment support (`//` and `/* */`)
313
+ - Bracket matching and auto-closing
314
+ - Code folding
315
+
316
+ ### Neovim (Tree-sitter)
317
+
318
+ Add to your Tree-sitter configuration:
319
+
320
+ ```lua
321
+ local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
322
+ parser_config.jfx = {
323
+ install_info = {
324
+ url = "https://github.com/tedtyree/FlexJson",
325
+ files = {"extensions/tree-sitter-jfx/src/parser.c"},
326
+ branch = "main",
327
+ },
328
+ filetype = "jfx",
329
+ }
330
+
331
+ vim.filetype.add({
332
+ extension = {
333
+ jfx = "jfx",
334
+ },
335
+ })
336
+ ```
337
+
338
+ Then run `:TSInstall jfx`
339
+
340
+ ### Other Editors
341
+
342
+ The Tree-sitter grammar in `extensions/tree-sitter-jfx/` can be used with any editor that supports Tree-sitter (Emacs, Helix, Zed, etc.). See your editor's documentation for setup instructions.
343
+
344
+ ### Contributing
345
+
346
+ 1. Fork this repository.
347
+ 2. Create new branch with feature name.
348
+ 3. Create your feature.
349
+ 4. Commit and set commit message with feature name.
350
+ 5. Push your code to your fork repository.
351
+ 6. Create pull request.
352
+
353
+ ### Backlog
354
+ - Add support for """ and ''' multi-line quotes
355
+ - Publish as a VS Code extension
356
+ - Publish to Open VSX
357
+
358
+ ### Support
359
+
360
+ If you like this project, you can support us by starring ⭐ this repository or donating to [un0.org](https://un0.org/).
361
+
362
+ ### Acknowledgements
363
+
364
+ Special thanks to [un0.org](https://un0.org/) and [eBiashara Rahisi Ltd](https://ebiashararahisi.com/) for their work in Machakos, Kenya.
365
+
366
+ ### License
367
+
368
+ [MIT](LICENSE.txt)
369
+