Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Json serialization to map firebase response #30

Merged
merged 8 commits into from
Sep 16, 2022

Conversation

pablobaldez
Copy link
Member

The problem

Firebase is awesome, but its SDK sucks. We need to fetch info from Firestore, but there is no "easy" way to serialize the response. The current approach will force us to map property by property, which can be painful for complex structures (such as, a list of confrontations).

The solution

I can convert the firebase response to a Map<String, Any?> so my first try was to use the Kotlin serialization properties, a library to serialize maps into custom data classes, exactly what we need. The problem is this library also sucks because it has a bug and it's not serializing the response properly (I opened an issue for them).
So after some hours of thinking I found a solution, using the kotlin-serialization-json for that. What I'm doing is:

  1. get the map response from firebase
  2. transform this map manually into a JsonObject (I'm not covering all supported types from Firebase yet)
  3. use the kotlin library to serialize the JsonObject into a data class

It's not an optimized solution, but at least we can avoid repeating unnecessary code.
Unfortunelly, I was not able to find a solution to convert a custom data class into a map (to write things on firebase). The kotlin serialization json can't be used for that

@pablobaldez pablobaldez added the enhancement New feature or request label Sep 15, 2022
@pablobaldez pablobaldez self-assigned this Sep 15, 2022
Base automatically changed from feature/confrontations-list to main September 16, 2022 16:21
@pablobaldez pablobaldez merged commit 427eac4 into main Sep 16, 2022
@pablobaldez pablobaldez deleted the feature/json-serialization branch September 16, 2022 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants