[6a3a178] | 1 | Node.js - jsonfile
|
---|
| 2 | ================
|
---|
| 3 |
|
---|
| 4 | Easily read/write JSON files.
|
---|
| 5 |
|
---|
| 6 | [![npm Package](https://img.shields.io/npm/v/jsonfile.svg?style=flat-square)](https://www.npmjs.org/package/jsonfile)
|
---|
| 7 | [![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.svg)](http://travis-ci.org/jprichardson/node-jsonfile)
|
---|
| 8 | [![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)
|
---|
| 9 |
|
---|
| 10 | <a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>
|
---|
| 11 |
|
---|
| 12 | Why?
|
---|
| 13 | ----
|
---|
| 14 |
|
---|
| 15 | Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | Installation
|
---|
| 20 | ------------
|
---|
| 21 |
|
---|
| 22 | npm install --save jsonfile
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | API
|
---|
| 27 | ---
|
---|
| 28 |
|
---|
| 29 | ### readFile(filename, [options], callback)
|
---|
| 30 |
|
---|
| 31 | `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).
|
---|
| 32 | - `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
|
---|
| 33 | If `false`, returns `null` for the object.
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | ```js
|
---|
| 37 | var jsonfile = require('jsonfile')
|
---|
| 38 | var file = '/tmp/data.json'
|
---|
| 39 | jsonfile.readFile(file, function(err, obj) {
|
---|
| 40 | console.dir(obj)
|
---|
| 41 | })
|
---|
| 42 | ```
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | ### readFileSync(filename, [options])
|
---|
| 46 |
|
---|
| 47 | `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).
|
---|
| 48 | - `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
|
---|
| 49 |
|
---|
| 50 | ```js
|
---|
| 51 | var jsonfile = require('jsonfile')
|
---|
| 52 | var file = '/tmp/data.json'
|
---|
| 53 |
|
---|
| 54 | console.dir(jsonfile.readFileSync(file))
|
---|
| 55 | ```
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | ### writeFile(filename, obj, [options], callback)
|
---|
| 59 |
|
---|
| 60 | `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.
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | ```js
|
---|
| 64 | var jsonfile = require('jsonfile')
|
---|
| 65 |
|
---|
| 66 | var file = '/tmp/data.json'
|
---|
| 67 | var obj = {name: 'JP'}
|
---|
| 68 |
|
---|
| 69 | jsonfile.writeFile(file, obj, function (err) {
|
---|
| 70 | console.error(err)
|
---|
| 71 | })
|
---|
| 72 | ```
|
---|
| 73 |
|
---|
| 74 | **formatting with spaces:**
|
---|
| 75 |
|
---|
| 76 | ```js
|
---|
| 77 | var jsonfile = require('jsonfile')
|
---|
| 78 |
|
---|
| 79 | var file = '/tmp/data.json'
|
---|
| 80 | var obj = {name: 'JP'}
|
---|
| 81 |
|
---|
| 82 | jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
|
---|
| 83 | console.error(err)
|
---|
| 84 | })
|
---|
| 85 | ```
|
---|
| 86 |
|
---|
| 87 | **overriding EOL:**
|
---|
| 88 |
|
---|
| 89 | ```js
|
---|
| 90 | var jsonfile = require('jsonfile')
|
---|
| 91 |
|
---|
| 92 | var file = '/tmp/data.json'
|
---|
| 93 | var obj = {name: 'JP'}
|
---|
| 94 |
|
---|
| 95 | jsonfile.writeFile(file, obj, {spaces: 2, EOL: '\r\n'}, function(err) {
|
---|
| 96 | console.error(err)
|
---|
| 97 | })
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | **appending to an existing JSON file:**
|
---|
| 101 |
|
---|
| 102 | You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
|
---|
| 103 |
|
---|
| 104 | ```js
|
---|
| 105 | var jsonfile = require('jsonfile')
|
---|
| 106 |
|
---|
| 107 | var file = '/tmp/mayAlreadyExistedData.json'
|
---|
| 108 | var obj = {name: 'JP'}
|
---|
| 109 |
|
---|
| 110 | jsonfile.writeFile(file, obj, {flag: 'a'}, function (err) {
|
---|
| 111 | console.error(err)
|
---|
| 112 | })
|
---|
| 113 | ```
|
---|
| 114 |
|
---|
| 115 | ### writeFileSync(filename, obj, [options])
|
---|
| 116 |
|
---|
| 117 | `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.
|
---|
| 118 |
|
---|
| 119 | ```js
|
---|
| 120 | var jsonfile = require('jsonfile')
|
---|
| 121 |
|
---|
| 122 | var file = '/tmp/data.json'
|
---|
| 123 | var obj = {name: 'JP'}
|
---|
| 124 |
|
---|
| 125 | jsonfile.writeFileSync(file, obj)
|
---|
| 126 | ```
|
---|
| 127 |
|
---|
| 128 | **formatting with spaces:**
|
---|
| 129 |
|
---|
| 130 | ```js
|
---|
| 131 | var jsonfile = require('jsonfile')
|
---|
| 132 |
|
---|
| 133 | var file = '/tmp/data.json'
|
---|
| 134 | var obj = {name: 'JP'}
|
---|
| 135 |
|
---|
| 136 | jsonfile.writeFileSync(file, obj, {spaces: 2})
|
---|
| 137 | ```
|
---|
| 138 |
|
---|
| 139 | **overriding EOL:**
|
---|
| 140 |
|
---|
| 141 | ```js
|
---|
| 142 | var jsonfile = require('jsonfile')
|
---|
| 143 |
|
---|
| 144 | var file = '/tmp/data.json'
|
---|
| 145 | var obj = {name: 'JP'}
|
---|
| 146 |
|
---|
| 147 | jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
|
---|
| 148 | ```
|
---|
| 149 |
|
---|
| 150 | **appending to an existing JSON file:**
|
---|
| 151 |
|
---|
| 152 | You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
|
---|
| 153 |
|
---|
| 154 | ```js
|
---|
| 155 | var jsonfile = require('jsonfile')
|
---|
| 156 |
|
---|
| 157 | var file = '/tmp/mayAlreadyExistedData.json'
|
---|
| 158 | var obj = {name: 'JP'}
|
---|
| 159 |
|
---|
| 160 | jsonfile.writeFileSync(file, obj, {flag: 'a'})
|
---|
| 161 | ```
|
---|
| 162 |
|
---|
| 163 | License
|
---|
| 164 | -------
|
---|
| 165 |
|
---|
| 166 | (MIT License)
|
---|
| 167 |
|
---|
| 168 | Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>
|
---|