*.json config files

Showing 1-17 of 17 messages
*.json config files Oleg Slobodskoi 3/21/11 9:25 AM
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

Re: [nodejs] *.json config files Tim Caswell 3/21/11 9:35 AM
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.


Re: [nodejs] *.json config files Oleg Slobodskoi 3/21/11 9:42 AM
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
Re: [nodejs] *.json config files Fedor Indutny 3/21/11 9:46 AM
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": {
     ...
Re: [nodejs] *.json config files Tim Caswell 3/21/11 9:49 AM
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
Re: [nodejs] *.json config files Oleg Slobodskoi 3/21/11 9:50 AM
nice!  never thought about this :)

2011/3/21 Fedor Indutny <fe...@indutny.com>
Re: [nodejs] *.json config files Isaac Schlueter 3/21/11 9:51 AM
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

Re: [nodejs] *.json config files Isaac Schlueter 3/21/11 9:53 AM
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.
Re: [nodejs] *.json config files Oleg Slobodskoi 3/21/11 10:06 AM
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>
Re: [nodejs] *.json config files Diogo Resende 3/21/11 10:17 AM
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..

> --
> 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.
>
>

Re: [nodejs] *.json config files Oleg Slobodskoi 3/21/11 10:23 AM
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>



--
regards,
Oleg Slobodskoi
--------------------------------------------------------------------
Xing: https://xing.com/profile/Oleg_Slobodskoi
Twitter: https://twitter.com/oleg008
Github: https://github.com/kof
Re: [nodejs] *.json config files Isaac Schlueter 3/21/11 10:34 AM
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

Re: [nodejs] *.json config files khs4473 3/21/11 11:40 AM
var json = commentedJSON.replace(/\/\/.*|\/\*[\s\S]*?\*\/|("(?:\\[\s\S]|[^"\\])*")|('(?:\\[\s\S]|[^'\\])*')/g, "$1$2");

or something
Re: [nodejs] *.json config files 3rdEden 3/21/11 12:13 PM
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

--
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.

Re: [nodejs] *.json config files Marak Squires 3/21/11 1:15 PM
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:

--
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.


Re: [nodejs] *.json config files Oleg Slobodskoi 3/21/11 4:58 PM
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.



Re: [nodejs] *.json config files julionc 3/22/11 8:01 AM
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