Package Exports
- jsonfile
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonfile) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Node.js - jsonfile
Easily read/write JSON files.
Why?
Writing JSON.stringify()
and then fs.writeFile()
and JSON.parse()
with fs.readFile()
enclosed in try/catch
blocks became annoying.
Installation
npm install jsonfile --save
API
readFile()
var jf = require('jsonfile')
, util = require('util');
var file = '/tmp/data.json';
js.readFile(file, function(err, obj) {
console.log(util.inspect(obj));
});
readFileSync()
var jf = require('jsonfile')
, util = require('util');
var file = '/tmp/data.json';
console.log(util.inspect(jf.readFileSync(file)));
writeFile()
var jf = require('jsonfile')
var file = '/tmp/data.json';
var obj = {name: 'JP'};
jf.writeFile(file, obj, function(err) {
console.log(err);
})
writeFileSync()
var jf = require('jsonfile')
var file = '/tmp/data.json';
var obj = {name: 'JP'};
jf.writeFileSync(file, obj);
spaces
Number of spaces to indent JSON files.
default: 2
var jf = require('jsonfile');
jf.spaces = 4;
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);
});
License
(MIT License)
Copyright 2012-2013, JP Richardson jprichardson@gmail.com