Convert HOCON (.conf) to JSON using scala / play?

I want to convert a .conf file directly to json to pass it to frontend. Is there any way to do this in scala / play? This seems incredibly cumbersome along the path I'm taking now:

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 

I am tempted to return to JSON, but the HOCON syntax is perfect for our use case. HOCON is basically JSON with fewer brackets and quotes - so the conversion should be very simple. However, I cannot find an easy way to do something like this with play / scala.

+5
source share
2 answers

This will do:

 val config = ConfigFactory.load(); // read Config here val configJSON : String = config.root().render( ConfigRenderOptions.concise() ) 

This will give you a JSON string.

There are additional options for how you want the format to be formatted. See the documentation for more: https://typesafehub.imtqy.com/config/latest/api/com/typesafe/config/ConfigValue.html#render ()

+18
source

In case someone comes here wondering how to do the same in Java, at least for play 2.2.x you can do the following:

 config.underlying().root().render(ConfigRenderOptions.concise()); 
0
source

Source: https://habr.com/ru/post/1207973/


All Articles