mirror of
https://github.com/owncast/owncast.git
synced 2024-10-10 19:16:02 +00:00
![dependabot[bot]](/assets/img/avatar_default.png)
* Commit updated Javascript packages * Bump preact from 10.5.4 to 10.5.5 in /build/javascript (#265) * Trying a new github workflow to install javascript packages * Bump tailwindcss from 1.9.2 to 1.9.4 in /build/javascript (#266) Bumps [tailwindcss](https://github.com/tailwindlabs/tailwindcss) from 1.9.2 to 1.9.4. - [Release notes](https://github.com/tailwindlabs/tailwindcss/releases) - [Changelog](https://github.com/tailwindlabs/tailwindcss/blob/master/CHANGELOG.md) - [Commits](https://github.com/tailwindlabs/tailwindcss/compare/v1.9.2...v1.9.4) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Commit updated Javascript packages * Bump preact from 10.5.4 to 10.5.5 in /build/javascript Bumps [preact](https://github.com/preactjs/preact) from 10.5.4 to 10.5.5. - [Release notes](https://github.com/preactjs/preact/releases) - [Commits](https://github.com/preactjs/preact/compare/10.5.4...10.5.5) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Gabe Kangas <gabek@real-ity.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Owncast <owncast@owncast.online> * Bump @justinribeiro/lite-youtube in /build/javascript Bumps [@justinribeiro/lite-youtube](https://github.com/justinribeiro/lite-youtube) from 0.9.0 to 0.9.1. - [Release notes](https://github.com/justinribeiro/lite-youtube/releases) - [Commits](https://github.com/justinribeiro/lite-youtube/commits) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Owncast <owncast@owncast.online> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Gabe Kangas <gabek@real-ity.com>
194 lines
5.0 KiB
Markdown
194 lines
5.0 KiB
Markdown
Node.js - jsonfile
|
|
================
|
|
|
|
Easily read/write JSON files in Node.js. _Note: this module cannot be used in the browser._
|
|
|
|
[](https://www.npmjs.org/package/jsonfile)
|
|
[](http://travis-ci.org/jprichardson/node-jsonfile)
|
|
[](https://ci.appveyor.com/project/jprichardson/node-jsonfile/branch/master)
|
|
|
|
<a href="https://github.com/feross/standard"><img src="https://cdn.rawgit.com/feross/standard/master/sticker.svg" alt="Standard JavaScript" width="100"></a>
|
|
|
|
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).
|
|
- `throws` (`boolean`, default: `true`). If `JSON.parse` throws an error, pass this error to the callback.
|
|
If `false`, returns `null` for the object.
|
|
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
const file = '/tmp/data.json'
|
|
jsonfile.readFile(file, function (err, obj) {
|
|
if (err) console.error(err)
|
|
console.dir(obj)
|
|
})
|
|
```
|
|
|
|
You can also use this method with promises. The `readFile` method will return a promise if you do not pass a callback function.
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
const file = '/tmp/data.json'
|
|
jsonfile.readFile(file)
|
|
.then(obj => console.dir(obj))
|
|
.catch(error => console.error(error))
|
|
```
|
|
|
|
### readFileSync(filename, [options])
|
|
|
|
`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).
|
|
- `throws` (`boolean`, default: `true`). If an error is encountered reading or parsing the file, throw the error. If `false`, returns `null` for the object.
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
const 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Can also pass in `spaces` and override `EOL` string.
|
|
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFile(file, obj, function (err) {
|
|
if (err) console.error(err)
|
|
})
|
|
```
|
|
Or use with promises as follows:
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFile(file, obj)
|
|
.then(res => {
|
|
console.log('Write complete')
|
|
})
|
|
.catch(error => console.error(error))
|
|
```
|
|
|
|
|
|
**formatting with spaces:**
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
|
|
if (err) console.error(err)
|
|
})
|
|
```
|
|
|
|
**overriding EOL:**
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
|
|
if (err) console.error(err)
|
|
})
|
|
```
|
|
|
|
**appending to an existing JSON file:**
|
|
|
|
You can use `fs.writeFile` option `{ flag: 'a' }` to achieve this.
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/mayAlreadyExistedData.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
|
|
if (err) console.error(err)
|
|
})
|
|
```
|
|
|
|
### writeFileSync(filename, obj, [options])
|
|
|
|
`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.
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFileSync(file, obj)
|
|
```
|
|
|
|
**formatting with spaces:**
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFileSync(file, obj, { spaces: 2 })
|
|
```
|
|
|
|
**overriding EOL:**
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/data.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
|
|
```
|
|
|
|
**appending to an existing JSON file:**
|
|
|
|
You can use `fs.writeFileSync` option `{ flag: 'a' }` to achieve this.
|
|
|
|
```js
|
|
const jsonfile = require('jsonfile')
|
|
|
|
const file = '/tmp/mayAlreadyExistedData.json'
|
|
const obj = { name: 'JP' }
|
|
|
|
jsonfile.writeFileSync(file, obj, { flag: 'a' })
|
|
```
|
|
|
|
License
|
|
-------
|
|
|
|
(MIT License)
|
|
|
|
Copyright 2012-2016, JP Richardson <jprichardson@gmail.com>
|