I am writing scripting host, but can’t find a api to add compiler plugin.
How to add one like kotlinx.serialization
or kapt.
Is it able to config custom plugin using annotation like @file:DependsOn
I am writing scripting host, but can’t find a api to add compiler plugin.
How to add one like kotlinx.serialization
or kapt.
Is it able to config custom plugin using annotation like @file:DependsOn
First, you should define know what kind of plugin you need
gradle
plugin to work with gradle (the easiest and most flexible)kotlin compiler
plugin to work with the compilerEvery one of these has its own manual and can be searched by its name.
But the thing is, you must first determine what you want to do and where
you want it to be done. (And if it is worth it or not)
I say compiler plugin
or kotlin compiler plugin
.
And my question is about kotlin scripting
, I can’t find any doc about that.
Any solution so far? For Kotlin scripting
Here some workarounds https://youtrack.jetbrains.com/issue/KT-47384
Please vote for it too
Offtopic
In my case I switched to Jackson for serialization.
Example:
#!/usr/bin/env kotlin
@file:DependsOn("com.fasterxml.jackson.core:jackson-databind:2.17.2")
@file:DependsOn("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.2")
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
data class User(val name: String, val age: Int, val email: String)
val jsonString = """{"name":"John Doe","age":30,"email":"johndoe@example.com"}"""
val mapper = jacksonObjectMapper().apply {
registerKotlinModule()
}
// Deserialize JSON to a Kotlin object
val user: User = mapper.readValue(jsonString)
println("Deserialized: $user")
// Serialize Kotlin object to JSON
val serializedUser = mapper.writeValueAsString(user)
println("Serialized: $serializedUser")