2020-03-07 11:45:40 +08:00
|
|
|
'use strict';
|
2020-03-31 20:40:00 +08:00
|
|
|
var errorEx = require('error-ex');
|
|
|
|
var fallback = require('./vendor/parse');
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
var JSONError = errorEx('JSONError', {
|
2020-03-07 11:45:40 +08:00
|
|
|
fileName: errorEx.append('in %s')
|
|
|
|
});
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
module.exports = function (x, reviver, filename) {
|
2020-03-07 11:45:40 +08:00
|
|
|
if (typeof reviver === 'string') {
|
|
|
|
filename = reviver;
|
|
|
|
reviver = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
2020-03-31 20:40:00 +08:00
|
|
|
return JSON.parse(x, reviver);
|
2020-03-07 11:45:40 +08:00
|
|
|
} catch (err) {
|
2020-03-31 20:40:00 +08:00
|
|
|
fallback.parse(x, {
|
|
|
|
mode: 'json',
|
|
|
|
reviver: reviver
|
|
|
|
});
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2020-03-31 20:40:00 +08:00
|
|
|
var jsonErr = new JSONError(err);
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
jsonErr.fileName = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw jsonErr;
|
|
|
|
}
|
|
|
|
};
|