Bitcointalk

JSON-RPC password

中本聪 · 2010 年 7 月 21 日

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

阅读语言

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

I was researching config file formats, here's a comparison.

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

YAML is massive. I'm not sure there's a lightweight easy to build library we can integrate into our project. Seems overkill.

JSON 很诱人,我也倾向喜欢它,但有两个主要痛点:

JSON is tempting and I'm inclined to like it, but two main sticking points:

1) 不能写注释!一个配置文件居然不能注释掉一行来停用它?

1) No comments! How can you have a config file where you can't comment out a line to disable it?

2) 所有字符串都得「加引号」,连键也一样,还得记得行尾的逗号。

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",

"key" : "value",

}

}

我想我们可以很容易地预处理 JSON:逐行读配置文件,在任何 # 字符处截断行(还有 "//"?),拼成一个字符串传给 JSON,于是可以写成:

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

# comment

"key" : "value", # 还得记得逗号

"key" : "value", # still have to remember the comma

"key2" : "value", // 像这样注释,或两种都用

"key2" : "value", // comment like this or both

Boost 有 boost::program_options。

Boost has boost::program_options.

我们可以自己读行,喂进 map mapConfig。

We could read lines ourselves and feed them into a map mapConfig.

while (!eof)

while (!eof)

read line

read line

if '#' found, truncate line

if '#' found, truncate line

split line at first ':' -> key, value

split line at first ':' -> key, value

mapConfig.insert(key, value)

mapConfig.insert(key, value)

如果我们用这样的语法:

If we use the syntax:

# comment

# comment

key : value

key : value

……并且不允许键前面有空白缩进,我猜我们就是 YAML 的一个子集,将来需要更复杂的东西时可以切换到 YAML。

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

如果走自解析路线,也不妨碍按需对特定参数值用 JSON。某个选项需要列表或更结构化的数据时,它的值可以随时按 json 解析:

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"]

key : ["item1", "item2", "item3"]

只是那就必须全部写在一行上。

Although it has to be all on one line then.

我想我倾向于自解析的 mapConfig:

I guess I'm leaning towards self parsed mapConfig:

# comment

# comment

key : value

key : value

来源
Bitcointalk 原始链接 ↗ 记录编号 SN-1429

阅读字号

选择适合你的字号,之后阅读会继续使用。