flex-json 0.0.1
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 +1750 -0
- package/FlexJsonConstants.js +57 -0
- package/FlexJsonMeta.js +26 -0
- package/FlexJsonPosition.js +32 -0
- package/LICENSE.txt +21 -0
- package/README.md +178 -0
- package/package.json +29 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// jsonTypeEnum Values - Numeric representation of jsonType
|
|
2
|
+
//NOTE: The NUMBER VALUES are critical to the SORT() routine
|
|
3
|
+
// So that FlexJson arrays get sorted in this order: NULL, Number, String, Boolean, Array, Object, Error
|
|
4
|
+
const jsonTypeEnum_null = 0;
|
|
5
|
+
const jsonTypeEnum_number = 1;
|
|
6
|
+
const jsonTypeEnum_string = 2;
|
|
7
|
+
const jsonTypeEnum_boolean = 3;
|
|
8
|
+
const jsonTypeEnum_array = 10;
|
|
9
|
+
const jsonTypeEnum_object = 11;
|
|
10
|
+
const jsonTypeEnum_error = 90;
|
|
11
|
+
const jsonTypeEnum_invalid = 99;
|
|
12
|
+
|
|
13
|
+
// Indicates status of deserialize process.
|
|
14
|
+
// Order of numbers is important! (because we check for _status >= ST_STRING)
|
|
15
|
+
const ST_BEFORE_ITEM=0;
|
|
16
|
+
const ST_EOL_COMMENT=1;
|
|
17
|
+
const ST_AST_COMMENT=2;
|
|
18
|
+
const ST_EOL_COMMENT_POST=3;
|
|
19
|
+
const ST_AST_COMMENT_POST=4;
|
|
20
|
+
const ST_STRING=10;
|
|
21
|
+
const ST_STRING_NO_QUOTE=12;
|
|
22
|
+
const ST_AFTER_ITEM=99;
|
|
23
|
+
|
|
24
|
+
const NEWLINE="\r\n";
|
|
25
|
+
|
|
26
|
+
const typeObject = "object";
|
|
27
|
+
const typeArray = "array";
|
|
28
|
+
const typeString = "string";
|
|
29
|
+
const typeNumber = "number";
|
|
30
|
+
const typeBoolean = "boolean";
|
|
31
|
+
const typeNull = "null";
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
jsonTypeEnum_null
|
|
35
|
+
,jsonTypeEnum_number
|
|
36
|
+
,jsonTypeEnum_string
|
|
37
|
+
,jsonTypeEnum_boolean
|
|
38
|
+
,jsonTypeEnum_array
|
|
39
|
+
,jsonTypeEnum_object
|
|
40
|
+
,jsonTypeEnum_error
|
|
41
|
+
,jsonTypeEnum_invalid
|
|
42
|
+
,ST_BEFORE_ITEM
|
|
43
|
+
,ST_EOL_COMMENT
|
|
44
|
+
,ST_AST_COMMENT
|
|
45
|
+
,ST_EOL_COMMENT_POST
|
|
46
|
+
,ST_AST_COMMENT_POST
|
|
47
|
+
,ST_STRING
|
|
48
|
+
,ST_STRING_NO_QUOTE
|
|
49
|
+
,ST_AFTER_ITEM
|
|
50
|
+
,NEWLINE
|
|
51
|
+
,typeObject
|
|
52
|
+
,typeArray
|
|
53
|
+
,typeString
|
|
54
|
+
,typeNumber
|
|
55
|
+
,typeBoolean
|
|
56
|
+
,typeNull
|
|
57
|
+
}
|
package/FlexJsonMeta.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class FlexJsonMeta
|
|
2
|
+
{
|
|
3
|
+
statusMsg = null;
|
|
4
|
+
tmpStatusMsg = null;
|
|
5
|
+
msgLog = null;
|
|
6
|
+
msgLogOn = false;
|
|
7
|
+
errorPosition = null; // indicator of error position during deserialization process
|
|
8
|
+
startPosition = null; // indicator of start position during deserialization process
|
|
9
|
+
endPosition = null; // indicator of final position during deserialization process
|
|
10
|
+
|
|
11
|
+
// When/if this object exists, it will include flags to indicate if the object is intended to keep spacing and/or comments
|
|
12
|
+
// keepSpacing - false=DO NOT preserve spacing, true=Preserve spacing including carriage returns
|
|
13
|
+
// keepComments - false=DO NOT preseerve comments, true=Preserve comments in Flex JSON
|
|
14
|
+
// NOTE: These flags only have an affect during the deserialize process.
|
|
15
|
+
// FUTURE: How to clear spacing/comments after the load? (set _keep to null? or just keep/pre/post attributes) Make this recursive to sub-objects?
|
|
16
|
+
// FUTURE: Flag to keep double/single quotes or no-quotes for Flex JSON (and write back to file using same quotes or no-quote per item)
|
|
17
|
+
keepSpacing = false;
|
|
18
|
+
keepComments = false;
|
|
19
|
+
preSpace = null;
|
|
20
|
+
finalSpace = null;
|
|
21
|
+
preKey = null;
|
|
22
|
+
postKey = null;
|
|
23
|
+
stats = null; // null indicates we are not tracking stats. stats object will also be created if an error occurs - to hold the error message.
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = FlexJsonMeta;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class FlexJsonPosition
|
|
2
|
+
{
|
|
3
|
+
lineNumber;
|
|
4
|
+
linePosition;
|
|
5
|
+
absolutePosition;
|
|
6
|
+
|
|
7
|
+
constructor(initLineNumber, initLinePosition, initAbsolutePosition) {
|
|
8
|
+
this.lineNumber = initLineNumber;
|
|
9
|
+
this.linePosition = initLinePosition;
|
|
10
|
+
this.absolutePosition = initAbsolutePosition;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
increment(positionIncrement = 1) {
|
|
14
|
+
this.linePosition += positionIncrement;
|
|
15
|
+
this.absolutePosition += positionIncrement;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// IncrementLine() - tracks the occurrance of a line break.
|
|
19
|
+
// AbsoluteIncrement must be provided because on some systems line breaks are 2 characters
|
|
20
|
+
incrementLine(absoluteIncrement, lineIncrement = 1) {
|
|
21
|
+
this.lineNumber += lineIncrement;
|
|
22
|
+
this.linePosition = 0;
|
|
23
|
+
this.absolutePosition += absoluteIncrement;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
clone() {
|
|
27
|
+
let newClone = new FlexJsonPosition(this.lineNumber,this.linePosition,this.absolutePosition);
|
|
28
|
+
return newClone;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = FlexJsonPosition;
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ted Tyree, Michael Njuguna
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# FlexJson
|
|
2
|
+
|
|
3
|
+
## Flexible JSON manipulation library for JavaScript.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Install](#install)
|
|
8
|
+
- [Usage](#usage)
|
|
9
|
+
- [Why flex-json](#why-flex-json)
|
|
10
|
+
- [How the library works](#how-the-library-works)
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# NPM
|
|
16
|
+
npm install flex-json
|
|
17
|
+
|
|
18
|
+
#PNPM
|
|
19
|
+
pnpm install flex-json
|
|
20
|
+
|
|
21
|
+
# Yarn
|
|
22
|
+
Yarn install flex-json
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
const FlexJson = require('flex-json');
|
|
30
|
+
|
|
31
|
+
// Create a FlexJson object
|
|
32
|
+
const myJson = new FlexJson('{"key": "value"}', true);
|
|
33
|
+
|
|
34
|
+
// Access properties
|
|
35
|
+
console.log(myJson.jsonString); // Get JSON string representation
|
|
36
|
+
|
|
37
|
+
// Modify properties
|
|
38
|
+
myJson.keepSpacing = true; // Preserve spacing during deserialization
|
|
39
|
+
|
|
40
|
+
// Check status and type
|
|
41
|
+
console.log(myJson.Status); // Get status
|
|
42
|
+
console.log(myJson.jsonType); // Get JSON type
|
|
43
|
+
|
|
44
|
+
// Manipulate JSON object
|
|
45
|
+
myJson.i('key').thisValue = 'new value'; // Set a new value for a key
|
|
46
|
+
|
|
47
|
+
// Convert JSON object to array
|
|
48
|
+
myJson.ConvertToArray();
|
|
49
|
+
|
|
50
|
+
// Access array elements
|
|
51
|
+
console.log(myJson.item(0).thisValue); // Access first element in the array
|
|
52
|
+
|
|
53
|
+
// Use new methods
|
|
54
|
+
myJson.forEach(item => {
|
|
55
|
+
console.log(item.jsonString); // Iterate through each item and log JSON string
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
myJson.add('new item', 'newKey'); // Add a new item to the JSON object
|
|
59
|
+
|
|
60
|
+
console.log(myJson.indexOfKey('newKey')); // Get the index of a key in the JSON object
|
|
61
|
+
|
|
62
|
+
console.log(myJson.contains('key')); // Check if a key exists in the JSON object
|
|
63
|
+
|
|
64
|
+
console.log(myJson.getStr('key', 'default')); // Get string value by key with a default value
|
|
65
|
+
|
|
66
|
+
console.log(myJson.getNum('count', 0)); // Get numeric value by key with a default value
|
|
67
|
+
|
|
68
|
+
console.log(myJson.getBool('flag', false)); // Get boolean value by key with a default value
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Serialization and Deserialization examples
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
const FlexJson = require("flex-json");
|
|
76
|
+
|
|
77
|
+
// Create an instance of FlexJson
|
|
78
|
+
const flexJson = new FlexJson();
|
|
79
|
+
|
|
80
|
+
// Example: Deserialize JSON
|
|
81
|
+
const jsonString = '{"name": "John", "age": 30, "city": "New York"}';
|
|
82
|
+
flexJson.Deserialize(jsonString);
|
|
83
|
+
|
|
84
|
+
// Example: Serialize to JSON
|
|
85
|
+
flexJson.SerializeMe();
|
|
86
|
+
|
|
87
|
+
// Access serialized JSON string
|
|
88
|
+
const serializedJson = flexJson.SerializeMe();
|
|
89
|
+
console.log(serializedJson);
|
|
90
|
+
```
|
|
91
|
+
|
|
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 formattted Json in web page content.
|
|
101
|
+
|
|
102
|
+
## How the library works
|
|
103
|
+
|
|
104
|
+
### Flex-json syntax
|
|
105
|
+
|
|
106
|
+
BTW FlexJson 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 FlexJson 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 FlexJson library has the following features:
|
|
117
|
+
|
|
118
|
+
- Like JavaScript, comments can be surrounded by /* (start of comment) and */ (end of comment)
|
|
119
|
+
|
|
120
|
+
- Like Javasrcipt, 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:__
|
|
129
|
+
|
|
130
|
+
```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
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
__example 3:__
|
|
144
|
+
|
|
145
|
+
```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
|
+
]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
__Note__ that {number:"2"} is not the same as {number:2} because FlexJson will see that the 2 without quotes is a valid number and load it as a numeric.
|
|
156
|
+
|
|
157
|
+
### Contributing
|
|
158
|
+
|
|
159
|
+
1. Fork this repository.
|
|
160
|
+
2. Create new branch with feature name.
|
|
161
|
+
3. Create your feature.
|
|
162
|
+
4. Commit and set commit message with feature name.
|
|
163
|
+
5. Push your code to your fork repository.
|
|
164
|
+
6. Create pull request.
|
|
165
|
+
|
|
166
|
+
### Support
|
|
167
|
+
|
|
168
|
+
If you like this project, You can support us with starring ⭐ this repository or donate to [uO.heartofkenya.com](https://u0.heartofkenya.com/).
|
|
169
|
+
|
|
170
|
+
### License
|
|
171
|
+
|
|
172
|
+
[MIT](LICENSE.txt)
|
|
173
|
+
|
|
174
|
+
Made with 💙
|
|
175
|
+
|
|
176
|
+
### Acknowledgements
|
|
177
|
+
|
|
178
|
+
Special thanks to [u0.heartofKenya.com](https://u0.heartofkenya.com/) and [ebiashararahisi](https://ebiashararahisi.com/) for their work in Machakos, Kenya.
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "flex-json",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "This is a library that makes each 'Node' in the JSON Object/Array a JSON Object with a Key/Value pair.",
|
|
5
|
+
"main": "FlexJsonClass.js",
|
|
6
|
+
"authors": "[michael njuguna michaelnjuguna184@gmail.com, ted tyree tedtyree@gmail.com]",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"JSON",
|
|
9
|
+
"JSON Object",
|
|
10
|
+
"JSON Array",
|
|
11
|
+
"JSON Object Array",
|
|
12
|
+
"flexjson"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"string-builder": "^0.1.8"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/tedtyree/FlexJson.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/tedtyree/FlexJson/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/tedtyree/FlexJson",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT"
|
|
29
|
+
}
|