Serializing groovy map to string with quotes

后端 未结 2 1634
猫巷女王i
猫巷女王i 2020-12-24 12:50

I\'m trying to persist a groovy map to a file. My current attempt is to write the string representation out and then read it back in and call evaluate on it to

相关标签:
2条回答
  • 2020-12-24 13:17

    Groovy provides the inspect() method returns an object as a parseable string:

    // serialize
    def m = [a: 123, b: 'test']
    def str = m.inspect()
    
    // deserialize
    m = Eval.me(str)
    

    Another way to serialize a groovy map as a readable string is with JSON:

    // serialize
    import groovy.json.JsonBuilder
    def m = [a: 123, b: 'test']
    def builder = new JsonBuilder()
    builder(m)
    println builder.toString()
    
    // deserialize
    import groovy.json.JsonSlurper
    def slurper = new JsonSlurper()
    m = slurper.parseText('{"a": 123, "b": "test"}')
    
    0 讨论(0)
  • 2020-12-24 13:18

    You can use myMap.toMapString()

    0 讨论(0)
提交回复
热议问题