漂亮的 ConfigObject 打印?
我有这个 groovy 程序,它使用 ConfigObject 创建一个 groovy 配置文件。设置 ConfigObject 后,将使用以下命令将其写入文件:
myFile.withWriter {writer -> myConfigObject.writeTo(writer)}
这会导致 ConfigObject 的每个属性都写入一行。例如,一张地图将被打印为:
graphs=[["type":"std", "host":"localhost", "name":"cpurawlinux"], ["type":"std", "host":"localhost", "name":"memory"], ["type":"std", "host":"localhost", "name":"udp"] ... ]
如果有人必须看一下它,这是非常不可读的。 有没有办法获得更友好的输出?类似的东西会很棒:
graphs=[
["type":"std", "host":"localhost", "name":"cpurawlinux"],
["type":"std", "host":"localhost", "name":"memory"],
["type":"std", "host":"localhost", "name":"udp"]
...
]
我知道我可以创建自己的 writeTo
,但是 Groovy 中不是已经有这样的东西了吗?
I have this groovy program that creates a groovy configuration file, using a ConfigObject. Once the ConfigObject is set up, it is written to a file using:
myFile.withWriter {writer -> myConfigObject.writeTo(writer)}
This results in each property of the ConfigObject being written on a single line. So for instance a map will be printed as:
graphs=[["type":"std", "host":"localhost", "name":"cpurawlinux"], ["type":"std", "host":"localhost", "name":"memory"], ["type":"std", "host":"localhost", "name":"udp"] ... ]
which is quite unreadable if someone has to take a look at it.
Is there a way to get a more friendly output? Something like that would be great:
graphs=[
["type":"std", "host":"localhost", "name":"cpurawlinux"],
["type":"std", "host":"localhost", "name":"memory"],
["type":"std", "host":"localhost", "name":"udp"]
...
]
I know I could create my own writeTo
, but isn't there already something in Groovy for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,您需要像您所说的那样编写自己的
writeTo
。如果你有一个结构如下的配置文件:
那么 writeTo 会用结构写出它,但如果你的配置文件只是一个大的旧列表,它会把它写成一个大的旧列表
Unfortunately, you'll need to write your own
writeTo
as you say.If you have a config file with structure like:
Then writeTo will write it out with structure, but if your config file is just a big old list of things, it will write it out as a big old list
如果它对任何人有帮助,我有同样的问题并写了这个......不漂亮(哈),但有效:
if it helps anyone, i had the same question and wrote this...not pretty (ha), but works:
基于迈克上面的回答:
对于如下输入:
产生:
Based on mike's answer above:
For an input like:
Produces:
由于 GreenGiant 的答案在我尝试使用它时似乎已损坏,因此这里有一个工作版本以供将来参考:
Since GreenGiant answer seems broken when I tried using it, here's a working version for future reference :