Using kotlinx.serialization from within java is si...
# serialization
h
Using kotlinx.serialization from within java is simple. My libraries export everything needed, so JSONifying data objects defined in kotlin − on a java client depending on my library − is just a matter of getting the serializer and calling
stringify()
. But it's not very pretty with the companion stuff:
JSON serializer = JSON.Companion.getPlain();
... followed by serializing with a KSerializer, which is also accessed through a Companion object:
serializer.stringify(DataObj.Companion.serializer(), dataInst);
So I thought I could hide all of this in my library, in the
toString()
function of the
DataObj
. Using this from java would then be a simple question of
dataInst.toString()
! But alas! The needed code isn't available before the class is compiled, of course. Which is too late for this solution, unfortunately. Does anyone have some other solution to making the code more elegant for java users?