I'm looking into generating kotlin code from JSON ...
# getting-started
b
I'm looking into generating kotlin code from JSON schema files (custom stuff); do I want something other than a string templating library?
✅ 1
j
I would usually reach for Kotlin Poet
☝🏻 1
b
thank you
j
It has type-safe types and functions to generate Kotlin code, it makes things much easier
h
I implemented an OpenAPI to Kotlin generator, but unfortunately, I didn't refactor the JSON schema stuff into its own artifact, but maybe you could reuse something (or create a PR :) https://github.com/hfhbd/kfx
b
I've looked into github.com/OptimumCode/json-schema-validator but decided to handroll my own schema classes since I'll only need a subset
I've got a stupid JS API which defines data structures much like Django models. It's impossible to model in Kotlin (nor TypeScript) so I've settled on generating interfaces and typesafe builders
j
Can't you model those with sealed classes? Usually anything that has weird "alternatives" can be modeled this way. But then you also have to have value-aware serializers (at least if you intend to use kotlinx.serialization), like something that looks at the value of one or two fields to determine which type to deserialize to.
b
no, because the structure of those definitions also informs a Partial<Obj> that is derived from it
plus it adds property onto the class itself that work differently depending on whether it is updated or accessed
e.g. SchemaField("folder", ForeignKeyField(Folder)) adds obj.folder.name and obj.update({folder: "id"})
so the whole Partial<T> type is it's own can of worms that Kotlin doesn't supported, but even if it did, you'd still be left with the difference of obj.prop being an object and the partial only containing a string id field
so my idea is to generate interfaces that the fat model can implement for accessing stuff and type safe builders that spit out the correct JSON object required for the update() method