scala Hocon:从配置文件中读取对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17196166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hocon: Read an array of objects from a configuration file
提问by Sonson123
I have created an Play application (2.1) which uses the configuration in conf/application.confin the Hocon format.
我已经创建了使用配置在播放应用(2.1)conf/application.conf在Hocon格式。
I want to add an array of projects in the configuration. The file conf/application.conflooks like this:
我想在配置中添加一系列项目。该文件conf/application.conf如下所示:
...
projects = [
{name: "SO", url: "http://stackoverflow.com/"},
{name: "google", url: "http://google.com"}
]
I try to read this configuration in my Scala project:
我尝试在我的 Scala 项目中读取此配置:
import scala.collection.JavaConversions._
case class Project(name: String, url: String)
val projectList: List[Project] =
Play.maybeApplication.map{x =>
val simpleConfig = x.configration.getObjectList("projects").map{y =>
y.toList.map{z =>
Project(z.get("name").toString, z.get("url").toString) // ?!? doesn't work
...
}}}}}}}} // *arg*
This approach seems to be very complicated, I am lost in a lot of Options, and my Eclipse IDE cannot give me any hints about the classes.
这种方法似乎很复杂,我迷失在很多Options.
Has anybody an example how you can read an array of objects from a Hocon configuration file? Or should I use for this a JSON-file with an JSON-parser instead of Hocon?
有没有人举例说明如何从 Hocon 配置文件中读取对象数组?或者我应该为此使用带有 JSON 解析器的 JSON 文件而不是 Hocon?
采纳答案by Andy MacKinlay
The following works for me in Play 2.1.2 (I don't have a .maybeApplicationon my play.Playobject though, and I'm not sure why you do):
以下在 Play 2.1.2 中对我有用(不过.maybeApplication我的play.Play对象上没有,我不确定你为什么这样做):
import play.Play
import scala.collection.JavaConversions._
case class Project(name: String, url: String)
val projectList: List[Project] = {
val projs = Play.application.configuration.getConfigList("projects") map { p =>
Project(p.getString("name"), p.getString("url")) }
projs.toList
}
println(projectList)
Giving output:
给出输出:
List(Project(SO,http://stackoverflow.com/), Project(google,http://google.com))
There's not a whole lot different, although I don't get lost in a whole lot of Optioninstances either (again, different from the API you seem to have).
没有太多不同,尽管我也不会在很多情况Option下迷失方向(同样,与您似乎拥有的 API 不同)。
More importantly, getConfigListseems to be a closer match for what you want to do, since it returns List[play.Configuration], which enables you to specify types on retrieval instead of resorting to casts or .toString()calls.
更重要的是,它getConfigList似乎更符合您想要做的事情,因为它返回List[play.Configuration],这使您可以在检索时指定类型,而不是求助于强制转换或.toString()调用。
回答by flurdy
If a normal HOCON configuration then similar to strangefeatures answer this will work
如果一个正常的 HOCON 配置然后类似于奇怪的功能回答这将起作用
import javax.inject._
import play.api.Configuration
trait Barfoo {
def configuration: Configuration
def projects = for {
projectsFound <- configuration.getConfigList("projects").toList
projectConfig <- projectsFound
name <- projectConfig.getString("name").toList
url <- projectConfig.getString("url").toList
} yield Project(name,url)
}
class Foobar @Inject() (val configuration: Configuration) extends Barfoo
(Using Play 2.4+ Injection)
(使用 Play 2.4+ 注入)
回答by cmbaxter
What are you trying to accomplish with this part y.toList.map{z =>? If you want a collection of Projectas the result, why not just do:
你想用这部分完成y.toList.map{z =>什么?如果你想要一个集合Project作为结果,为什么不这样做:
val simpleConfig = x.configration.getObjectList("projects").map{y =>
Project(y.get("name").toString, y.get("url").toString)
}
In this case, the mapoperation should be taking instances of ConfigObjectwhich is what yis. That seems to be all you need to get your Projectinstances, so I'm not sure why you are toListing that ConfigObject(which is a Map) into a List of Tuple2and then further mapping that again.
在这种情况下,map操作应该考虑ConfigObject哪些实例是什么y。这似乎是您获取Project实例所需的全部内容,所以我不确定您为什么要将toList其ConfigObject(即 a Map)放入 List of 中Tuple2,然后再次进一步映射。
回答by Pere Villega
Given that the contents of the array are Json and you have a case class, you could try to use the Json Play APIand work with the objects in that way. The Inceptionpart should make it trivial.
鉴于数组的内容是 Json 并且您有一个 case 类,您可以尝试使用Json Play API并以这种方式处理对象。在成立之初部分应使琐碎。

