开发者

How can I use Groovy's configslurper to serialize lists?

开发者 https://www.devze.com 2023-02-06 20:50 出处:网络
I want to use the ConfigSlurper to write a configuration file to disk, but I get the error: groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.indexOf() is applicable for

I want to use the ConfigSlurper to write a configuration file to disk, but I get the error:

groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.indexOf() is applicable for argument types: (java.lang.String) values: [.]

The foll开发者_C百科owing example works fine until I uncomment the "aList" part of it. Is there a problem with the serialization of this in the config slurper, or is there an alternative way that I could write this?

def configObj = new ConfigObject()
configObj.testing = [1, 2, 3]
configObj.nested = [ objects : 'wtf' ]

/*configObj.aList[0] = 
    [
        listItem:"000",
        subLists:(["ZZ","AB"]),
    ]*/
print configObj

//serialize it
new File( 'newout.groovy' ).withWriter{ writer ->
  configObj.writeTo( writer )
}


Yes, this should work. You only need to initialise the map first like this:

configObj.aList = []

And then use your code

configObj.aList[0] = 
    [
        listItem:"000",
        subLists:(["ZZ","AB"]),
    ]

Alternatively, just in-line the element

configObj.aList = [
    [
        listItem:"000",
        subLists:(["ZZ","AB"]),
    ]
]
0

精彩评论

暂无评论...
验证码 换一张
取 消