How to print Groovy list and keep the quotes?

We have a list:

List test = ["a", "b", "c"]

I don't want to alter this list hardcoded, since it has many items.

When printing this like:

println "${test}"

We get [a, b, c] but I want to have ["a", "b", "c"]

Any suggestions?


Solution 1:

You can try representing your list as String by joining all elements like this:

List test = ["a", "b", "c"]

String listAsString =  "[\"${test.join('", "')}\"]"

println listAsString

Output

["a", "b", "c"]

It join all elements using ", " and adds [" in the beginning and "] in the end of the string.

Solution 2:

Groovy has inspect() for better output (closer to input, but be aware, that this is no proper way to serialize Groovy datastructures):

Groovy Shell (2.5.0-beta-1, JVM: 1.8.0_152)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------
groovy:000> test = ["a", "b", "c"]
===> [a, b, c]
groovy:000> test.inspect()
===> ['a', 'b', 'c']