I'm building a Kotlin server application. I know that I will need a specific Json configuration such as Json { ignoreUnknownKeys = true }
throughout.
My question is whether it makes more sense to declare a top-level var
(or maybe an object
with the config as a property) and use that everywhere, or whether to create a new Json {}
within each function.
I guess my concern with using a singleton is a tie-up in the case when I have multiple simultaneous requests coming in all of which need to use the config. I don't know enough about JVM internals or the kotlinx architecture to know if that is an issue. Meanwhile, creating a new config within every function seems highly inefficient, creating new objects every time.
// which to choose?
// top-level val
val myJson : Json = Json { ignoreUnknownKeys = true }
// top-level object with property
object myJConfig {
val myJson : Json = { ignoreUnknownKeys = true }
}
// create a new config within a function
class myClass {
fun myFunc() {
val myJson : Json = { ignoreUnknownKeys = true }
... rest of function here ...
}
}