32

I'd like to persist a Config object (https://github.com/typesafehub/config) as a serialized string(maybe JSON ??) and read it back when required. However, I didnt find any API on the Config api docs that supports. Any help on this is appreciated.

I tried

config.toString

but the result looks like

Config(SimpleConfigObject({...data}))

questionersam
  • 1,115
  • 1
  • 11
  • 24

2 Answers2

69

Try this:

config.root().render(ConfigRenderOptions.concise())
Sasha O
  • 3,710
  • 2
  • 35
  • 45
  • +1, and Java serialization is also supported, if that's better for a given application. – Havoc P Jun 25 '13 at 18:56
  • This also gives everything else besides my config in application.conf. so how can I json pretty print only the config from my application.conf? – user1870400 Mar 30 '18 at 20:32
  • @user1870400 if you really just want `application.conf` you can use `ConfigFactory.parseResourcesAnySyntax("application.conf")` - but this won't substitute any environment variables or system properties. You might look at https://stackoverflow.com/questions/18195527/typesafe-config-load-additional-config-from-path-external-to-packaged-scala-app for some other options. – tilde Dec 22 '20 at 20:09
  • @user1870400 if you wrap everything in your `application.conf` in a top level object `myapp { ... }` you could do something like `ConfigFactory.load().getConfig("myapp").root().render( ... )`. – tilde Dec 22 '20 at 20:24
4

Of course you can also adjust a few parameters, for example:

val renderOptions = ConfigRenderOptions
  .defaults()
  .setOriginComments(false)
  .setComments(false)
  .setFormatted(true)

println(config.root().render(renderOptions))
Twistleton
  • 2,735
  • 5
  • 26
  • 37