json-easy-strip
v1.1.0
Published
Load and strip JS-type comments from JSON file, easy and fast!
Maintainers
Readme
json-easy-strip
Load and strip JS-type comments from JSON file, easy and fast!
- Works with all JS-type comments
- Allows trailing commas and UTF-8 BOM
- File caching functionality
- Optional access to strip method only
- Super easy and fast, zero dependencies
- Tiny: about 110 lines of code (with comments!)
Install
$ npm install json-easy-stripUsage
sweet.json:
{
/*
* Sweet section
*/
"fruit": "Watermelon", // Yes, watermelons is sweet!
"dessert": /* Yummy! */ "Cheesecake",
// And finally
"drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}index.js:
const requireJSON = require('json-easy-strip');
const obj = requireJSON('./sweet.json');
console.log(obj);
// {
// fruit: 'Watermelon',
// dessert: 'Cheesecake',
// drink: 'Milkshake - /* strawberry */ or // chocolate!'
// }
Caching
Enabled by default, but you always can flush it. As with the standard require(),
cached object is shared between calls (mutations are visible everywhere) and file
changes are not tracked until the cache entry is deleted
const path = require('path');
const requireJSON = require('json-easy-strip');
// Loads, strips and parse sweet.json, puts it
// in the cache, and then returns a JSON object
const obj = requireJSON('./sweet.json');
// Returns a JSON object from cache
const obj2 = requireJSON('./sweet.json');
// Flushes the cache as in the standard require() function
delete requireJSON.cache[path.resolve('./sweet.json')];
// Again loads, strips and parse sweet.json and
// puts it in the cache
const obj3 = requireJSON('./sweet.json');
Access to strip method only
If you don't need to load file, only strip method is avaliable
const requireJSON = require('json-easy-strip');
const jsonString = `{
/*
* Sweet section
*/
"fruit": "Watermelon", // Yes, watermelons is sweet!
"dessert": /* Yummy! */ "Cheesecake",
// And finally
"drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}`;
const obj = requireJSON.strip(jsonString);
console.log(obj);
// {
// fruit: 'Watermelon',
// dessert: 'Cheesecake',
// drink: 'Milkshake - /* strawberry */ or // chocolate!'
// }API
requireJSON(path)
path
Type: string
Path to JSON file.
returns
Type: object
The function returns a parsed JSON object, as the standard require() function does.
Parse errors are thrown as SyntaxError with the file path prepended to the message
(positions in the message match the file).
requireJSON.strip(data)
data
Type: string
Stringified JSON data, loaded or recieved from elsewhere.
returns
Type: object
The function returns a parsed JSON object. Comments, trailing commas (after a value)
and leading BOM are replaced with whitespace before parsing, so error positions match
the original data; everything else must be valid JSON.
requireJSON.cache
Type: object
Contains cached data as key (absolute file path) => value (JSON object).
Notes
- Line comments end at
\nor\r, as in VS Code's JSONC parser:U+2028andU+2029don't end them.
Tests
$ npm test