Search code examples
javagroovyformattingconfiguration-files

Pretty print for a groovy ConfigObject?


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?


Solution

  • Unfortunately, you'll need to write your own writeTo as you say.

    If you have a config file with structure like:

    graphs {
      a=["type":"std", "host":"localhost", "name":"cpurawlinux"]
      b=["type":"std", "host":"localhost", "name":"memory"]
    }
    

    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