| 1 | var _fs |
| 2 | try { |
| 3 | _fs = require('graceful-fs') |
| 4 | } catch (_) { |
| 5 | _fs = require('fs') |
| 6 | } |
| 7 | |
| 8 | function readFile (file, options, callback) { |
| 9 | if (callback == null) { |
| 10 | callback = options |
| 11 | options = {} |
| 12 | } |
| 13 | |
| 14 | if (typeof options === 'string') { |
| 15 | options = {encoding: options} |
| 16 | } |
| 17 | |
| 18 | options = options || {} |
| 19 | var fs = options.fs || _fs |
| 20 | |
| 21 | var shouldThrow = true |
| 22 | // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead |
| 23 | if ('passParsingErrors' in options) { |
| 24 | shouldThrow = options.passParsingErrors |
| 25 | } else if ('throws' in options) { |
| 26 | shouldThrow = options.throws |
| 27 | } |
| 28 | |
| 29 | fs.readFile(file, options, function (err, data) { |
| 30 | if (err) return callback(err) |
| 31 | |
| 32 | data = stripBom(data) |
| 33 | |
| 34 | var obj |
| 35 | try { |
| 36 | obj = JSON.parse(data, options ? options.reviver : null) |
| 37 | } catch (err2) { |
| 38 | if (shouldThrow) { |
| 39 | err2.message = file + ': ' + err2.message |
| 40 | return callback(err2) |
| 41 | } else { |
| 42 | return callback(null, null) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | callback(null, obj) |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | function readFileSync (file, options) { |
| 51 | options = options || {} |
| 52 | if (typeof options === 'string') { |
| 53 | options = {encoding: options} |
| 54 | } |
| 55 | |
| 56 | var fs = options.fs || _fs |
| 57 | |
| 58 | var shouldThrow = true |
| 59 | // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead |
| 60 | if ('passParsingErrors' in options) { |
| 61 | shouldThrow = options.passParsingErrors |
| 62 | } else if ('throws' in options) { |
| 63 | shouldThrow = options.throws |
| 64 | } |
| 65 | |
| 66 | var content = fs.readFileSync(file, options) |
| 67 | content = stripBom(content) |
| 68 | |
| 69 | try { |
| 70 | return JSON.parse(content, options.reviver) |
| 71 | } catch (err) { |
| 72 | if (shouldThrow) { |
| 73 | err.message = file + ': ' + err.message |
| 74 | throw err |
| 75 | } else { |
| 76 | return null |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | function writeFile (file, obj, options, callback) { |
| 82 | if (callback == null) { |
| 83 | callback = options |
| 84 | options = {} |
| 85 | } |
| 86 | options = options || {} |
| 87 | var fs = options.fs || _fs |
| 88 | |
| 89 | var spaces = typeof options === 'object' && options !== null |
| 90 | ? 'spaces' in options |
| 91 | ? options.spaces : this.spaces |
| 92 | : this.spaces |
| 93 | |
| 94 | var str = '' |
| 95 | try { |
| 96 | str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n' |
| 97 | } catch (err) { |
| 98 | if (callback) return callback(err, null) |
| 99 | } |
| 100 | |
| 101 | fs.writeFile(file, str, options, callback) |
| 102 | } |
| 103 | |
| 104 | function writeFileSync (file, obj, options) { |
| 105 | options = options || {} |
| 106 | var fs = options.fs || _fs |
| 107 | |
| 108 | var spaces = typeof options === 'object' && options !== null |
| 109 | ? 'spaces' in options |
| 110 | ? options.spaces : this.spaces |
| 111 | : this.spaces |
| 112 | |
| 113 | var str = JSON.stringify(obj, options.replacer, spaces) + '\n' |
| 114 | // not sure if fs.writeFileSync returns anything, but just in case |
| 115 | return fs.writeFileSync(file, str, options) |
| 116 | } |
| 117 | |
| 118 | function stripBom (content) { |
| 119 | // we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified |
| 120 | if (Buffer.isBuffer(content)) content = content.toString('utf8') |
| 121 | content = content.replace(/^\uFEFF/, '') |
| 122 | return content |
| 123 | } |
| 124 | |
| 125 | var jsonfile = { |
| 126 | spaces: null, |
| 127 | readFile: readFile, |
| 128 | readFileSync: readFileSync, |
| 129 | writeFile: writeFile, |
| 130 | writeFileSync: writeFileSync |
| 131 | } |
| 132 | |
| 133 | module.exports = jsonfile |