SnakeYaml 转储函数用单引号写入
SnakeYaml dump function writes with single quotes
考虑以下代码:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
Yaml yaml = new Yaml();
FileWriter writer = new FileWriter("/path/to/file.yaml");
for (Map.Entry m : data.entrySet()) {
String temp = new StringBuilder().append(m.getKey()).append(": ").append(m.getValue()).toString();
yaml.dump(temp, file);
}
}
以上代码的输出为
'NAME1: Raj'
'NAME2: Kumar'
但我想要不带单引号的输出,例如
NAME1: Raj
NAME2: Kumar
这个东西解析文件很舒服。
如果有人有解决方案,请帮我解决。提前致谢
好吧,SnakeYaml 完全按照您的要求执行:对于 Map 中的每个条目,它将键、字符串 ": "
和值的串联转储为 YAML 文档。字符串映射到 YAML 中的标量,并且由于标量包含 :
后跟 space,它必须被引用(否则它将是键值对)。
您实际上想要做的是将地图转储为 YAML 映射。你可以这样做:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter("/path/to/file.yaml");
yaml.dump(data, writer);
}
考虑以下代码:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
Yaml yaml = new Yaml();
FileWriter writer = new FileWriter("/path/to/file.yaml");
for (Map.Entry m : data.entrySet()) {
String temp = new StringBuilder().append(m.getKey()).append(": ").append(m.getValue()).toString();
yaml.dump(temp, file);
}
}
以上代码的输出为
'NAME1: Raj'
'NAME2: Kumar'
但我想要不带单引号的输出,例如
NAME1: Raj
NAME2: Kumar
这个东西解析文件很舒服。 如果有人有解决方案,请帮我解决。提前致谢
好吧,SnakeYaml 完全按照您的要求执行:对于 Map 中的每个条目,它将键、字符串 ": "
和值的串联转储为 YAML 文档。字符串映射到 YAML 中的标量,并且由于标量包含 :
后跟 space,它必须被引用(否则它将是键值对)。
您实际上想要做的是将地图转储为 YAML 映射。你可以这样做:
public void testDumpWriter() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("NAME1", "Raj");
data.put("NAME2", "Kumar");
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter("/path/to/file.yaml");
yaml.dump(data, writer);
}