Given a nested JSON as configuration, like:
给定嵌套的JSON作为配置,如:
{
app: {
id: "app1"
instances: 2,
servers: [
{ host: "farm1.myco.com", port: 9876 }
{ host: "farm2.myco.com", port: 9876 }
]
}
}
When using typeSafe config, is it possible to address elements of an array directly in a path?
使用typeSafe配置时,是否可以直接在路径中寻址数组的元素?
At the moment we need to do something like the following, which is kind of verbose:
目前我们需要做类似以下的事情,这有点冗长:
val servers = config.getObjectList("app.servers")
val server = servers.get(0).toConfig
val host = server.getString("host")
val port = server.getInt("port")
Something like this would be ideal:
这样的事情是理想的:
val host = config.getString("app.servers.0.host")
?
val host = config.getString(“app.servers.0.host”)?
Does the TypeSafe API supports something like this?
TypeSafe API是否支持这样的内容?
2 个解决方案
#1
3
This seems not possible so far.
See https://github.com/typesafehub/config/issues/30
到目前为止这似乎是不可能的。请参阅https://github.com/typesafehub/config/issues/30
#2
0
Implement a helper function that transforms the object into something a little more manageable. This is what I ended up using.
实现一个帮助函数,将对象转换为更易于管理的东西。这就是我最终使用的。
def toList(jList:java.util.List[_ <: ConfigObject]) : List[Config] = {
val l = jList.asScala.toList
val slConfig = l.map(item => {
item.toConfig
})
slConfig
}
The use of that function would look like
使用该功能看起来像
val servers = toList(objectList)
for(server <- servers) {
println(server.getString("host"))
println(server.getString("port"))
}
#1
3
This seems not possible so far.
See https://github.com/typesafehub/config/issues/30
到目前为止这似乎是不可能的。请参阅https://github.com/typesafehub/config/issues/30
#2
0
Implement a helper function that transforms the object into something a little more manageable. This is what I ended up using.
实现一个帮助函数,将对象转换为更易于管理的东西。这就是我最终使用的。
def toList(jList:java.util.List[_ <: ConfigObject]) : List[Config] = {
val l = jList.asScala.toList
val slConfig = l.map(item => {
item.toConfig
})
slConfig
}
The use of that function would look like
使用该功能看起来像
val servers = toList(objectList)
for(server <- servers) {
println(server.getString("host"))
println(server.getString("port"))
}