Is there a way to re-use the kotlin serialization ...
# announcements
h
Is there a way to re-use the kotlin serialization feature to deserialize an object from a Map<String,Any?> ? Since I'm using data classes I can't use delegated properties.
Deserializing Json is also "just" mapping key:value pairs to object properties, so my guess is there should be a way to use a Map as a source.
n
can you convert the Any in there to Strings ? then yes you can probably deserialize those but without more info you will not get around making some kind of instanceof check and when/switch statements
h
My intention is to create the objects from a map already containing the (matching) native types (e.g. a number is already an Int/Double etc.) or other serializable objects. If anything does not match, an exception is fine.
Basically I want to convert Google Firestore documents to Kotlin objects that are also immutable (data) classes. So I can't have the no-arg constructor Firebase wants to map documents to objects.
c
You can use kotlin no-arg plugin, it will generate a synthetic no-arg constructor which should allow Firebase to do its thing.
h
For future reference: I managed to do what I want by leveraging the automatically created serializers of
@Serializable
objects with a custom
Decoder
. While this is not strictly serialization/deserialization but rather a transform it could be very useful for a variety of further uses. If there is interest, I could clean it up and put it on github