Support for Kotlin Serialization?
Hello there, and thanks for kscript!
I would like to request the addition of support for Kotlin Serialization. Since it needs an extra plug-in during compilation, I couldn't see any way to use it when using kscript.
Couldn't this be configured with an option parameter? Either for the compiler or runtime. kscript supports both, see the examples under https://github.com/holgerbrandl/kscript#annotation-driven-script-configuration
Interesting! You are right -- on a closer look, I could get it to work, although it's a bit of a hack since I need to hardcode a path to the jar.
#!/usr/bin/env kscript
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0", "org.jetbrains.kotlin:kotlin-maven-serialization:1.3.72")
@file:CompilerOpts("-Xplugin=/home/knuckles/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.3.72/kotlin-maven-serialization-1.3.72.jar")
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
data class Data(val a: Int, val b: String = "42")
fun main() {
// Json also has .Default configuration which provides more reasonable settings,
// but is subject to change in future versions
val json = Json(JsonConfiguration.Stable)
// serializing objects
val jsonData = json.stringify(Data.serializer(), Data(42))
// serializing lists
println(jsonData) // {"a": 42, "b": "42"}
// parsing data back
val obj = json.parse(Data.serializer(), """{"a":42}""") // b is optional since it has default value
println(obj) // Data(a=42, b="42")
}
main()
Is there a way to make this a bit more portable?
No clue. I've forwarded the question to the experts https://kotlinlang.slack.com/archives/C7A1U5PTM/p1593722985324800
This doesn't seem to work anymore: warning: the following compiler arguments are ignored on script compilation: -Xplugin (also CompilerOpts isn't available, seems to have been renamed to CompilerOptions)
Interesting! You are right -- on a closer look, I could get it to work, although it's a bit of a hack since I need to hardcode a path to the jar. [...] Is there a way to make this a bit more portable?
I managed to get this approach to work, thanks! I have one question, though. Since I'm hoping to make this script portable, I don't really want to hard-code my own home path. Is there any way to interpolate the user's home path in the annotation string?
The following actually does work:
@file:CompilerOpts("-Xplugin=$HOME/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.7.20/kotlin-maven-serialization-1.7.20.jar")
However, it makes IntelliJ really angry:

I also tried "-Xplugin=\$HOME/.m2...", "-Xplugin={{HOME}}/.m2..." (according to the README, that works with @file:Repository) and multiple other random things, but none of them work :(
Has anyone figured out to make this work without infuriating IntelliJ?
Has anyone figured out to make this work without infuriating IntelliJ?
I know it's pretty late, but I found that you could use $(echo ~), like so:
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0", "org.jetbrains.kotlin:kotlin-maven-serialization:1.9.0")
@file:CompilerOptions("-Xplugin=$(echo ~)/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.9.0/kotlin-maven-serialization-1.9.0.jar")
It also works with --package while $HOME breaks that.