guy mograbi
guy mograbi

Reputation: 28608

Groovy - to string that passes Eval.me

I would like to be able to turn strings to object back and forth in groovy.

For example

def mapString = "['guy':'mograbi']"
def myMap = Eval.me(mapString)
def savedString = myMap.toString()
def savedMap = Eval.me(savedString)

the last line fails because toString does not output a String I can evaluate.

How can I turn the map to a string I can evaluate?

Upvotes: 1

Views: 1474

Answers (1)

ataylor
ataylor

Reputation: 66059

The inspect() method returns a parseable string. Example:

def map = [guy:'mograbi']
def str = map.inspect()
def mapFromString = Eval.me(str)
assert map == mapFromString

Upvotes: 3

Related Questions