, 14 tweets, 2 min read Read on Twitter
i've not found any decent serialization approach for kotlin that doesn't basically devolve to writing an external serializer -- meaning that all serialized values must be externally visible and there must be a constructor to put the object back together.
of course, by the time you've done that, the serialization code is doing nothing for you. that is, one needs only ObjectInputStream and ObjectOutputStream.
it's possible, always, of course, that i'm just too dumb to see the way forward.
the two basic approaches are to piggyback on to java.io.serializable or to try kotlinx.serialize.
the problems all arise when you want custom serialization. if your classes were all very simple, essentially data objects with nothing clever, you could probably get away with either approach.
one of the great powers of kotlin, tho, is its ability to express complex inter-property relationships very simply. this matters a lot when you're doing javafx, for instance. you can say "class A(x:Int) { val xProperty = SimpleIntegerProperty(x) ... var x by xProperty }"
(not out of the box, but using tornadofx, or using any clever thing like that.)
serializing that using one of the wire protocols -- as far as i can make out this is really all that kotlinx.serialize will do -- will mean serializing a bizarre nested structure full of extra fields that have no significance but add time/space to the output.
workaround: derive off of either kotlin's serializable or java's serializable. whoops on both counts...
java's serializable requires an internal readObject() member. you can't change an ordinary val using it, so extend my example to add a simple val field, and the readObject fails: you can't set the value of a val field after construction.
kotlin's serializer does this, but it also doesn't do the graphs, only the trees.
now, i can *force* either of these answers. that is, code works for me, i don't work for it, and i know there's a way to make my objects use either model successfully.

*but*. what, then, is the model giving me? why i am bothering, if i have to write tons of custom code anyway?
what do i get for writing all that custom code internally to those classes to make it fit either of the serialization models?

i get massive solution sprawl and a bunch of annotations that tell the geek nothing.
so? what's my fallback? well. as i say, i'll use external serialization everywhere, base it against ObjectInputStream and ObjectOutputStream, and make sure I can init new objects on the loads.
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to GeePaw Hill
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!