Socket
Socket
Sign inDemoInstall

jsonfile

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonfile - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

6

CHANGELOG.md

@@ -0,1 +1,7 @@

2.0.0 / 2014-07-28
------------------
* added `\n` to end of file on write. [#14](https://github.com/jprichardson/node-jsonfile/pull/14)
* added `options.throws` to `readFileSync()`
* dropped support for Node v0.8
1.2.0 / 2014-06-29

@@ -2,0 +8,0 @@ ------------------

15

lib/jsonfile.js

@@ -28,3 +28,12 @@ var fs = require('fs')

me.readFileSync = function(file, options) {
return JSON.parse(fs.readFileSync(file, options))
var noThrow = options && !options.throws
if (!noThrow) //i.e. throw on invalid JSON
return JSON.parse(fs.readFileSync(file, options))
else
try {
return JSON.parse(fs.readFileSync(file, options))
} catch (err) {
return null
}
}

@@ -40,3 +49,3 @@

try {
str = JSON.stringify(obj, null, me.spaces)
str = JSON.stringify(obj, null, me.spaces) + '\n';
} catch (err) {

@@ -50,4 +59,4 @@ if (callback) return callback(err, null)

me.writeFileSync = function(file, obj, options) {
var str = JSON.stringify(obj, null, me.spaces)
var str = JSON.stringify(obj, null, me.spaces) + '\n';
return fs.writeFileSync(file, str, options) //not sure if fs.writeFileSync returns anything, but just in case
}

14

package.json
{
"name": "jsonfile",
"version": "1.2.0",
"version": "2.0.0",
"description": "Easily read/write JSON files.",

@@ -9,3 +9,8 @@ "repository": {

},
"keywords": ["read", "write", "file", "json"],
"keywords": [
"read",
"write",
"file",
"json"
],
"author": "JP Richardson <jprichardson@gmail.com>",

@@ -20,4 +25,5 @@ "licenses": [

"devDependencies": {
"testutil": "~0.5.1",
"mocha": "*"
"testutil": "^0.7.0",
"mocha": "*",
"terst": "^0.2.0"
},

@@ -24,0 +30,0 @@ "main": "./lib/jsonfile.js",

@@ -29,9 +29,9 @@ [![build status](https://secure.travis-ci.org/jprichardson/node-jsonfile.png)](http://travis-ci.org/jprichardson/node-jsonfile)

```javascript
var jf = require('jsonfile');
var util = require('util');
var jf = require('jsonfile')
var util = require('util')
var file = '/tmp/data.json';
var file = '/tmp/data.json'
jf.readFile(file, function(err, obj) {
console.log(util.inspect(obj));
});
console.log(util.inspect(obj))
})
```

@@ -43,11 +43,13 @@

```javascript
var jf = require('jsonfile');
var util = require('util');
var jf = require('jsonfile')
var util = require('util')
var file = '/tmp/data.json';
var file = '/tmp/data.json'
console.log(util.inspect(jf.readFileSync(file)));
console.log(util.inspect(jf.readFileSync(file)))
```
**options**: `throws`. Set to `false` if you don't ever want this method to throw on invalid JSON. Will return `null` instead. Defaults to `true`. Others passed directly to `fs.readFileSync`.
### writeFile(filename, [options], callback)

@@ -58,7 +60,7 @@

var file = '/tmp/data.json';
var obj = {name: 'JP'};
var file = '/tmp/data.json'
var obj = {name: 'JP'}
jf.writeFile(file, obj, function(err) {
console.log(err);
console.log(err)
})

@@ -72,6 +74,6 @@ ```

var file = '/tmp/data.json';
var obj = {name: 'JP'};
var file = '/tmp/data.json'
var obj = {name: 'JP'}
jf.writeFileSync(file, obj);
jf.writeFileSync(file, obj)
```

@@ -87,12 +89,12 @@

```
var jf = require('jsonfile');
var jf = require('jsonfile')
jf.spaces = 4;
var file = '/tmp/data.json';
var obj = {name: 'JP'};
var file = '/tmp/data.json'
var obj = {name: 'JP'}
jf.writeFile(file, obj, function(err) { //json file has four space indenting now
console.log(err);
});
console.log(err)
})
```

@@ -116,2 +118,3 @@

- [1] [Pablo Vallejo](https://github.com/PabloVallejo)
- [1] [Miroslav Bajtoš](https://github.com/bajtos)

@@ -118,0 +121,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc