Is there a recommended pattern for serializable Kotlin classes? One that won’t take days to debug?
In my euphoria with Kotlin I decided to upgrade my plugin settings to Kotlin, before doing any feasibility tests. It has been a trip down tourette’s alley for two very long days.
First, I could not get Kotlin to accept @State annotation no matter what parameters I specified, it kept complaining about syntax.
Giving up, I made ‘dummy’ Java classes that only serve as the interface to
PersistentStateComponent<MyApplicationState> and @State annotation, they get their data from Kotlin classes. To get this to work took a loooong time. Wrestling with runtime errors on failed serialization and de-serialization. In the end it was constructor and visibility issues but were hell to figure out from runtime messages.
After two days of rewrites and trials I finally got persistent state saved and loaded in IDEA IU 15.02 and for fun tried running it on IDEA 14.0. Well, nothing worked and no errors. Kotlin serialization is not supported and no problems reported.
I opted out for implemented my own serialization rather than going back to Java and wrote ~400 lines of tiny Kotlin classes to handle persistence via
PersistentStateComponent<Element> that works down to IDEA 13.1.
In a day had it all working with all setting classes implemented and tested. It is easier to use than annotations with greater control of persisted format. Out of the box basic types including state of other states and collections, sets, maps of these are supported. A couple of lines and any other type can be added. Visibility is not an issue since all access is done by lambdas passed by the class being serialized.