Kotlin multiplatform SqlDelight + Kotlinx-Serializ...
# squarelibraries
e
Kotlin multiplatform SqlDelight + Kotlinx-Serialization Has anyone tried to use SqlDelight with Kotlinx-serialization to parse the entities in JSON format, then, store them to the database? The JSON and the database table can be represented with the same model which is the one generated by SqlDelight. Trying to use the external serializer feature of Kotlinx-serialization but it doesn't look like it works well with
null
attributes specially that the JSON does NOT include the keys with null values like
"someAtt": null
. We're open to any suggestion on how to handle this considering that we have 40+ tables and we prefer if we don't duplicate the model or write the serializers ourselves.
n
can you share some code on the attempts that you have made? so far what you are trying to achieve is unclear to me.. what you would od in this case is use a custom class in the sqldelight definitions and make sure to assign a adapter.. which can then call serialization code is there a specific reason why this is impossible ?
e
Trying to reuse the same models generated by SqlDelight for Json parsing. Currently, I have two models
User.Impl
and
@Serializable UserJson(..): User
. Trying to see if the external serializer feature would allow me to to reuse the same model. The solution would be
@Serializer(forClass: User.Impl::class) object UserSerializer
n
if sqldelight does nto allow it yet.. maybe open a issue to request defining annotations on the class and properties ? that could be useful for more than just serialization.. and i don't think it would be very complicated to implement in the code generation
e
Annotations are a bit invasive for SQLDelight but will open a feature request there. Also opened a kotlinx-serialization feature request here
n
then it seems more like sqldelight should have the correct default values set on the generated code
e
It doesn't always work because of ColumnAdapters... It would be impossible to convert the
DEFAULT
value in the
CREATE TABLE
statement to a custom type like
Date
or
enums
n
ah right.. then the default value would need to call the column adapter.. which is not a option
👍 1
e
It doesn't always work because of ColumnAdapters... You cannot use the
DEFAULT
value to build custom types like
Date
or
enums
n
but if a custom type is nullable and you want null as a default value that should not be a issue right ?
its not like serialization could magically figure out what values you want either..
e
yeah, right if it is nullable without a default value then yes, shouldn't be an issue
n
or add
DEFAULT null
to the sql
355 Views