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.

168 lines
4.2 KiB

4 years ago
  1. Node.js - jsonfile
  2. ================
  3. Easily read/write JSON files.
  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)
  17. `options` (`object`, default `undefined`): Pass in any `fs.readFile` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
  18. - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
  19. If `false`, returns `null` for the object.
  20. ```js
  21. var jsonfile = require('jsonfile')
  22. var file = '/tmp/data.json'
  23. jsonfile.readFile(file, function(err, obj) {
  24. console.dir(obj)
  25. })
  26. ```
  27. ### readFileSync(filename, [options])
  28. `options` (`object`, default `undefined`): Pass in any `fs.readFileSync` options or set `reviver` for a [JSON reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
  29. - `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
  30. ```js
  31. var jsonfile = require('jsonfile')
  32. var file = '/tmp/data.json'
  33. console.dir(jsonfile.readFileSync(file))
  34. ```
  35. ### writeFile(filename, obj, [options], callback)
  36. `options`: Pass in any `fs.writeFile` 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` and override `EOL` string.
  37. ```js
  38. var jsonfile = require('jsonfile')
  39. var file = '/tmp/data.json'
  40. var obj = {name: 'JP'}
  41. jsonfile.writeFile(file, obj, function (err) {
  42. console.error(err)
  43. })
  44. ```
  45. **formatting with spaces:**
  46. ```js
  47. var jsonfile = require('jsonfile')
  48. var file = '/tmp/data.json'
  49. var obj = {name: 'JP'}
  50. jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
  51. console.error(err)
  52. })
  53. ```
  54. **overriding EOL:**
  55. ```js
  56. var jsonfile = require('jsonfile')
  57. var file = '/tmp/data.json'
  58. var obj = {name: 'JP'}
  59. jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
  60. console.error(err)
  61. })
  62. ```
  63. **appending to an existing JSON file:**
  64. You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
  65. ```js
  66. var jsonfile = require('jsonfile')
  67. var file = '/tmp/mayAlreadyExistedData.json'
  68. var obj = {name: 'JP'}
  69. jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
  70. console.error(err)
  71. })
  72. ```
  73. ### writeFileSync(filename, obj, [options])
  74. `options`: Pass in any `fs.writeFileSync` 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` and override `EOL` string.
  75. ```js
  76. var jsonfile = require('jsonfile')
  77. var file = '/tmp/data.json'
  78. var obj = {name: 'JP'}
  79. jsonfile.writeFileSync(file, obj)
  80. ```
  81. **formatting with spaces:**
  82. ```js
  83. var jsonfile = require('jsonfile')
  84. var file = '/tmp/data.json'
  85. var obj = {name: 'JP'}
  86. jsonfile.writeFileSync(file, obj, {spaces: 2})
  87. ```
  88. **overriding EOL:**
  89. ```js
  90. var jsonfile = require('jsonfile')
  91. var file = '/tmp/data.json'
  92. var obj = {name: 'JP'}
  93. jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
  94. ```
  95. **appending to an existing JSON file:**
  96. You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
  97. ```js
  98. var jsonfile = require('jsonfile')
  99. var file = '/tmp/mayAlreadyExistedData.json'
  100. var obj = {name: 'JP'}
  101. jsonfile.writeFileSync(file, obj, {flag: 'a'})
  102. ```
  103. License
  104. -------
  105. (MIT License)
  106. Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>