-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathWorkJSON.js
222 lines (173 loc) · 8.04 KB
/
WorkJSON.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
WorkJSON.js (2kB minified)
JSON extended with:
- Optional trailing commas on arrays and objects
- C++ single and multline comments
- `Backtick` multiline strings
- Explicit plus sign permitted on numbers
- Numbers with bare leading or trailing decimal
- NaN, Infinity, -Infinity
- Hexadecimal numbers
- JavaScript undefined
- Optional unquoted object keys using [A-Za-z_0-9]+ characters only
See also ../tools/betterjson.py for the Python version
Just use WorkJSON.parse and WorkJSON.stringify in place of
JSON.parse and JSON.stringify. They have the same API.
WorkJSON.parse is 100% backwards compatible to JSON.parse
as long as your input has no characters in the Unicode private
use area \uE000 through \uF8FF.
The output of WorkJSON.stringify is backwards compatible to
JSON as long as you don't use NaN or Infinity values, which
JSON.stringify would error out on anyway.
The implementation is designed to use only regexps and a vanilla
JSON parser to simplify porting to other languages.
This is similar to JSON5 (https://json5.org/) but is about 10x
smaller. The primary difference is that WorkJSON intentionally
does not support unicode in unquoted keys.
By @CasualEffects
------------------------------------------------------------------
OSI MIT LICENSE
Copyright 2020 Morgan McGuire, https://casual-effects.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Polyfill for IE
if (! String.prototype.replaceAll) {
String.prototype.replaceAll = function(pattern, str) {
return this.replace(typeof pattern === 'object' ? pattern : new RegExp(pattern, 'g'), str);
};
}
const WorkJSON = (function () {
// Use the Multilinguial plane Unicode private use area,
// which has room for over 6000 strings that should not be
// in use by the source text.
//
// https://en.wikipedia.org/wiki/Private_Use_Areas
//
// Must also match hardcoded values below in regexps
const doubleQuoteProtection = '\uE000';
const NaNSymbol = '\uE001';
const InfinitySymbol = '\uE002';
const NegInfinitySymbol = '\uE003';
const UndefinedSymbol = '\uE004';
const ESCAPED_ESCAPE = '\uE009';
const protectionBlockStart = 0xE010;
return {
/** Returns the new string and a remapping array. If an array is
provided, then it is extended. */
protectQuotedStrings: function protectQuotedStrings(src, protectionMap) {
protectionMap = protectionMap || [];
// Hide escaped escapes momentarily
src = src.replace(/\\\\/g, ESCAPED_ESCAPE);
// Hide escaped quotes that would confuse the main regexp
src = src.replace(/\\"/g, doubleQuoteProtection);
// Restore escaped escapes
src = src.replace(/\uE009/g, '\\\\');
// TODO: i = 97 string is being replaced with "if"
// Protect all strings
src = src.replace(/"((?:[^"\\]|\\.)*)"/g, function (match, str) {
const i = protectionMap.length;
protectionMap.push(str);
return '"' + String.fromCharCode(i + protectionBlockStart) + '"';
});
return [src, protectionMap];
},
unprotectQuotedStrings : function unprotectQuotedStrings(s, protectionMap) {
// Unprotect strings
for (let i = 0; i < protectionMap.length; ++i) {
s = s.replace(String.fromCharCode(i + protectionBlockStart), protectionMap[i]);
}
// Unprotect escaped quotes
return s.replaceAll(doubleQuoteProtection, '\\"');
},
stringify: function stringify(value, replacer, space) {
// Allow IEEE numeric constants by temporarily hiding them in strings
function betterReplacer(key, value) {
if (replacer) { value = replacer(key, value); }
if (typeof value === 'undefined') { return UndefinedSymbol; }
if ((typeof value === 'number') && isNaN(value)) { return NaNSymbol; }
switch (value) {
case Infinity: return InfinitySymbol;
case -Infinity: return NegInfinitySymbol;
default: return value;
}
}
// Restore the constants
let json = JSON.stringify(value, betterReplacer, space);
json = json.replace(/"\uE001"/g, 'NaN').replace(/"\uE002"/g, 'Infinity').replace(/"\uE003"/g, '-Infinity').replace(/"\uE004"/g, 'undefined');
return json;
},
parse: function parse(text, reviver) {
function betterReviver(key, value) {
// Restore the temporarily hidden IEEE 754 values
switch (value) {
case NaNSymbol: value = NaN; break;
case InfinitySymbol: value = Infinity; break;
case NegInfinitySymbol: value = -Infinity; break;
case UndefinedSymbol: value = undefined; break;
}
if (reviver) { value = reviver(key, value); }
return value;
}
// Add leading and trailing whitespace so that \b can detect
// breaks at the start and end of the string.
text = ' ' + text + ' ';
// Protect strings
let protect = WorkJSON.protectQuotedStrings(text);
text = protect[0];
// Convert to strict Unix newlines
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
// Non-escaped empty backquote strings
text = text.replace(/(^|[^\\])``/g, "");
// Convert multiline backquote strings to singleline
text = text.replace(/`((?:\s|\S)*?[^\\])`/g, function (match, inside) {
return '"' + inside.replace(/\n/g, '\\n').replace(/\\`/g, '`').replace(/"/g, '\\"') + '"';
});
// Re-protect, hiding the newly converted strings
protect = WorkJSON.protectQuotedStrings(WorkJSON.unprotectQuotedStrings(text, protect[1]));
text = protect[0];
// Remove multiline comments, preserving newlines
text = text.replace(/\/\*(.|\n)*\*\//g, function (match) {
return match.replace(/[^\n]/g, '');
});
// Remove singleline comments
text = text.replace(/\/\/.*\n/g, '\n');
// Remove trailing commas
text = text.replace(/,(\s*[\]}\)])/g, '$1');
// Remove leading + signs on numbers, without catching
// exponential notation
text = text.replace(/([\[:\s,])\+\s*(?=\.?\d)/g, '$1');
// Hex constants
text = text.replace(/0x[\da-fA-F]{1,8}/g, function (hex) {
return '' + parseInt(hex);
});
// Numbers with leading '.'
text = text.replace(/([\[:\s,])\.(?=\d)/g, '0.');
// Numbers with trailing '.'
text = text.replace(/(\d)\.([\]}\s,])/g, '$1$2');
// Hide IEEE constants
text = text.replace(/\bNaN\b/g, '"' + NaNSymbol + '"').
replace(/\b-\s*Infinity\b/g, '"' + NegInfinitySymbol + '"').
replace(/\bInfinity\b/g, '"' + InfinitySymbol + '"');
// Quote unquoted keys
text = text.replace(/([A-Za-z_][A-Za-z_0-9]*)(?=\s*:)/g, '"$1"');
// Restore strings
text = unprotectQuotedStrings(text, protect[1]);
return JSON.parse(text, betterReviver);
} // parse
}; // WorkJSON object
})();