source: trip-planner-front/node_modules/jsonfile/README.md@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1Node.js - jsonfile
2================
3
4Easily 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
12Why?
13----
14
15Writing `JSON.stringify()` and then `fs.writeFile()` and `JSON.parse()` with `fs.readFile()` enclosed in `try/catch` blocks became annoying.
16
17
18
19Installation
20------------
21
22 npm install --save jsonfile
23
24
25
26API
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
37var jsonfile = require('jsonfile')
38var file = '/tmp/data.json'
39jsonfile.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
51var jsonfile = require('jsonfile')
52var file = '/tmp/data.json'
53
54console.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
64var jsonfile = require('jsonfile')
65
66var file = '/tmp/data.json'
67var obj = {name: 'JP'}
68
69jsonfile.writeFile(file, obj, function (err) {
70 console.error(err)
71})
72```
73
74**formatting with spaces:**
75
76```js
77var jsonfile = require('jsonfile')
78
79var file = '/tmp/data.json'
80var obj = {name: 'JP'}
81
82jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
83 console.error(err)
84})
85```
86
87**overriding EOL:**
88
89```js
90var jsonfile = require('jsonfile')
91
92var file = '/tmp/data.json'
93var obj = {name: 'JP'}
94
95jsonfile.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
102You can use `fs.writeFile` option `{flag: 'a'}` to achieve this.
103
104```js
105var jsonfile = require('jsonfile')
106
107var file = '/tmp/mayAlreadyExistedData.json'
108var obj = {name: 'JP'}
109
110jsonfile.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
120var jsonfile = require('jsonfile')
121
122var file = '/tmp/data.json'
123var obj = {name: 'JP'}
124
125jsonfile.writeFileSync(file, obj)
126```
127
128**formatting with spaces:**
129
130```js
131var jsonfile = require('jsonfile')
132
133var file = '/tmp/data.json'
134var obj = {name: 'JP'}
135
136jsonfile.writeFileSync(file, obj, {spaces: 2})
137```
138
139**overriding EOL:**
140
141```js
142var jsonfile = require('jsonfile')
143
144var file = '/tmp/data.json'
145var obj = {name: 'JP'}
146
147jsonfile.writeFileSync(file, obj, {spaces: 2, EOL: '\r\n'})
148```
149
150**appending to an existing JSON file:**
151
152You can use `fs.writeFileSync` option `{flag: 'a'}` to achieve this.
153
154```js
155var jsonfile = require('jsonfile')
156
157var file = '/tmp/mayAlreadyExistedData.json'
158var obj = {name: 'JP'}
159
160jsonfile.writeFileSync(file, obj, {flag: 'a'})
161```
162
163License
164-------
165
166(MIT License)
167
168Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>
Note: See TracBrowser for help on using the repository browser.