-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (166 loc) · 4.41 KB
/
index.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
const jsonic = require('jsonic');
const clarinet = require('clarinet');
const fixJson = (json, lastError) => {
const getJSONParseError = (json) => {
var parser = clarinet.parser(),
firstError = undefined;
// generate a detailed error using the parser's state
function makeError(e) {
var currentNL = 0,
nextNL = json.indexOf('\n'),
line = 1;
while (line < parser.line) {
currentNL = nextNL;
nextNL = json.indexOf('\n', currentNL + 1);
++line;
}
return {
snippet: json.substr(currentNL + 1, nextNL - currentNL - 1),
message: (e.message || '').split('\n', 1)[0],
line: parser.line,
column: parser.column,
};
}
// trigger the parse error
parser.onerror = function (e) {
firstError = makeError(e);
parser.close();
};
try {
parser.write(json).close();
} catch (e) {
if (firstError === undefined) {
return makeError(e);
} else {
return firstError;
}
}
return firstError;
};
const insertCharacter = (text, line, column, character) => {
let lines = text.split(/\r?\n/);
lines[line] =
lines[line].slice(0, column) + character + lines[line].slice(column);
return lines.join('\n');
};
const replacePairCharacter = (text, line, column, newChar, pairedChar) => {
let lines = text.split(/\r?\n/);
lines[line] =
lines[line].slice(0, column) +
newChar +
lines[line].slice(column + 1).replace(pairedChar, newChar);
return lines.join('\n');
};
const replaceCharacter = (text, line, newChar, oldChar) => {
let lines = text.split(/\r?\n/);
lines[line] = lines[line].replace(oldChar, newChar);
return lines.join('\n');
};
try {
// console.log('Validating', json);
// Execute first phase validator
const result = jsonic(json);
// Execute second phase validator (strict)
const e = getJSONParseError(json);
if (e && e.message === 'Bad value') {
if (
lastError &&
lastError.message === e.message &&
lastError.column === e.column &&
lastError.line === e.line
) {
// Bail out from recursive call, we cannot fix this error
throw e;
} else {
if (e.snippet.indexOf('\u201C') > -1) {
// try to fix error on validate again
return this.fixJson(
replaceCharacter(
replaceCharacter(json, e.line - 1, '"', '\u201C'),
e.line - 1,
'"',
'\u201D'
)
);
} else throw e;
}
}
// Valid json
else return json;
} catch (e) {
console.log('Error', e);
// Missing ":" error
if (e.message === 'Expected ":" but "\\"" found.') {
if (
lastError &&
lastError.message === e.message &&
lastError.column === e.column &&
lastError.line === e.line
) {
// Bail out from recursive call, we cannot fix this error
throw e;
}
// try to fix error on validate again
else
return this.fixJson(
insertCharacter(json, e.line - 1, e.column - 1, ':'),
e
);
}
// Left quote/right quote error
else if (
e.message === 'Expected ",", "}" or key but "\\u201C" found.' ||
e.message === 'Expected "}" or key but "\\u201C" found.'
) {
if (
lastError &&
lastError.message === e.message &&
lastError.column === e.column &&
lastError.line === e.line
) {
// Bail out from recursive call, we cannot fix this error
throw e;
}
// try to fix error on validate again
else
return this.fixJson(
replacePairCharacter(json, e.line - 1, e.column - 1, '"', '\u201D'),
e
);
} else throw e;
}
};
const addMissingCommas = (str) => {
const regex = /([\"a-zA-Z0-9]+)([: ]+)([\"][\w]+[\"]([\s\n\}\]]{1,}))+(['"\w])/g;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
// console.log("m.index = " + m.index);
// console.log("regex.lastIndex = " + regex.lastIndex);
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
if (groupIndex === 5) {
str =
str.slice(0, regex.lastIndex - 1) +
',' +
str.slice(regex.lastIndex - 1);
}
// console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
return str;
};
const parse = (data) => {
let parsed = data;
// add if any missing commas
parsed = addMissingCommas(parsed);
parsed = fixJson(parsed);
// console.log('Fixed json', parsed);
// fix the json as possible
parsed = jsonic(parsed);
return parsed;
};
module.exports = { parse };