原始文献 / 论坛帖 / Re: JSON-RPC password
Re: JSON-RPC password
Bitcointalk · 2010-07-21 05:51:34 UTC
我研究了一下配置文件格式,做了个对比。
YAML 太庞大了。我不确定有没有轻量好集成的库能放进我们项目。感觉杀鸡用牛刀。
JSON 很诱人,我也挺喜欢,但有两个主要问题:
1) 没有注释!配置文件里连一行都不能注释掉禁用,这怎么行?
2) 对人不友好,所有字符串包括键都要"加引号",还得记住行尾加逗号。
{
"key" : "value",
}
我想我们可以很容易地预处理 JSON:读配置文件时一行一行读,在任何 # 字符处(和/或 "//"?)截断行,把它们拼成一个字符串再传给 JSON,这样就可以:
# comment
"key" : "value", # 逗号还是得记住
"key2" : "value", // 这样注释或两种都行
Boost 有 boost::program_options。
我们也可以自己按行读,喂进一个 map<string, string> mapConfig。
while (!eof)
read line
if '#' found, truncate line
split line at first ':' -> key, value
mapConfig.insert(key, value)
如果用这个语法:
# comment
key : value
……而且键前面不允许缩进空格,那我们就是 YAML 的一个子集,以后需要更复杂时可以切到 YAML。
就算用自解析,也不意味着特定参数值不能用 JSON。如果某个选项需要列表或更结构化的数据,它的值随时可以按 json 解析:
key : ["item1", "item2", "item3"]
不过那样就必须全在一行里了。
我想我倾向于自解析的 mapConfig:
# comment
key : value
英文原文
I was researching config file formats, here's a comparison.
YAML is massive. I'm not sure there's a lightweight easy to build library we can integrate into our project. Seems overkill.
JSON is tempting and I'm inclined to like it, but two main sticking points:
1) No comments! How can you have a config file where you can't comment out a line to disable it?
2) Not very user friendly to have to "quote" all the strings, including the keys, and also have to remember the comma at the end of lines.
{
"key" : "value",
}
I suppose we could easily preprocess JSON reading the config file one line at a time, truncate the lines at any # character (and/or "//"?), concatenate them into a string and pass it to JSON, so you could go:
# comment
"key" : "value", # still have to remember the comma
"key2" : "value", // comment like this or both
Boost has boost::program_options.
We could read lines ourselves and feed them into a map<string, string> mapConfig.
while (!eof)
read line
if '#' found, truncate line
split line at first ':' -> key, value
mapConfig.insert(key, value)
If we use the syntax:
# comment
key : value
...and don't allow whitespace indenting before the keys, I guess we would be a subset of YAML and could switch to YAML someday if we need more complexity.
If we go with self parsed, that doesn't mean we can't use JSON on particular parameter values as needed. If an option needs a list or more structured data, it could always parse its value as json:
key : ["item1", "item2", "item3"]
Although it has to be all on one line then.
I guess I'm leaning towards self parsed mapConfig:
# comment
key : value
