I need to convert a String representation of a nested List back to a nested List (of Strings) in Groovy / Java, e.g.
String myString = "[[one, two], [three, four]]"
List myList = isThereAnyMethodForThis(myString)
I know that there’s the Groovy .split method for splitting Strings by comma for example and that I could use regular expressions to identify nested Lists between [ and ], but I just want to know if there’s an existing method that can do this or if I have to write this code myself.
I guess the easiest thing would be a List constructor that takes the String representation as an argument, but I haven’t found anything like this.
In Groovy, if your strings are delimited as such, you can do this:
However, if they are not delimited like in your example, I think you need to start playing with the shell and a custom binding…
Edit to explain things
The Binding in Groovy “Represents the variable bindings of a script which can be altered from outside the script object or created outside of a script and passed into it.”
So here, we create a custom binding which returns the name of the variable when a variable is requested (think of it as setting the default value of any variable to the name of that variable).
We set this as being the Binding that the GroovyShell will use for evaluating variables, and then run the String representing our list through the Shell.
Each time the Shell encounters
one,two, etc., it assumes it is a variable name, and goes looking for the value of that variable in the Binding. The binding simply returns the name of the variable, and that gets put into our listAnother edit… I found a shorter way
You can use Maps as Binding objects in Groovy, and you can use a
withDefaultclosure to Maps so that when a key is missing, the result of this closure is returned as a default value for that key. An example can be found hereThis means, we can cut the code down to:
As you can see, the Map (thanks to
withDefault) returns the key that was passed to it if it is missing from the Map.