This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json-parser.js
236 lines (225 loc) · 6.84 KB
/
json-parser.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const SafeHtmlUtils = {
AMP_RE: new RegExp(/&/g),
GT_RE: new RegExp(/>/g),
LT_RE: new RegExp(/</g),
SQUOT_RE: new RegExp(/'/g),
QUOT_RE: new RegExp(/"/g),
htmlEscape: function(s) {
if (s.indexOf('&') !== -1) {
s = s.replace(SafeHtmlUtils.AMP_RE, '&');
}
if (s.indexOf('<') !== -1) {
s = s.replace(SafeHtmlUtils.LT_RE, '<');
}
if (s.indexOf('>') !== -1) {
s = s.replace(SafeHtmlUtils.GT_RE, '>');
}
if (s.indexOf('"') !== -1) {
s = s.replace(SafeHtmlUtils.QUOT_RE, '"');
}
if (s.indexOf('\'') !== -1) {
s = s.replace(SafeHtmlUtils.SQUOT_RE, ''');
}
return s;
}
};
export class JsonParser {
constructor(opts) {
this.rawData = opts.raw || '';
this.cssPrefix = opts.cssPrefix || '';
this._numberIndexes = {}; // Regexp number indexes
this.jsonValue = null;
this.latestError = null;
this.elementsCounter = 0;
this._setJson(opts.json);
this.hasPerformanceApi = typeof performance !== 'undefined';
}
_setJson(jsonData) {
if (typeof jsonData === 'string') {
try {
this.jsonValue = JSON.parse(jsonData);
if (!this.rawData) {
this.rawData = jsonData;
}
} catch (e) {
this.latestError = e.message;
}
} else {
this.jsonValue = jsonData;
}
}
/**
* Get created HTML content.
* @return {String}
*/
getHTML() {
let parsedData = '<div class="' + this.cssPrefix + 'prettyPrint">';
parsedData += this.parse(this.jsonValue);
parsedData += '</div>';
return parsedData;
}
/**
* Parse JSON data
* @param {*} data
* @param {Object} opts
* @return {string}
*/
parse(data, opts) {
opts = opts || {};
let result = '';
if (data === null) {
result += this.parseNullValue();
} else if (typeof data === 'number') {
result += this.parseNumericValue(data);
} else if (typeof data === 'boolean') {
result += this.parseBooleanValue(data);
} else if (typeof data === 'string') {
result += this.parseStringValue(data);
} else if (data instanceof Array) {
result += this.parseArray(data);
} else {
result += this.parseObject(data);
}
if (opts.hasNextSibling && !opts.holdComa) {
result += '<span class="' + this.cssPrefix + 'punctuation dimmed">,</span>';
}
return result;
}
parseNullValue() {
let result = '';
result += '<span class="' + this.cssPrefix + 'nullValue">';
result += 'null';
result += '</span>';
return result;
}
parseNumericValue(number) {
let expectedNumber;
let max = Number.MAX_SAFE_INTEGER;
if (!max) {
max = 9007199254740991;
}
if (number > max) { // IE doesn't support Number.MAX_SAFE_INTEGER
let comp = String(number);
comp = comp.substr(0, String(max).length);
const r = new RegExp(comp + '(\\d+),?', 'gim');
if (comp in this._numberIndexes) {
r.lastIndex = this._numberIndexes[comp];
}
const _result = r.exec(this.rawData);
if (_result) {
this._numberIndexes[comp] = _result.index;
expectedNumber = comp + _result[1];
}
}
let result = '';
result += '<span class="' + this.cssPrefix + 'numeric">';
if (expectedNumber) {
result += '<js-max-number-error class="' + this.cssPrefix +
'number-error" expectednumber="' + expectedNumber + '">';
}
result += number + '';
if (expectedNumber) {
result += '</js-max-number-error>';
}
result += '</span>';
return result;
}
parseBooleanValue(bool) {
let result = '';
result += '<span class="' + this.cssPrefix + 'booleanValue">';
if (bool !== null && bool !== undefined) {
result += bool + '';
} else {
result += 'null';
}
result += '</span>';
return result;
}
parseStringValue(str) {
let result = '';
let value = str || '';
if (value !== null && value !== undefined) {
value = SafeHtmlUtils.htmlEscape(value);
if (value.slice(0, 1) === '/' || value.substr(0, 4) === 'http') {
value = '<a class="' + this.cssPrefix + '" title="Click to insert into URL field" ' +
'response-anchor add-root-url href="' + value + '">' + value + '</a>';
}
} else {
value = 'null';
}
result += '"';
result += '<span class="' + this.cssPrefix + 'stringValue">';
result += value;
result += '</span>';
result += '"';
return result;
}
parseObject(object) {
let result = '';
result += '{';
result += '<div collapse-indicator class="' + this.cssPrefix + 'info-row">...</div>';
Object.keys(object).forEach((key, i, arr) => {
const value = object[key];
const lastSibling = (i + 1) === arr.length;
const parseOpts = {
hasNextSibling: !lastSibling
};
if (value instanceof Array) {
parseOpts.holdComa = true;
}
const elementNo = this.elementsCounter++;
const data = this.parse(value, parseOpts);
const hasManyChildren = this.elementsCounter - elementNo > 1;
result += '<div data-element="' + elementNo + '" style="margin-left: 24px" class="' +
this.cssPrefix + 'node">';
const _nan = isNaN(/** @type any */ (key));
if (_nan) {
result += '"';
}
result += this.parseKey(key);
if (_nan) {
result += '"';
}
result += ': ' + data;
if (hasManyChildren) {
result += '<div data-toggle="' + elementNo + '" class="' + this.cssPrefix +
'rootElementToggleButton"></div>';
}
result += '</div>';
});
result += '}';
return result;
}
parseArray(array) {
const cnt = array.length;
let result = '';
result += '<span class="' + this.cssPrefix + 'punctuation dimmed">[</span>';
result += '<span class="' + this.cssPrefix + 'array-counter brace punctuation" count="' +
cnt + '"></span>';
for (let i = 0; i < cnt; i++) {
const elementNo = this.elementsCounter++;
const lastSibling = (i + 1) === cnt;
const data = this.parse(array[i], {
hasNextSibling: !lastSibling
});
const hasManyChildren = this.elementsCounter - elementNo > 1;
result += '<div data-element="' + elementNo +
'" style="margin-left: 24px" class="' + this.cssPrefix + 'node">';
result += '<span class="' + this.cssPrefix + 'array-key-number" index="' + i +
'"> </span>';
result += data;
if (hasManyChildren) {
result += '<div data-toggle="' + elementNo + '" class="' + this.cssPrefix +
'rootElementToggleButton"></div>';
}
result += '</div>';
}
result += '<span class="' + this.cssPrefix + 'punctuation dimmed">],</span>';
return result;
}
parseKey(key) {
let result = '';
result += '<span class="' + this.cssPrefix + 'key-name">' + key + '</span>';
return result;
}
}