*.json config files

598 views
Skip to first unread message

Oleg Slobodskoi

unread,
Mar 21, 2011, 12:25:09 PM3/21/11
to nodejs
Hi,

well, my problem is: I like using json files for configs, but the
problem is large configs need to be commented.

1. JSON spec doesn't has comments, so using JSON.parse will throw
error.
2. it is not a valid json anymor, so have to rename .json to .js

My workarounds:
1. not to use JSON.parse, but evaluate the string, f.e. using
vm.runInNewContext. In this case somebody could write any js into the
config, but he shouldn't. Also it is less secure, but it doesn't
matter for config files.
or
2. strip out the comments first and then use JSON.parse ..., will
force to use valid json notation except of comments, but it is still
not valid .json file.

Actually this needs to be a new spec: cjson .. lol "comented
javascript object notation"

Somebody want to write a spec :) ?


Oleg

Tim Caswell

unread,
Mar 21, 2011, 12:35:04 PM3/21/11
to nod...@googlegroups.com
If security doesn't matter, what's wrong with using plain JS files?  You get a much laxer parser and can use comments as well as non-quoted keys.  Also you can do advanced stuff like require other config files to break up large config files to several modules, or change config values based on properties of the environment.


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Oleg Slobodskoi

unread,
Mar 21, 2011, 12:42:54 PM3/21/11
to nod...@googlegroups.com
just because I want to keep the config clean, consistent and easy readable. If I use a js file, this is not a config anymore for me, its just js. And due to the fact the people can do whatever they want, this will become less readable then pure json file. Probably I am trying to adress a non-existent issue ... but I like commented json ... lol


2011/3/21 Tim Caswell <t...@creationix.com>



--
regards,
Oleg Slobodskoi
--------------------------------------------------------------------
Xing: https://xing.com/profile/Oleg_Slobodskoi
Twitter: https://twitter.com/oleg008
Github: https://github.com/kof

Fedor Indutny

unread,
Mar 21, 2011, 12:46:08 PM3/21/11
to nod...@googlegroups.com
You can use "//" as key value for comments, f.e. :
{
  "//": "here comes http server configuration",
  "http": {
     "port": 80,
     "host": "localhost"
  },

  "//": "that's database conf",
  "db": {
     ...

Tim Caswell

unread,
Mar 21, 2011, 12:49:06 PM3/21/11
to nod...@googlegroups.com
If you really want a restricted format like JSON, but modify it a little, then that's not too hard.  There are many open source JSON parsers out there to be modified, and you can easily add support for "cjson" or whatever you call it to node's require using register extension.  Even if it's a niche use case that only you need, it's not hard to solve at all.  I just wanted to make sure you had considered using plain JS files.  I fine it refreshingly simple and easy.

http://t.co/mXfUyH5 - register extension example.
http://zaach.github.com/jison/docs/ - A compiler generator that's used for coffeescript and has an example for JSON parsing

Oleg Slobodskoi

unread,
Mar 21, 2011, 12:50:48 PM3/21/11
to nod...@googlegroups.com
nice!  never thought about this :)

2011/3/21 Fedor Indutny <fe...@indutny.com>

Isaac Schlueter

unread,
Mar 21, 2011, 12:51:37 PM3/21/11
to nod...@googlegroups.com
Why not use INI format?
https://github.com/isaacs/npm/blob/master/lib/utils/ini-parser.js

That's what npm uses for the .npmrc file. Very readable, commentable, etc.

--i

Isaac Schlueter

unread,
Mar 21, 2011, 12:53:30 PM3/21/11
to nod...@googlegroups.com
Also, if you parse them in a new context, the damage they can do is
pretty minimal. Of course, you could have a `while(1);` in there or
something.

Oleg Slobodskoi

unread,
Mar 21, 2011, 1:06:52 PM3/21/11
to nod...@googlegroups.com
its actually a standard for the rest of the world, but I still tend to .json, because:

- I suppose you has implemented only a part of the ini spec, is there actually a real spec?
- json has some more things like arrays, booleans and objects literals which can be used as values, not only strings
- dependency on parser lib (not really an issue)

P.S. can you push your parser as new independent module and describe what it can?



2011/3/21 Isaac Schlueter <i...@izs.me>

Diogo Resende

unread,
Mar 21, 2011, 1:17:16 PM3/21/11
to nod...@googlegroups.com
I actually use the this:

var settings = JSON.parse(
fs.readFileSync("./settings.json").toString().replace(
new RegExp("\\/\\*(.|\\r|\\n)*?\\*\\/", "g"),
"" // strip out comments
)
);

It only supports /**/ comments..

Oleg Slobodskoi

unread,
Mar 21, 2011, 1:23:44 PM3/21/11
to nod...@googlegroups.com
hah, lol - I knew  I am not the only one with this issue ( @Tim :) )

thanks @diogo for your regex ...

2011/3/21 Diogo Resende <dio...@gmail.com>

Isaac Schlueter

unread,
Mar 21, 2011, 1:34:51 PM3/21/11
to nod...@googlegroups.com
On Mon, Mar 21, 2011 at 10:23, Oleg Slobodskoi <ole...@googlemail.com> wrote:
> 2011/3/21 Diogo Resende <dio...@gmail.com>
>>
>> I actually use the this:
>>
>> var settings = JSON.parse(
>>        fs.readFileSync("./settings.json").toString().replace(
>>                new RegExp("\\/\\*(.|\\r|\\n)*?\\*\\/", "g"),
>>                "" // strip out comments
>>        )
>> );

This would support both:

fs.readFileSync(blah, "utf8").replace(/\/\*(?:.|\r|\n)*?\*\/|\/\/[^\n]\n/, "")

However, only trivially. This will break it:

{ "block comments": "look /* like this */"
, "line comments" : "look // like this" }

to reliably strip out comments from arbitrary json, you have to
actually walk the string, keeping track of the state (whether you're
in a single-quoted string, a double-quoted string, whether the last
char was a \ escape, etc.) Not rocket surgery, but not a one-liner,
either.

On Mon, Mar 21, 2011 at 10:06, Oleg Slobodskoi <ole...@googlemail.com> wrote:
> - I suppose you has implemented only a part of the ini spec, is there
> actually a real spec?

Nope. Just implementations, as far as I know. And the
implementations are all a little different.

> - json has some more things like arrays, booleans and objects literals which
> can be used as values, not only strings

That's true. The config handling logic deeper in npm interprets
"true", "false", "null", and numbers as their intended values.

> P.S. can you push your parser as new independent module and describe what it
> can?

It's on the todo list to split out the whole config-and-param parsing
logic in npm out into a separate package.

--i

khs4473

unread,
Mar 21, 2011, 2:40:36 PM3/21/11
to nod...@googlegroups.com
var json = commentedJSON.replace(/\/\/.*|\/\*[\s\S]*?\*\/|("(?:\\[\s\S]|[^"\\])*")|('(?:\\[\s\S]|[^'\\])*')/g, "$1$2");

or something

Arnout Kazemier

unread,
Mar 21, 2011, 3:13:10 PM3/21/11
to nod...@googlegroups.com
I just run my configs through JSON minify before I parse them: https://github.com/getify/JSON.minify

On Mar 21, 2011, at 7:40 PM, khs4473 wrote:

var json = commentedJSON.replace(/\/\/.*|\/\*[\s\S]*?\*\/|("(?:\\[\s\S]|[^"\\])*")|('(?:\\[\s\S]|[^'\\])*')/g, "$1$2");

or something

Marak Squires

unread,
Mar 21, 2011, 4:15:52 PM3/21/11
to nod...@googlegroups.com, Oleg Slobodskoi
My standard workaround is using eval for this. Eval seems to not care about the comments. 

On Mon, Mar 21, 2011 at 9:25 AM, Oleg Slobodskoi <ole...@googlemail.com> wrote:

Oleg Slobodskoi

unread,
Mar 21, 2011, 7:58:14 PM3/21/11
to nodejs
Thanks for all answers, I decided to spend a half day and write a node module. It is already published to npm and has a good test coverage.



It can read your .json files with single and multiline comments, taking care about comments inside of strings. You can pass a path to one file, array of files or a directory and optionally merge many configs to one object.

Enjoy :)




2011/3/21 Oleg Slobodskoi <ole...@googlemail.com>
--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Julio Napurí Carlos

unread,
Mar 22, 2011, 11:01:33 AM3/22/11
to nod...@googlegroups.com, Oleg Slobodskoi
Maybe you should use YAML for configuration files in Node? It more
clean and elegant. See this js-yaml [1]

[1] https://github.com/visionmedia/js-yaml

Reply all
Reply to author
Forward
0 new messages