Groovy:是不是有一个stringToMap开箱即用? [英] Groovy: isn't there a stringToMap out of the box?
问题描述
我习惯于在tcl中的字符串,列表和数组/地图之间进行转换。在tcl中,像
['a':2,'b':4] - > println key ++ value}
将是可能的,在groovy中,每个命令步骤通过字符串的每个字符。
这将是一个很大的问题,我可以很容易地使用像split或tokenize命令,但因为序列化列表或映射isn 只是a:2,b:4,这有点难以解析。
看起来,griffon开发人员使用一个stringToMap库( http://code.google.com/p/stringtomap/ ),但该示例无法应对使用序列化的地图。
所以我的问题现在是:在groovy中解析地图或列表的最好方法是什么?
干杯,
Ralf
PS:这是一个groovy的问题,但我用grails标记了它,因为我需要这个功能grails我想通过URL传递地图
更新:这仍然是一个开放的问题给我...所以这里有一些更新为那些谁有同样的问题:
- 当你把一个地图变成一个字符串,一个
.toString()
将导致在所有情况下都不能返回到地图的东西,但是一个.inspect()
将给您一个可以评估回到地图的String! - 在Grails中,有一个
.encodeAsJSON()
和JSON.parse(String)
- 两个都很好,但是我还没有检查出解析器将对JSON函数做什么(可能的安全问题)
不完全是原生的groovy,但可用于序列化到JSON:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def map = ['a ':2,'b':4]
def s = new JsonBuilder(map).toString()
println s
assert map == new JsonSlurper()。parseText (s)
编程:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
Map.metaClass.toJson = {new JsonBuilder(delegate).toString()}
String.metaClass.toMap = {new JsonSlurper()。parseText(delegate)}
def map = [ 'a':2,'b':4]
assert map.toJson()=='{a:2,b:4}'
assert map.toJson()。 toMap()== map
不幸的是,不可能覆盖toString()方法...
as a tcl developer starting with groovy, I am a little bit surprised about the list and map support in groovy. Maybe I am missing something here.
I am used to convert between strings, lists and arrays/maps in tcl on the fly. In tcl, something like
"['a':2,'b':4]".each {key, value -> println key + " " + value}
would be possible, where as in groovy, the each command steps through each character of the string.
This would be much of a problem is I could easily use something like the split or tokenize command, but because a serialized list or map isn't just "a:2,b:4", it is a little bit harder to parse.
It seems that griffon developers use a stringToMap library (http://code.google.com/p/stringtomap/) but the example can't cope with the serialized maps either.
So my question is now: what's the best way to parse a map or a list in groovy?
Cheers, Ralf
PS: it's a groovy question, but I've tagged it with grails, because I need this functionality for grails where I would like to pass maps through the URL
Update: This is still an open question for me... so here are some updates for those who have the same problem:
- when you turn a Map into a String, a
.toString()
will result in something which can't be turned back into a map in all cases, but an.inspect()
will give you a String which can be evaluated back to a map! - in Grails, there is a
.encodeAsJSON()
andJSON.parse(String)
- both work great, but I haven't checked out yet what the parser will do with JSON functions (possible security problem)
Not exactly native groovy, but useful for serializing to JSON:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def map = ['a':2,'b':4 ]
def s = new JsonBuilder(map).toString()
println s
assert map == new JsonSlurper().parseText(s)
with meta-programming:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
Map.metaClass.toJson = { new JsonBuilder(delegate).toString() }
String.metaClass.toMap = { new JsonSlurper().parseText(delegate) }
def map = ['a':2,'b':4 ]
assert map.toJson() == '{"a":2,"b":4}'
assert map.toJson().toMap() == map
unfortunately, it's not possible to override the toString() method...
这篇关于Groovy:是不是有一个stringToMap开箱即用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!