使用scala/play将HOCON(.conf)转换为JSON?

kor*_*dge 6 scala playframework-2.0 hocon

我想将.conf文件直接转换为json,以便将其传递给前端.有没有办法在scala/play中这样做?我正在采取的路径似乎非常麻烦:

val conf: Configuration = play.api.Configuration.apply(ConfigFactory.parseFile(new File("app/assets/strings.conf")))
conf.entrySet.seq.map(t => t._1 -> t._2.unwrapped())
// which gives me a Seq[(String, AnyRef)] which cannot be converted with Json, so the path from here is even uglier
Run Code Online (Sandbox Code Playgroud)

我很想回到JSON,但HOCON语法非常适合我们的用例.HOCON是basicly JSON用更少的括号和引号-所以转换应该简单的.我还是找不到一个简单的方法来玩play/scala这样的事情.

And*_*ann 22

这样做:

val config = ConfigFactory.load(); // read Config here 

val configJSON : String = 
  config.root().render( ConfigRenderOptions.concise() )
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个JSON字符串.

您还希望输出格式化的其他选项.更多文档:https: //typesafehub.github.io/config/latest/api/com/typesafe/config/ConfigValue.html#render()