Bitcointalk · JSON-RPC password

中本聪,2010 年 7 月 21 日

SN-1429 已核对来源,附原文与上下文。

阅读语言
中文译文

我调研了一圈配置文件格式,给你个对比。

YAML 太庞大了。我不确定有轻量易集成的库能进我们的项目。感觉杀鸡用牛刀。

JSON 很诱人,我也倾向喜欢它,但有两个主要痛点: 1) 不能写注释!一个配置文件居然不能注释掉一行来停用它? 2) 所有字符串都得「加引号」,连键也一样,还得记得行尾的逗号。 { "key" : "value", }

我想我们可以很容易地预处理 JSON:逐行读配置文件,在任何 # 字符处截断行(还有 "//"?),拼成一个字符串传给 JSON,于是可以写成: # comment "key" : "value", # 还得记得逗号 "key2" : "value", // 像这样注释,或两种都用

Boost 有 boost::program_options。

我们可以自己读行,喂进 map 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

ORIGINAL · 英文原文
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
来源
Bitcointalk 原始链接 ↗ 记录编号 SN-1429