mmorihiro
02/25/2017, 12:21 PMcom.typesafe.config.Config.extract<T>
and Any.toConfig
to convert between Config
and Kotlin Objects.
data class Person(val name: String, val age: Int)
data class Family(val list: List<Person>)
// typesafe config supports not only HOCON but also JSON
// HOCON(Human-Optimized Config Object Notation) is the JSON superset
val config = ConfigFactory.parseString("""
| // HOCON style
|family {
| list = [{
| name = "foo"
| age = 20
| }, {
| name = "bar"
| age = 25
| }]
|}""".trimMargin())
// only typesafe config
val list = config.getList("family.list")
val foo = list[0].atKey("foo").run {
Person(getString("foo.name"), getInt("foo.age"))
}
val bar = list[1].atKey("bar").run {
Person(getString("bar.name"), getInt("bar.age"))
}
Family(listOf(foo, bar))
// typesafe config + config4k
config.extract<Family>("family")