flex-json 0.0.3 → 0.0.5

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/FlexJsonClass.js CHANGED
@@ -3,7 +3,7 @@ const FlexJsonPosition = require("./FlexJsonPosition.js");
3
3
  const FlexJsonMeta = require("./FlexJsonMeta.js");
4
4
  const fs = require("fs");
5
5
  const StringBuilder = require("string-builder");
6
- const { trackingStats } = require( "./get/index.js");
6
+
7
7
 
8
8
  class FlexJson {
9
9
  _status = 0;
@@ -36,28 +36,22 @@ class FlexJson {
36
36
  if (this._NoStatsOrMsgs && !force) {
37
37
  return;
38
38
  } // Do not track stats/message/meta-data
39
- if (this._meta == null) {
40
- this._meta = new FlexJsonMeta();
39
+ if (!this._meta) {
40
+ this._meta = {};
41
41
  }
42
42
  }
43
43
 
44
- // get trackingStats() {
45
- // if (this._meta == null) return false;
46
- // if (this._meta.stats == null) return false;
47
- // return true;
48
- // }
49
- // use the imported trackingStats function instead
50
44
 
51
45
  get trackingStats() {
52
- return trackingStats(this._meta);
46
+ if (this._meta == null || this._meta.status == null) {
47
+ return false;
48
+ }
49
+ return true;
53
50
  }
54
51
 
55
-
56
52
  get statusMsg() {
57
- if (this._meta != null) {
58
- if (this._meta.statusMsg != null) {
59
- return this._meta.statusMsg;
60
- }
53
+ if (this._meta != null && this._meta.statusMsg != null) {
54
+ return meta.statusMsg;
61
55
  }
62
56
  return "";
63
57
  }
@@ -77,17 +71,15 @@ class FlexJson {
77
71
  }
78
72
 
79
73
  get tmpStatusMsg() {
80
- if (this._meta != null) {
81
- if (this._meta.tmpStatusMsg != null) {
82
- return this._meta.tmpStatusMsg;
83
- }
74
+ if (this._meta && this._meta.tmpStatusMsg) {
75
+ return this._meta.tmpStatusMsg;
84
76
  }
85
77
  return "";
86
78
  }
87
79
  set tmpStatusMsg(value) {
88
80
  this.createMetaIfNeeded();
89
- if (_meta != null) {
90
- _meta.tmpStatusMsg = value;
81
+ if (this._meta) {
82
+ this._meta.tmpStatusMsg = value;
91
83
  }
92
84
  }
93
85
 
@@ -96,16 +88,16 @@ class FlexJson {
96
88
  }
97
89
 
98
90
  set key(value) {
99
- this._key = value + "";
91
+ this._key = "" + value; // convert to string (future: allow numeric key?)
100
92
  }
101
93
 
102
94
  get preSpace() {
103
- if (this._meta != null) {
104
- if (this._meta.preSpace != null) {
105
- return this._meta.preSpace;
106
- }
107
- }
108
- return null;
95
+ //if (this._meta != null) {
96
+ // if (this._meta.preSpace != null) {
97
+ // return this._meta.preSpace;
98
+ // }
99
+ //}
100
+ return this._meta && this._meta.preSpace ? this._meta.preSpace : null;
109
101
  }
110
102
 
111
103
  set preSpace(value) {
@@ -116,12 +108,12 @@ class FlexJson {
116
108
  }
117
109
 
118
110
  get postSpace() {
119
- if (this._meta != null) {
120
- if (this._meta.postSpace != null) {
121
- return this._meta.postSpace;
122
- }
123
- }
124
- return null;
111
+ //if (this._meta != null) {
112
+ // if (this._meta.postSpace != null) {
113
+ // return this._meta.postSpace;
114
+ // }
115
+ //}
116
+ return this._meta && this._meta.postSpace ? this._meta.postSpace : null;
125
117
  }
126
118
 
127
119
  set postSpace(value) {
@@ -181,7 +173,7 @@ class FlexJson {
181
173
 
182
174
  get keepSpacing() {
183
175
  if (this._meta != null) {
184
- return this._meta.keepSpacing;
176
+ return this._meta.keepSpacing || false;
185
177
  }
186
178
  return false;
187
179
  }
@@ -194,7 +186,7 @@ class FlexJson {
194
186
 
195
187
  get keepComments() {
196
188
  if (this._meta != null) {
197
- return this._meta.keepComments;
189
+ return this._meta.keepComments || false;
198
190
  }
199
191
  return false;
200
192
  }
@@ -484,7 +476,6 @@ class FlexJson {
484
476
  forEach(callback) {
485
477
  if (this.jsonType == "object" || this.jsonType == "array") {
486
478
  for (let i = 0; i < this.length; i++) {
487
- let jj = this.i(i); // debugger
488
479
  callback(this.i(i));
489
480
  } // end for
490
481
  } else {
@@ -500,11 +491,6 @@ class FlexJson {
500
491
 
501
492
  add(value, idx = "", dotNotation = true) {
502
493
  var newV;
503
- var debugType = typeof value; /// debug debug debug
504
- if (value === null) {
505
- /// debug debug debug
506
- console.log("is null");
507
- }
508
494
  if (
509
495
  value !== null &&
510
496
  typeof value === "object" &&
@@ -711,9 +697,6 @@ class FlexJson {
711
697
  if (i > 0) {
712
698
  s.append(",");
713
699
  }
714
- if (o.key == "data") {
715
- console.log("debug here111"); // debug debug debug
716
- }
717
700
  let k = o.SerializeMe();
718
701
 
719
702
  // Here we ignore keepSpacing/keepComments - these flags are only used during the deserialize process
@@ -1005,7 +988,7 @@ class FlexJson {
1005
988
  // if we are no longer in pre-space territory, the we need to store the whitespace/comments
1006
989
  if (meStatus >= FlexJsonConstants.ST_STRING) {
1007
990
  if (keepCM || keepSP) {
1008
- preSpace = getSpace;
991
+ this.preSpace = getSpace;
1009
992
  getSpace = ""; // clear
1010
993
  }
1011
994
  }
@@ -1371,7 +1354,15 @@ class FlexJson {
1371
1354
 
1372
1355
  CreatePartClone(keepSP = false, keepCM = false) {
1373
1356
  let jClone = new FlexJson();
1374
- jClone.UseFlexJson = this.UseFlexJson;
1357
+ if (this.UseFlexJson) {
1358
+ jClone.UseFlexJson = true;
1359
+ }
1360
+ if (keepSP) {
1361
+ jClone.keepSpacing = true;
1362
+ }
1363
+ if (keepCM) {
1364
+ jClone.keepComments = true;
1365
+ }
1375
1366
  jClone.ALLOW_SINGLE_QUOTE_STRINGS = this.ALLOW_SINGLE_QUOTE_STRINGS;
1376
1367
  return jClone;
1377
1368
  }
@@ -1380,6 +1371,8 @@ class FlexJson {
1380
1371
  var j2;
1381
1372
  var jNew;
1382
1373
  let Key = "";
1374
+ let preKey = "";
1375
+ let postKey = "";
1383
1376
  var c;
1384
1377
 
1385
1378
  let v = [];
@@ -1446,8 +1439,8 @@ class FlexJson {
1446
1439
  } else {
1447
1440
  // capture the white space/comments here
1448
1441
  if (keepCM || keepSP) {
1449
- this.preKey = j2.preSpace;
1450
- this.postKey = j2.finalSpace;
1442
+ preKey = j2.preSpace;
1443
+ postKey = j2.finalSpace;
1451
1444
  }
1452
1445
  Key = j2._value; // already verified that this is type "string"
1453
1446
  this._keyQuote = j2._stringQuote;
@@ -1514,6 +1507,12 @@ class FlexJson {
1514
1507
  // *** For all cases: object, array, string, number, boolean, or null
1515
1508
  jNew.Parent = this;
1516
1509
  jNew._key = Key;
1510
+ if (preKey) {
1511
+ jNew.preKey = preKey;
1512
+ }
1513
+ if (postKey) {
1514
+ jNew.postKey = postKey;
1515
+ }
1517
1516
  v.push(jNew); // FUTURE: IS THIS WRONG? SHOULD WE CHECK TO SEE IF THE KEY ALREADY EXISTS? AS IS, THE FIRST VALUE WILL "overshadow" ANY SUBSEQUENT VALUE. MAYBE THIS IS OK.
1518
1517
  mePos = finalPos;
1519
1518
  }
@@ -1694,8 +1693,8 @@ class FlexJson {
1694
1693
  DeserializeFlexFile(
1695
1694
  FilePath,
1696
1695
  OkToClip = false,
1697
- spacing_flag = -1,
1698
- comments_flag = -1
1696
+ spacing_flag = 1, // default = keep spacing & comments
1697
+ comments_flag = 1
1699
1698
  ) {
1700
1699
  this.UseFlexJson = true;
1701
1700
  if (spacing_flag >= 0 || comments_flag >= 0) {
@@ -1704,6 +1703,23 @@ class FlexJson {
1704
1703
  return this.DeserializeFile(FilePath, OkToClip);
1705
1704
  }
1706
1705
 
1706
+ // WriteToFile()
1707
+ // If object is setup as FlexJSON and with KeepSpacing/KeepComments, then file will be written accordingly
1708
+ // Return: 0=ok, -1=error, -2=could not write because of invalid FlexJson object
1709
+ WriteToFile(FilePath) {
1710
+ try {
1711
+ let outString = this.jsonString;
1712
+ if (this._status == 0) {
1713
+ fs.writeFileSync(FilePath, outString, { encoding: "utf8" });
1714
+ } else {
1715
+ return -2;
1716
+ }
1717
+ } catch {
1718
+ return -1;
1719
+ }
1720
+ return 0;
1721
+ }
1722
+
1707
1723
  StatusErr(nErr, strErr) {
1708
1724
  this._status = nErr;
1709
1725
  this.AddStatusMessage(strErr);
package/README.md CHANGED
@@ -1,13 +1,85 @@
1
1
  # flex-json
2
2
 
3
- ## Flexible JSON manipulation library for JavaScript.
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/)|
4
11
 
5
12
  ## Table of Contents
6
13
 
7
- - [Install](#install)
8
- - [Usage](#usage)
9
14
  - [Why flex-json](#why-flex-json)
10
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.
11
83
 
12
84
  ## Install
13
85
 
@@ -89,70 +161,46 @@ const serializedJson = flexJson.SerializeMe();
89
161
  console.log(serializedJson);
90
162
  ```
91
163
 
92
- ## Why flex-json
93
-
94
- 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.
95
-
96
- - Easy config file formatting
97
- - Includes comments in both /* */ and // notation
98
- - Simple to edit Json files
99
- - Allows for other JavaScript like features such as using either single quotes or double quotes.
100
- - 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.
101
-
102
- ## How the library works
103
-
104
- ### Flex-json syntax
105
-
106
- 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.
107
-
108
- ### Strict Mode
109
-
110
- 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.
111
-
112
- 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.
113
-
114
- ### Flex Mode
115
-
116
- When in flex mode, the flex-json library has the following features:
117
-
118
- - Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)
119
-
120
- - Like JavaScript, when a "//" is encountered, the remainder of the line is considered to be a comment
121
-
122
- - Strings do not require quotes unless they contain special characters
123
-
124
- - Strings can be quoted using double quotes or single quotes
125
-
126
- When in flex mode, all of the following examples of Json are valid:
127
-
128
- __example 1:__
164
+ Config file example (this is the best part!)
129
165
 
166
+ First create a json config file for example a text file c:/temp/my-config.json containing the following text…
130
167
  ```javascript
131
- {apple: red, banana: yellow, 'sky': 'blue'}
132
- ```
133
-
134
- __example 2:__
135
-
136
- ```javascript
137
- {"apple": "red"
138
- ,'banana': 'yellow'
139
- // ,'sky': 'blue' - this line is commented out
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'
140
175
  }
141
176
  ```
142
177
 
143
- __example 3:__
178
+ In your node.js app run these commands…
144
179
 
145
180
  ```javascript
146
- [ "one, is first"
147
- ,'two, is next'
148
- /* comment out remainder of array
149
- ,"three, is third"
150
- ,'four', is last"
151
- */
152
- ]
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);
153
197
  ```
154
198
 
155
- __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.
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
156
204
 
157
205
  ### Contributing
158
206
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flex-json",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "This is a library that makes each 'Node' in the JSON Object/Array a JSON Object with a Key/Value pair.",
5
5
  "main": "FlexJsonClass.js",
6
6
  "authors": "[michael njuguna michaelnjuguna184@gmail.com, ted tyree tedtyree@gmail.com]",
package/get/index.js DELETED
@@ -1,11 +0,0 @@
1
- function trackingStats(meta) {
2
- if (meta == null || meta.status == null) {
3
- return false;
4
- }
5
- return true;
6
- }
7
-
8
- // exports
9
- module.exports = {
10
- trackingStats: trackingStats
11
- };