-
Notifications
You must be signed in to change notification settings - Fork 14
/
json.js
42 lines (38 loc) · 942 Bytes
/
json.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
/*jslint node: true */
var fs = require('fs');
exports.readFile = function(filepath, callback) {
/** Read object from the specified (JSON formatted) file and callback with an object.
callback: function(Error | null, Object | null)
*/
var expanded_filepath = filepath.replace(/^~/, process.env.HOME);
fs.readFile(expanded_filepath, {encoding: 'utf8'}, function(err, data) {
if (err) return callback(err);
try {
callback(null, JSON.parse(data));
}
catch (exc) {
callback(exc);
}
});
};
exports.parse = function(json_string) {
try {
return JSON.parse(json_string);
}
catch (exception) {
return exception;
}
};
exports.parseAsync = function(json_string, callback) {
try {
var result = JSON.parse(json_string);
setImmediate(function() {
callback(null, result);
});
}
catch (exception) {
setImmediate(function() {
callback(exception);
});
}
};