BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / jsonfile / README.md

README.md

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
README.md 4.0 KiB Download Raw

Node.js - jsonfile ================

Easily read/write JSON files.

npm Package build status windows Build status

Standard JavaScript

Why? ----

Writing JSON.stringify() and then fs.writeFile() and JSON.parse() with fs.readFile() enclosed in try/catch blocks became annoying.

Installation ------------

npm install --save jsonfile

API ---

readFile(filename, [options], callback)

options (object, default undefined): Pass in any fs.readFile options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, pass this error to the callback.

If false, returns null for the object.

var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
jsonfile.readFile(file, function(err, obj) {
  console.dir(obj)
})

readFileSync(filename, [options])

options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, throw the error.

If false, returns null for the object.

var jsonfile = require('jsonfile')
var file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))

writeFile(filename, obj, [options], callback)

options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces.

var jsonfile = require('jsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, function (err) {
  console.error(err)
})

formatting with spaces:

var jsonfile = require('jsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
  console.error(err)
})

writeFileSync(filename, obj, [options])

options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces.

var jsonfile = require('jsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFileSync(file, obj)

formatting with spaces:

var jsonfile = require('jsonfile')

var file = '/tmp/data.json'
var obj = {name: 'JP'}

jsonfile.writeFileSync(file, obj, {spaces: 2})

spaces

Global configuration to set spaces to indent JSON files.

default: null

var jsonfile = require('jsonfile')

jsonfile.spaces = 4

var file = '/tmp/data.json'
var obj = {name: 'JP'}

// json file has four space indenting now
jsonfile.writeFile(file, obj, function (err) {
  console.error(err)
})

Note, it's bound to this.spaces. So, if you do this:

var myObj = {}
myObj.writeJsonSync = jsonfile.writeFileSync
// => this.spaces = null

Could do the following:

var jsonfile = require('jsonfile')
jsonfile.spaces = 4
jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation

var myCrazyObj = {spaces: 32}
myCrazyObj.writeJsonSync = jsonfile.writeFileSync
myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2

License -------

(MIT License)

Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>

Source
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 `JSON.parse` throws an error, throw the error.
49If `false`, returns `null` for the object.
50
51```js
52var jsonfile = require('jsonfile')
53var file = '/tmp/data.json'
54
55console.dir(jsonfile.readFileSync(file))
56```
57
58
59### writeFile(filename, obj, [options], callback)
60
61`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`.
62
63
64```js
65var jsonfile = require('jsonfile')
66
67var file = '/tmp/data.json'
68var obj = {name: 'JP'}
69
70jsonfile.writeFile(file, obj, function (err) {
71 console.error(err)
72})
73```
74
75**formatting with spaces:**
76
77```js
78var jsonfile = require('jsonfile')
79
80var file = '/tmp/data.json'
81var obj = {name: 'JP'}
82
83jsonfile.writeFile(file, obj, {spaces: 2}, function(err) {
84 console.error(err)
85})
86```
87
88
89### writeFileSync(filename, obj, [options])
90
91`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`.
92
93```js
94var jsonfile = require('jsonfile')
95
96var file = '/tmp/data.json'
97var obj = {name: 'JP'}
98
99jsonfile.writeFileSync(file, obj)
100```
101
102**formatting with spaces:**
103
104```js
105var jsonfile = require('jsonfile')
106
107var file = '/tmp/data.json'
108var obj = {name: 'JP'}
109
110jsonfile.writeFileSync(file, obj, {spaces: 2})
111```
112
113
114
115### spaces
116
117Global configuration to set spaces to indent JSON files.
118
119**default:** `null`
120
121```js
122var jsonfile = require('jsonfile')
123
124jsonfile.spaces = 4
125
126var file = '/tmp/data.json'
127var obj = {name: 'JP'}
128
129// json file has four space indenting now
130jsonfile.writeFile(file, obj, function (err) {
131 console.error(err)
132})
133```
134
135Note, it's bound to `this.spaces`. So, if you do this:
136
137```js
138var myObj = {}
139myObj.writeJsonSync = jsonfile.writeFileSync
140// => this.spaces = null
141```
142
143Could do the following:
144
145```js
146var jsonfile = require('jsonfile')
147jsonfile.spaces = 4
148jsonfile.writeFileSync(file, obj) // will have 4 spaces indentation
149
150var myCrazyObj = {spaces: 32}
151myCrazyObj.writeJsonSync = jsonfile.writeFileSync
152myCrazyObj.writeJsonSync(file, obj) // will have 32 space indentation
153myCrazyObj.writeJsonSync(file, obj, {spaces: 2}) // will have only 2
154```
155
156
157License
158-------
159
160(MIT License)
161
162Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>