Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
comment-json
Advanced tools
The comment-json npm package allows you to parse, modify, and stringify JSON files while preserving comments. This is particularly useful for configuration files where comments are often used to provide context or instructions.
Parsing JSON with comments
This feature allows you to parse JSON strings that include comments. The comments are ignored in the resulting JavaScript object.
const commentJson = require('comment-json');
const jsonWithComments = `{
// This is a comment
"key": "value"
}`;
const parsed = commentJson.parse(jsonWithComments);
console.log(parsed); // { key: 'value' }
Stringifying JSON with comments
This feature allows you to convert a JavaScript object back into a JSON string, optionally including comments.
const commentJson = require('comment-json');
const obj = { key: 'value' };
const jsonString = commentJson.stringify(obj, null, 2);
console.log(jsonString); // {
// This is a comment
"key": "value"
}
Modifying JSON while preserving comments
This feature allows you to modify a parsed JSON object and then convert it back to a JSON string, preserving the original comments.
const commentJson = require('comment-json');
const jsonWithComments = `{
// This is a comment
"key": "value"
}`;
let parsed = commentJson.parse(jsonWithComments);
parsed.newKey = 'newValue';
const modifiedJsonString = commentJson.stringify(parsed, null, 2);
console.log(modifiedJsonString); // {
// This is a comment
"key": "value",
"newKey": "newValue"
}
JSON5 is a JSON extension that aims to make JSON more human-friendly. It allows comments, trailing commas, and more. However, unlike comment-json, JSON5 does not preserve comments when parsing and stringifying.
Hjson is a user interface for JSON. It allows comments and is designed to be easy to read and write. Similar to JSON5, Hjson does not preserve comments when converting between JSON and JavaScript objects.
The jsonc-parser package is used by Visual Studio Code to handle JSON with comments. It can parse JSON with comments and provide a syntax tree, but it is more complex to use compared to comment-json.
The usage of comment-json
is exactly the same as the vanilla JSON
object.
$ npm install comment-json --save
package.json:
{
// package name
"name": "comment-json"
}
var json = require('comment-json');
var fs = require('fs');
var obj = json.parse(fs.readFileSync('package.json').toString());
console.log(obj);
// ->
// {
// "// name": [["// package name"]],
// name: "comment-json"
// }
json.stringify(obj, null, 2); // Will be the same as package.json
The arguments are the same as the vanilla JSON.parse
, except for removes_comments
:
Boolean
If true, the comments won't be maintained, which is often used when we want to get a clean object.Above all, json.parse()
is not a parser with 100% accuracy to output an AST which describes every detail of the commented json, including the locations of every comments, whitespaces, etc.
But it DOES work, and could meet most of our requirements to record important informations as fast as possible without making everything too complicated.
code:
/**
block comment at the top
*/
// comment at the top
{
// comment for a
// comment line 2 for a
/* block comment */
"a": 1 // comment at right
}
// comment at the bottom
var result = json.parse(code);
Then the result
will be:
{
// Comments at the top of the file
'//^': [
'/**\n block comment at the top\n */',
'// comment at the top'
],
// Comments at the bottom of the file
'//$': ['// comment at the bottom'],
// Comment for a property is the value of `'// <prop>'`
'// a': [
// Comments above property `a`
[
'// comment for a',
'// comment line 2 for a',
'/* block comment */'
],
['// comment at right']
],
// The real value
a: 1
}
json.parse(code, null, true);
// -> {a: 1}
TL;NR
There are two types of comments:
//
/*
and */
//^
, is the array which contains comments at the top. If there are two lines of comments which start with //
, they will be considered as two comment items in the array.
//$
, similar to //^
, is the comments at the bottom.
// <key>
, is a two-dimensional array contains the comments for a certain property key
.
key
key
The arguments are the same as the vanilla JSON.stringify
.
And it does the similar thing as the vanilla one, but also deal with extra properties and convert them into comments.
If space is not specified, or the space is an empty string, the result of json.stringify()
will be no comments.
For the case above:
console.log( json.stringify(result) ); // {"a":1}
console.log( json.stringify(result, null, 2) ); // is the same as `code`
MIT
FAQs
Parse and stringify JSON with comments. It will retain comments even after saved!
The npm package comment-json receives a total of 1,445,011 weekly downloads. As such, comment-json popularity was classified as popular.
We found that comment-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.