如何使用 jsonbuilder 制作一个 json,它有一个名为 DefaultGroovyMethods 方法(身份)的键?
How to use jsonbuilder to make a json which has a key named like a DefaultGroovyMethods method (identity)?
def builder = new groovy.json.JsonBuilder()
def root = builder.auth {
identity {
methods (['password'])
password {
user {
name {
usrName
domain {
id usrDomain
}
password "openstack"
}
}
}
scope {
project {
name prjName
domain {
id 'default'
}
}
}
}
}
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
断言输出:
Assertion failed:
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
| | |
| | false
| '{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}
基本上 "identity" 键被忽略了。
我看到有一个名为 "identity" 的方法属于 DefaultGroovyMethods,但我不知道如何克服它。
第一次写Groovy,有点背景Python。感谢任何帮助!
使用 Szymon Stepniak 的回答完成的完整解决方案(以及对 json 声明的其他更正):
def builder = new groovy.json.JsonBuilder()
builder auth: [
identity: {
methods(['password'])
password {
user {
name "$usrName"
domain {
id usrDomain
}
password "openstack"
}
}
},
scope: {
project {
name prjName
domain {
id 'default'
}
}
}
]
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
以防万一有人想跟随这个例子。
要克服此限制(传递给 JsonBuilder.call()
方法的闭包解析委托链中的 identity
方法),您需要将 JSON 文档表示为地图或在至少使用映射到 identity
键,从这个地方你可以使用闭包。
def builder = new groovy.json.JsonBuilder()
builder auth: [
identity: {
methods (['password'])
password {
user {
name {
usrName
domain {
id usrDomain
}
password "openstack"
}
}
}
scope {
project {
name prjName
domain {
id 'default'
}
}
}
}
]
此外,请记住,您定义的闭包不会生成预期的 JSON 字符串,但它会包含闭包示例中缺少的 identity
键。
def builder = new groovy.json.JsonBuilder()
def root = builder.auth {
identity {
methods (['password'])
password {
user {
name {
usrName
domain {
id usrDomain
}
password "openstack"
}
}
}
scope {
project {
name prjName
domain {
id 'default'
}
}
}
}
}
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
断言输出:
Assertion failed:
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
| | |
| | false
| '{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}
基本上 "identity" 键被忽略了。 我看到有一个名为 "identity" 的方法属于 DefaultGroovyMethods,但我不知道如何克服它。 第一次写Groovy,有点背景Python。感谢任何帮助!
使用 Szymon Stepniak 的回答完成的完整解决方案(以及对 json 声明的其他更正):
def builder = new groovy.json.JsonBuilder()
builder auth: [
identity: {
methods(['password'])
password {
user {
name "$usrName"
domain {
id usrDomain
}
password "openstack"
}
}
},
scope: {
project {
name prjName
domain {
id 'default'
}
}
}
]
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
以防万一有人想跟随这个例子。
要克服此限制(传递给 JsonBuilder.call()
方法的闭包解析委托链中的 identity
方法),您需要将 JSON 文档表示为地图或在至少使用映射到 identity
键,从这个地方你可以使用闭包。
def builder = new groovy.json.JsonBuilder()
builder auth: [
identity: {
methods (['password'])
password {
user {
name {
usrName
domain {
id usrDomain
}
password "openstack"
}
}
}
scope {
project {
name prjName
domain {
id 'default'
}
}
}
}
]
此外,请记住,您定义的闭包不会生成预期的 JSON 字符串,但它会包含闭包示例中缺少的 identity
键。