If you know the nested Map structure in advance, your solution will work. If you need to do this on an unknown arbitrary nested Map structure, try something like this:
import groovy.util.ConfigObject
def mapToConfig
mapToConfig = { Map map ->
map.collectEntries { k, v ->
v instanceof Map ? [(k):mapToConfig(v)] : [(k):v]
} as ConfigObject
}
Given your input and the above closure definition, the following print statement:
println mapToConfig(config).prettyPrint()
Yields this output:
test='lalala'
nestedObject {
foo1='foo1 val'
foo2='foo2 val'
nested2 {
anInt=5
anArray=['a', 'b', 'c']
anIntArray=[1, 2, 3]
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…