-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Add json read and write utility based on circular-json
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var fs = require( 'graceful-fs' ); | ||
var write = require( 'write' ); | ||
var circularJson = require( 'circular-json' ) | ||
|
||
module.exports = { | ||
|
||
/** | ||
* Read json file synchronously using circular-json | ||
* | ||
* @method readJSON | ||
* @param {String} filePath Json filepath | ||
* @returns {*} parse result | ||
*/ | ||
readJSON: function ( filePath ) { | ||
return circularJson.parse( fs.readFileSync( filePath ).toString().replace(/^\ufeff/g, '') ); | ||
}, | ||
|
||
/** | ||
* Write json file synchronously using circular-json | ||
* | ||
* @method writeJSON | ||
* @param {String} filePath Json filepath | ||
* @param {*} data Object to serialize | ||
*/ | ||
writeJSON: function (filePath, data ) { | ||
write.sync( filePath, circularJson.stringify( data ) ); | ||
} | ||
|
||
}; |