utils.js
1.23 KB
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
var FS = require('fs')
var jju = require('../')
// this function registers json5 extension, so you
// can do `require("./config.json5")` kind of thing
module.exports.register = function() {
var r = require, e = 'extensions'
r[e]['.json5'] = function(m, f) {
/*eslint no-sync:0*/
m.exports = jju.parse(FS.readFileSync(f, 'utf8'))
}
}
// this function monkey-patches JSON.parse, so it
// will return an exact position of error in case
// of parse failure
module.exports.patch_JSON_parse = function() {
var _parse = JSON.parse
JSON.parse = function(text, rev) {
try {
return _parse(text, rev)
} catch(err) {
// this call should always throw
require('jju').parse(text, {
mode: 'json',
legacy: true,
reviver: rev,
reserved_keys: 'replace',
null_prototype: false,
})
// if it didn't throw, but original parser did,
// this is an error in this library and should be reported
throw err
}
}
}
// this function is an express/connect middleware
// that accepts uploads in application/json5 format
module.exports.middleware = function() {
return function(req, res, next) {
throw Error('this function is removed, use express-json5 instead')
}
}