https://kotlinlang.org logo
#ktor
Title
# ktor
j

Jgafner

08/04/2021, 5:51 PM
Hi Is it possible to declare a list of Json objects in the config file and read it with the config.property? we have something like this:
Copy code
key : [
{
  aKey: aValue
},
{
  bKey: bValue
}
]
....
IJ suggesting me one of those but I am not sure which one to use
a

Aleksei Tirman [JB]

08/05/2021, 8:34 AM
Copy code
val objects = environment.config.configList("key")
println(objects[0].property("aKey").getString())
println(objects[1].property("bKey").getString())
j

Jgafner

08/05/2021, 10:20 AM
@Aleksei Tirman [JB] thanks worked Another related question: is there a way to get the key value (in my example the aKey and the bKey ) ?
a

Aleksei Tirman [JB]

08/05/2021, 10:21 AM
Do you mean to get keys of all objects in an array?
j

Jgafner

08/05/2021, 10:46 AM
I mean to be able to old the objects as a map [“aKey”:“aValue”,….]
a

Aleksei Tirman [JB]

08/05/2021, 10:50 AM
You can iterate over a list of objects and then get their keys using keys() method but this functionality will be available only in Ktor 2.0.0.
As a workaround, you use the solution described here.
j

Jgafner

08/05/2021, 10:55 AM
thanks but I was trying to avoid the use of ConfigurationFactory since I am passing the ktor config as a parameter for tests purpose , I will hack it with hardcoded value for now and will wait for Ktor 2.0.0 Thanks a lot
c

csieflyman

08/05/2021, 11:02 AM
@Jgafner You can use Config4k to convert application.conf to the kotlin data class and pass it as parameter https://github.com/config4k/config4k
j

Jgafner

08/05/2021, 11:04 AM
thanks @csieflyman will take a look
j

Jgafner

08/05/2021, 11:14 AM
thanks 🙏
26 Views