【发布时间】:2015-09-01 04:13:02
【问题描述】:
我通过参考 yaml-cpp 教程制作了简单的示例代码来演示使用 yaml-cpp(版本 0.5.2)进行 YAML 加载/转储。转储结果文件不是我所期望的。
- 手动编辑的 YAML 文件
# config.yaml file
lastLogin: 1441030476
password: "pass1234"
username: "admin"
- 示例程序
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "yaml-cpp/yaml.h"
int main()
{
const char config_yaml[] = "config.yaml";
YAML::Node config = YAML::LoadFile(config_yaml);
if (config["lastLogin"]) {
std::cout << "Last logged in: " << config["lastLogin"].as<int>() << "\n";
}
const std::string username = config["username"].as<std::string>();
const std::string password = config["password"].as<std::string>();
std::cout << "username: '" << username << "'" << std::endl;
std::cout << "password: '" << password << "'" << std::endl;
config["lastLogin"] = (int)time(0);
std::ofstream fout(config_yaml);
fout << config;
return 0;
}
- 生成的转储文件
# config.yaml file
lastLogin: 1441030476
password: !<!> pass1234
username: !<!> admindvp
正如您在上面生成的文件中看到的,字符串的值从带引号的字符串更改为"!<!>" 前缀字符串。
我的问题是转储结果中的字符串值之前的"!<!>" 是什么意思。
以及我如何使用上面的示例制作 yaml-cpp 转储引号字符串。
Nodejs yamljs 模块无法正确加载字符串,它会将其识别为 null,因此"!<!>" 表示法不能用于字符串。
【问题讨论】:
-
也许这会触发某人为数据文件发明第 N+1 (N >> 4) 个文本语法...*打哈欠* 对于所有这些 N“技术”,将有 50 %-80% 完成的工具链和库。当然,每个都可以说比另一个更好。