Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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`.
  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. **appending to an existing JSON file:**
  55. You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
  56. ```js
  57. var jsonfile = require('jsonfile')
  58. var file = '/tmp/mayAlreadyExistedData.json'
  59. var obj = {name: 'JP'}
  60. jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
  61. console.error(err)
  62. })
  63. ```
  64. ### writeFileSync(filename, obj, [options])
  65. `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`.
  66. ```js
  67. var jsonfile = require('jsonfile')
  68. var file = '/tmp/data.json'
  69. var obj = {name: 'JP'}
  70. jsonfile.writeFileSync(file, obj)
  71. ```
  72. **formatting with spaces:**
  73. ```js
  74. var jsonfile = require('jsonfile')
  75. var file = '/tmp/data.json'
  76. var obj = {name: 'JP'}
  77. jsonfile.writeFileSync(file, obj, {spaces: 2})
  78. ```
  79. **appending to an existing JSON file:**
  80. You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
  81. ```js
  82. var jsonfile = require('jsonfile')
  83. var file = '/tmp/mayAlreadyExistedData.json'
  84. var obj = {name: 'JP'}
  85. jsonfile.writeFileSync(file, obj, {flag: 'a'})
  86. ```
  87. ### spaces
  88. Global configuration to set spaces to indent JSON files.
  89. **default:** `null`
  90. ```js
  91. var jsonfile = require('jsonfile')
  92. jsonfile.spaces = 4
  93. var file = '/tmp/data.json'
  94. var obj = {name: 'JP'}
  95. // json file has four space indenting now
  96. jsonfile.writeFile(file, obj, function (err) {
  97. console.error(err)
  98. })
  99. ```
  100. Note, it's bound to `this.spaces`. So, if you do this:
  101. ```js
  102. var myObj = {}
  103. myObj.writeJsonSync = jsonfile.writeFileSync
  104. // => this.spaces = null
  105. ```
  106. Could do the following:
  107. ```js
  108. var jsonfile = require('jsonfile')
  109. jsonfile.spaces = 4
  110. jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
  111. var myCrazyObj = {spaces: 32}
  112. myCrazyObj.writeJsonSync = jsonfile.writeFileSync
  113. myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
  114. myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
  115. ```
  116. License
  117. -------
  118. (MIT License)
  119. Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>