You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.2 KiB

3 months ago
  1. Node.js - jsonfile
  2. ================
  3. Easily read/write JSON files in Node.js. _Note: this module cannot be used in the browser._
  4. [![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile)
  5. [![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.svg)](http://travis-ci.org/jprichardson/node-jsonfile)
  6. [![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-jsonfile/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-jsonfile/branch/master)
  7. <a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>
  8. Why?
  9. ----
  10. Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.
  11. Installation
  12. ------------
  13. npm install --save jsonfile
  14. API
  15. ---
  16. * [`readFile(filename, [options], callback)`](#readfilefilename-options-callback)
  17. * [`readFileSync(filename, [options])`](#readfilesyncfilename-options)
  18. * [`writeFile(filename, obj, [options], callback)`](#writefilefilename-obj-options-callback)
  19. * [`writeFileSync(filename, obj, [options])`](#writefilesyncfilename-obj-options)
  20. ----
  21. ### readFile(filename, [options], callback)
  22. `options` (`object`, default `undefined`): Pass in any [`fs.readFile`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback) options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
  23. - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
  24. If `false`, returns `null` for the object.
  25. ```js
  26. const jsonfile = require('jsonfile')
  27. const file = '/tmp/data.json'
  28. jsonfile.readFile(file, function (err, obj) {
  29. if (err) console.error(err)
  30. console.dir(obj)
  31. })
  32. ```
  33. You can also use this method with promises. The `readFile` method will return a promise if you do not pass a callback function.
  34. ```js
  35. const jsonfile = require('jsonfile')
  36. const file = '/tmp/data.json'
  37. jsonfile.readFile(file)
  38. .then(obj => console.dir(obj))
  39. .catch(error => console.error(error))
  40. ```
  41. ----
  42. ### readFileSync(filename, [options])
  43. `options` (`object`, default `undefined`): Pass in any [`fs.readFileSync`](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options) options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
  44. - `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
  45. ```js
  46. const jsonfile = require('jsonfile')
  47. const file = '/tmp/data.json'
  48. console.dir(jsonfile.readFileSync(file))
  49. ```
  50. ----
  51. ### writeFile(filename, obj, [options], callback)
  52. `options`: Pass in any [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback) options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`, or override `EOL` string or set `finalEOL` flag as `false` to not save the file with `EOL` at the end.
  53. ```js
  54. const jsonfile = require('jsonfile')
  55. const file = '/tmp/data.json'
  56. const obj = { name: 'JP' }
  57. jsonfile.writeFile(file, obj, function (err) {
  58. if (err) console.error(err)
  59. })
  60. ```
  61. Or use with promises as follows:
  62. ```js
  63. const jsonfile = require('jsonfile')
  64. const file = '/tmp/data.json'
  65. const obj = { name: 'JP' }
  66. jsonfile.writeFile(file, obj)
  67. .then(res => {
  68. console.log('Write complete')
  69. })
  70. .catch(error => console.error(error))
  71. ```
  72. **formatting with spaces:**
  73. ```js
  74. const jsonfile = require('jsonfile')
  75. const file = '/tmp/data.json'
  76. const obj = { name: 'JP' }
  77. jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
  78. if (err) console.error(err)
  79. })
  80. ```
  81. **overriding EOL:**
  82. ```js
  83. const jsonfile = require('jsonfile')
  84. const file = '/tmp/data.json'
  85. const obj = { name: 'JP' }
  86. jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  87. if (err) console.error(err)
  88. })
  89. ```
  90. **disabling the EOL at the end of file:**
  91. ```js
  92. const jsonfile = require('jsonfile')
  93. const file = '/tmp/data.json'
  94. const obj = { name: 'JP' }
  95. jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
  96. if (err) console.log(err)
  97. })
  98. ```
  99. **appending to an existing JSON file:**
  100. You can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.
  101. ```js
  102. const jsonfile = require('jsonfile')
  103. const file = '/tmp/mayAlreadyExistedData.json'
  104. const obj = { name: 'JP' }
  105. jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
  106. if (err) console.error(err)
  107. })
  108. ```
  109. ----
  110. ### writeFileSync(filename, obj, [options])
  111. `options`: Pass in any [`fs.writeFileSync`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options) options or set `replacer` for a [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces`, or override `EOL` string or set `finalEOL` flag as `false` to not save the file with `EOL` at the end.
  112. ```js
  113. const jsonfile = require('jsonfile')
  114. const file = '/tmp/data.json'
  115. const obj = { name: 'JP' }
  116. jsonfile.writeFileSync(file, obj)
  117. ```
  118. **formatting with spaces:**
  119. ```js
  120. const jsonfile = require('jsonfile')
  121. const file = '/tmp/data.json'
  122. const obj = { name: 'JP' }
  123. jsonfile.writeFileSync(file, obj, { spaces: 2 })
  124. ```
  125. **overriding EOL:**
  126. ```js
  127. const jsonfile = require('jsonfile')
  128. const file = '/tmp/data.json'
  129. const obj = { name: 'JP' }
  130. jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
  131. ```
  132. **disabling the EOL at the end of file:**
  133. ```js
  134. const jsonfile = require('jsonfile')
  135. const file = '/tmp/data.json'
  136. const obj = { name: 'JP' }
  137. jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })
  138. ```
  139. **appending to an existing JSON file:**
  140. You can use `fs.writeFileSync` option `{ flag: 'a' }` to achieve this.
  141. ```js
  142. const jsonfile = require('jsonfile')
  143. const file = '/tmp/mayAlreadyExistedData.json'
  144. const obj = { name: 'JP' }
  145. jsonfile.writeFileSync(file, obj, { flag: 'a' })
  146. ```
  147. License
  148. -------
  149. (MIT License)
  150. Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>