用杰克逊写yaml? [英] Use Jackson to write yaml?

查看:107
本文介绍了用杰克逊写yaml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson阅读和修改yaml文件。效果很好。但是我无法找到编写yaml所需的神奇咒语。

I'm using Jackson to read and modify yaml files. Works great. I can't find the magic incantations needed to write the yaml, though.

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
ObjectNode root = (ObjectNode)mapper.readTree(yamlFileIn);
// modify root here
mapper.writeValue(yamlFileOut, root); // writes json, not yaml. not sure why.

我确信这是作家,JsonGenerators和其他东西的组合。有人得到示例代码吗?

I'm sure it's some combination of writers, JsonGenerators, and something else. Anyone got sample code?

推荐答案

对于v2.8.3,以下内容应该有效:

For v2.8.3 the following should work:

YAMLFactory yf = new YAMLFactory();
ObjectMapper mapper = new ObjectMapper(yf);
ObjectNode root = (ObjectNode) mapper.readTree(yamlFileIn);
// modify root here     
FileOutputStream fos = new FileOutputStream(yamlFileOut);
SequenceWriter sw = mapper.writerWithDefaultPrettyPrinter().writeValues(fos);
sw.write(root);

这篇关于用杰克逊写yaml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆