I'm converting some existing Groovy/Gradle scripts to Kotlin DSL and found a bit of a gap in Kotlin's capability when it comes to reading arbitrary JSON.
In these Groovy scripts, an arbitrary JSON structure is read from a file using the wonderfully named JsonSlurper and accessed via 'dot' path.
e.g.
Copy code
val buildConfig : Any? = JsonSlurper().parseText(buildConfigFile.readText())
val defaultInstallDir = buildConfig.SetupDefaults.InstallDir // Not compiling as Kotlin compiler doesn't generate dynamic access code
I understand this kind of dynamicism is exactly where Groovy excels (and suffers in equal measure), where Kotlin is meant to be more type safe.
g
gildor
08/27/2019, 5:25 AM
Maybe better to extract this parsing to buildSrc plugin and use proper type-safe deserializer?
Because JsonSlurper is really bad when you want to use it with static types, you have to cast each element manually