Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Comments to json variables (the fix code I added) #2559

Open
vruzin opened this issue Oct 16, 2020 · 2 comments
Open

Add Comments to json variables (the fix code I added) #2559

vruzin opened this issue Oct 16, 2020 · 2 comments

Comments

@vruzin
Copy link

vruzin commented Oct 16, 2020

https://github.com/stylus/stylus/blob/dev/lib/functions/json.js

change line 54-55

  var json = JSON.parse(readFile(found, 'utf8'));

on

 // read
  var json = readFile(found, 'utf8');

  // remove comments from json
  json = json.replace(/((["'])(?:\\[\s\S]|.)*?\2|\/(?![*\/])(?:\\.|\[(?:\\.|.)\]|.)*?\/)|\/\/.*?$|\/\*[\s\S]*?\*\//gm,'$1');
  json = JSON.parse(json);

it's work in:

$Opt=json('Variables.json', { hash: true })

Variables.json:

{
   "color1:"#ffffff", // Primary color
   // Secondary color
   "color2":"#000000",
   /* new color */
   "color3":"#999999"
}
@vruzin
Copy link
Author

vruzin commented Oct 16, 2020

Or better version:

https://github.com/stylus/stylus/blob/dev/lib/functions/json.js

replace line 55

var json = JSON.parse(readFile(found, 'utf8'));

on

var json = JSON5.parse(readFile(found, 'utf8'));

and add line after line 2:

, JSON5 = require('json5')

and in console:

npm install json5

and you can add JSON5 in package.json file

@iChenLei
Copy link
Member

Did you mean support jsonc ? Json with comment is not valid json file, I think it's out of scope, but discussion is ok.

releated project https://github.com/microsoft/node-jsonc-parser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants