Has anyone tried using serialization with Graalvm ...
# serialization
g
Has anyone tried using serialization with Graalvm compiled to a native image?
b
Should not be an issue since it's reclectionless
g
Hmm ok.
I tried adding MyClassName$$serializer explicitly, but it doesn't work.
I get the "Serializer for class is not found" error.
However if I use the form of encode/decode that takes a serializer as first parameter, it seems to work fine.
b
Decompile generated bytecode to get a better sense where exactly compiler plugin places generated serializer
g
which class do you think I should decompile?
b
The one you annotated
g
ok. I'll take a look. I was assuming it was in the *$$serializer.class(es) that are generated.
If you use MyClass.serializer().javaClass.name, it's what you get.
b
Did you try running graalvm diagnostic tool to see what configs it generates for you?
g
I'm new to graalvm. I used --verbose and -H:+ReportExceptionStackTraces.
I was looking for that type of option in the docs, but that's what I came up with. Do you have any additional suggestions?
b
Very neat tool to get you started
g
Yes I saw that. One complication is I'm writing a lambda function, so not sure if I can use it in a lambda JVM environment. But I can certainly write a test case using serialization and run it locally.
b
Yes, you only need to run that piece of code to let the tool detect missing configs
Final executable would still be a native image for your lambdas
g
thanks for the suggestion
b
Good luck!
e
inline fun <reified T> StringFormat.encodeToString
looks up the serializer by name - this JVM-only helper does involve some reflection - while
fun <T> StringFormat.encodeToString
uses the serializer you pass in. I can imagine the former not working on GraalVM but the latter should be fine in all environments as everything is referenced statically
b
In that case tracing agent should sort you out
g
The tracing agent did the trick. For each serializable class two entries need to be added to the reflect-config.json file.
b
Can you share your reflect config for the future explorers here?
g
Here are the needed entries if using the example from https://github.com/Kotlin/kotlinx.serialization:
Copy code
{
  "name":"Project",
  "fields":[{"name":"Companion"}]
},
{
  "name":"Project$Companion",
  "methods":[{"name":"serializer","parameterTypes":[] }]
}
👍 1
Is there any way to configure serialization to not use reflection when on the JVM?
I didn't see anything with a quick search of the guide.
b
Well if you always pass in serializers explicitly ir won't
But you can't enforce that, unfortunately
Well you could always write a detekt rule for this I guess 😀
g
It's probably easier to explicitly pass in serializers than maintaining a reflect-config.json file.
Just makes the code a little less nice.
b
I'd go for reflectionless serialization myself
👍 1
To enforce it you could write a quick gradle task that checks your source files as plain text and fails if it detects an import of that reflection helper function
OR, move all your serialization code to commonMain where reflection is not even available
e
https://github.com/Kotlin/kotlinx.serialization/issues/1125 looks like they intend for it to work "soon", but it's been a year 🤷