flex-json 0.0.4 → 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 +71 -57
- package/README.md +98 -57
- package/package.json +1 -1
- package/get/index.js +0 -27
- package/set/index.js +0 -0
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
|
-
|
|
6
|
+
|
|
7
7
|
|
|
8
8
|
class FlexJson {
|
|
9
9
|
_status = 0;
|
|
@@ -36,31 +36,24 @@ class FlexJson {
|
|
|
36
36
|
if (this._NoStatsOrMsgs && !force) {
|
|
37
37
|
return;
|
|
38
38
|
} // Do not track stats/message/meta-data
|
|
39
|
-
if (this._meta
|
|
40
|
-
this._meta =
|
|
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
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// }
|
|
62
|
-
// return "";
|
|
63
|
-
return statusMsg(this._meta);
|
|
53
|
+
if (this._meta != null && this._meta.statusMsg != null) {
|
|
54
|
+
return meta.statusMsg;
|
|
55
|
+
}
|
|
56
|
+
return "";
|
|
64
57
|
}
|
|
65
58
|
set statusMsg(value) {
|
|
66
59
|
if (this.NoStatsOrMsgs) {
|
|
@@ -78,18 +71,15 @@ class FlexJson {
|
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
get tmpStatusMsg() {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
// }
|
|
86
|
-
// return "";
|
|
87
|
-
return tmpStatusMsg(this._meta);
|
|
74
|
+
if (this._meta && this._meta.tmpStatusMsg) {
|
|
75
|
+
return this._meta.tmpStatusMsg;
|
|
76
|
+
}
|
|
77
|
+
return "";
|
|
88
78
|
}
|
|
89
79
|
set tmpStatusMsg(value) {
|
|
90
80
|
this.createMetaIfNeeded();
|
|
91
|
-
if (_meta
|
|
92
|
-
_meta.tmpStatusMsg = value;
|
|
81
|
+
if (this._meta) {
|
|
82
|
+
this._meta.tmpStatusMsg = value;
|
|
93
83
|
}
|
|
94
84
|
}
|
|
95
85
|
|
|
@@ -98,16 +88,16 @@ class FlexJson {
|
|
|
98
88
|
}
|
|
99
89
|
|
|
100
90
|
set key(value) {
|
|
101
|
-
this._key =
|
|
91
|
+
this._key = "" + value; // convert to string (future: allow numeric key?)
|
|
102
92
|
}
|
|
103
93
|
|
|
104
94
|
get preSpace() {
|
|
105
|
-
if (this._meta != null) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
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;
|
|
111
101
|
}
|
|
112
102
|
|
|
113
103
|
set preSpace(value) {
|
|
@@ -118,12 +108,12 @@ class FlexJson {
|
|
|
118
108
|
}
|
|
119
109
|
|
|
120
110
|
get postSpace() {
|
|
121
|
-
if (this._meta != null) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
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;
|
|
127
117
|
}
|
|
128
118
|
|
|
129
119
|
set postSpace(value) {
|
|
@@ -183,7 +173,7 @@ class FlexJson {
|
|
|
183
173
|
|
|
184
174
|
get keepSpacing() {
|
|
185
175
|
if (this._meta != null) {
|
|
186
|
-
return this._meta.keepSpacing;
|
|
176
|
+
return this._meta.keepSpacing || false;
|
|
187
177
|
}
|
|
188
178
|
return false;
|
|
189
179
|
}
|
|
@@ -196,7 +186,7 @@ class FlexJson {
|
|
|
196
186
|
|
|
197
187
|
get keepComments() {
|
|
198
188
|
if (this._meta != null) {
|
|
199
|
-
return this._meta.keepComments;
|
|
189
|
+
return this._meta.keepComments || false;
|
|
200
190
|
}
|
|
201
191
|
return false;
|
|
202
192
|
}
|
|
@@ -486,7 +476,6 @@ class FlexJson {
|
|
|
486
476
|
forEach(callback) {
|
|
487
477
|
if (this.jsonType == "object" || this.jsonType == "array") {
|
|
488
478
|
for (let i = 0; i < this.length; i++) {
|
|
489
|
-
let jj = this.i(i); // debugger
|
|
490
479
|
callback(this.i(i));
|
|
491
480
|
} // end for
|
|
492
481
|
} else {
|
|
@@ -502,11 +491,6 @@ class FlexJson {
|
|
|
502
491
|
|
|
503
492
|
add(value, idx = "", dotNotation = true) {
|
|
504
493
|
var newV;
|
|
505
|
-
var debugType = typeof value; /// debug debug debug
|
|
506
|
-
if (value === null) {
|
|
507
|
-
/// debug debug debug
|
|
508
|
-
console.log("is null");
|
|
509
|
-
}
|
|
510
494
|
if (
|
|
511
495
|
value !== null &&
|
|
512
496
|
typeof value === "object" &&
|
|
@@ -713,9 +697,6 @@ class FlexJson {
|
|
|
713
697
|
if (i > 0) {
|
|
714
698
|
s.append(",");
|
|
715
699
|
}
|
|
716
|
-
if (o.key == "data") {
|
|
717
|
-
console.log("debug here111"); // debug debug debug
|
|
718
|
-
}
|
|
719
700
|
let k = o.SerializeMe();
|
|
720
701
|
|
|
721
702
|
// Here we ignore keepSpacing/keepComments - these flags are only used during the deserialize process
|
|
@@ -1007,7 +988,7 @@ class FlexJson {
|
|
|
1007
988
|
// if we are no longer in pre-space territory, the we need to store the whitespace/comments
|
|
1008
989
|
if (meStatus >= FlexJsonConstants.ST_STRING) {
|
|
1009
990
|
if (keepCM || keepSP) {
|
|
1010
|
-
preSpace = getSpace;
|
|
991
|
+
this.preSpace = getSpace;
|
|
1011
992
|
getSpace = ""; // clear
|
|
1012
993
|
}
|
|
1013
994
|
}
|
|
@@ -1373,7 +1354,15 @@ class FlexJson {
|
|
|
1373
1354
|
|
|
1374
1355
|
CreatePartClone(keepSP = false, keepCM = false) {
|
|
1375
1356
|
let jClone = new FlexJson();
|
|
1376
|
-
|
|
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
|
+
}
|
|
1377
1366
|
jClone.ALLOW_SINGLE_QUOTE_STRINGS = this.ALLOW_SINGLE_QUOTE_STRINGS;
|
|
1378
1367
|
return jClone;
|
|
1379
1368
|
}
|
|
@@ -1382,6 +1371,8 @@ class FlexJson {
|
|
|
1382
1371
|
var j2;
|
|
1383
1372
|
var jNew;
|
|
1384
1373
|
let Key = "";
|
|
1374
|
+
let preKey = "";
|
|
1375
|
+
let postKey = "";
|
|
1385
1376
|
var c;
|
|
1386
1377
|
|
|
1387
1378
|
let v = [];
|
|
@@ -1448,8 +1439,8 @@ class FlexJson {
|
|
|
1448
1439
|
} else {
|
|
1449
1440
|
// capture the white space/comments here
|
|
1450
1441
|
if (keepCM || keepSP) {
|
|
1451
|
-
|
|
1452
|
-
|
|
1442
|
+
preKey = j2.preSpace;
|
|
1443
|
+
postKey = j2.finalSpace;
|
|
1453
1444
|
}
|
|
1454
1445
|
Key = j2._value; // already verified that this is type "string"
|
|
1455
1446
|
this._keyQuote = j2._stringQuote;
|
|
@@ -1516,6 +1507,12 @@ class FlexJson {
|
|
|
1516
1507
|
// *** For all cases: object, array, string, number, boolean, or null
|
|
1517
1508
|
jNew.Parent = this;
|
|
1518
1509
|
jNew._key = Key;
|
|
1510
|
+
if (preKey) {
|
|
1511
|
+
jNew.preKey = preKey;
|
|
1512
|
+
}
|
|
1513
|
+
if (postKey) {
|
|
1514
|
+
jNew.postKey = postKey;
|
|
1515
|
+
}
|
|
1519
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.
|
|
1520
1517
|
mePos = finalPos;
|
|
1521
1518
|
}
|
|
@@ -1696,8 +1693,8 @@ class FlexJson {
|
|
|
1696
1693
|
DeserializeFlexFile(
|
|
1697
1694
|
FilePath,
|
|
1698
1695
|
OkToClip = false,
|
|
1699
|
-
spacing_flag =
|
|
1700
|
-
comments_flag =
|
|
1696
|
+
spacing_flag = 1, // default = keep spacing & comments
|
|
1697
|
+
comments_flag = 1
|
|
1701
1698
|
) {
|
|
1702
1699
|
this.UseFlexJson = true;
|
|
1703
1700
|
if (spacing_flag >= 0 || comments_flag >= 0) {
|
|
@@ -1706,6 +1703,23 @@ class FlexJson {
|
|
|
1706
1703
|
return this.DeserializeFile(FilePath, OkToClip);
|
|
1707
1704
|
}
|
|
1708
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
|
+
|
|
1709
1723
|
StatusErr(nErr, strErr) {
|
|
1710
1724
|
this._status = nErr;
|
|
1711
1725
|
this.AddStatusMessage(strErr);
|
package/README.md
CHANGED
|
@@ -11,10 +11,75 @@
|
|
|
11
11
|
|
|
12
12
|
## Table of Contents
|
|
13
13
|
|
|
14
|
-
- [Install](#install)
|
|
15
|
-
- [Usage](#usage)
|
|
16
14
|
- [Why flex-json](#why-flex-json)
|
|
17
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.
|
|
18
83
|
|
|
19
84
|
## Install
|
|
20
85
|
|
|
@@ -96,70 +161,46 @@ const serializedJson = flexJson.SerializeMe();
|
|
|
96
161
|
console.log(serializedJson);
|
|
97
162
|
```
|
|
98
163
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
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.
|
|
102
|
-
|
|
103
|
-
- Easy config file formatting
|
|
104
|
-
- Includes comments in both /* */ and // notation
|
|
105
|
-
- Simple to edit Json files
|
|
106
|
-
- Allows for other JavaScript like features such as using either single quotes or double quotes.
|
|
107
|
-
- 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.
|
|
108
|
-
|
|
109
|
-
## How the library works
|
|
110
|
-
|
|
111
|
-
### Flex-json syntax
|
|
112
|
-
|
|
113
|
-
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.
|
|
114
|
-
|
|
115
|
-
### Strict Mode
|
|
116
|
-
|
|
117
|
-
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.
|
|
118
|
-
|
|
119
|
-
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.
|
|
120
|
-
|
|
121
|
-
### Flex Mode
|
|
122
|
-
|
|
123
|
-
When in flex mode, the flex-json library has the following features:
|
|
124
|
-
|
|
125
|
-
- Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)
|
|
126
|
-
|
|
127
|
-
- Like JavaScript, when a "//" is encountered, the remainder of the line is considered to be a comment
|
|
128
|
-
|
|
129
|
-
- Strings do not require quotes unless they contain special characters
|
|
130
|
-
|
|
131
|
-
- Strings can be quoted using double quotes or single quotes
|
|
132
|
-
|
|
133
|
-
When in flex mode, all of the following examples of Json are valid:
|
|
134
|
-
|
|
135
|
-
__example 1:__
|
|
136
|
-
|
|
137
|
-
```javascript
|
|
138
|
-
{apple: red, banana: yellow, 'sky': 'blue'}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
__example 2:__
|
|
164
|
+
Config file example (this is the best part!)
|
|
142
165
|
|
|
166
|
+
First create a json config file for example a text file c:/temp/my-config.json containing the following text…
|
|
143
167
|
```javascript
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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'
|
|
147
175
|
}
|
|
148
176
|
```
|
|
149
177
|
|
|
150
|
-
|
|
178
|
+
In your node.js app run these commands…
|
|
151
179
|
|
|
152
180
|
```javascript
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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);
|
|
160
197
|
```
|
|
161
198
|
|
|
162
|
-
|
|
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
|
|
163
204
|
|
|
164
205
|
### Contributing
|
|
165
206
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flex-json",
|
|
3
|
-
"version": "0.0.
|
|
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,27 +0,0 @@
|
|
|
1
|
-
function trackingStats(meta) {
|
|
2
|
-
if (meta == null || meta.status == null) {
|
|
3
|
-
return false;
|
|
4
|
-
}
|
|
5
|
-
return true;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function statusMsg(meta){
|
|
9
|
-
if(meta != null && meta.statusMsg != null){
|
|
10
|
-
return meta.statusMsg;
|
|
11
|
-
}
|
|
12
|
-
return '';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function tmpStatusMsg(meta){
|
|
16
|
-
if(meta != null && meta.tmpStatusMsg != null){
|
|
17
|
-
return meta.tmpStatusMsg;
|
|
18
|
-
}
|
|
19
|
-
return '';
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// exports
|
|
23
|
-
module.exports = {
|
|
24
|
-
trackingStats: trackingStats,
|
|
25
|
-
statusMsg: statusMsg,
|
|
26
|
-
tmpStatusMsg: tmpStatusMsg
|
|
27
|
-
};
|
package/set/index.js
DELETED
|
File without changes
|