Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .toObject with Generic classes in Firestore to get POJO from DocumentSnapshot

In Firebase Realtime Database there is support for deserialising objects using GenericTypeIndicator. Can someone shed light on how to do the same with Firestore ?

I have a document snapshot which I want to convert to a POJO which is parametrised.

This is the error it throws when I try to do that

java.lang.RuntimeException: Could not deserialize object. Class com.thesportsbeing.thesportsbeing.screens.festadmin.eventslist.eventadmin.scoring.MatchScore has generic type parameters, please use GenericTypeIndicator instead. 

The problem is there is no GenericTypeIndicator in the firestore package, it is present in the realtime-database package. Any workarounds ? or something to get things done would be great.

MyClass is similiar to this

public class MyClass<R extends SomeAbstractClass> {
    R someClassWhichExtendsAboveAbstractClass;
}
like image 358
CommandSpace Avatar asked Aug 21 '18 04:08

CommandSpace


1 Answers

Here's a discussion of the issue and workarounds: https://github.com/firebase/firebase-android-sdk/issues/222

Easiest way is to declare a subclass of your generic class:

class MyNonGeneric extends MyClass<R> {}

// Usage
snapshot.toObject(MyNonGeneric.class);

Alternatively need get each top level property of the POJO separately and cast it.

like image 195
Goose Avatar answered Oct 24 '22 05:10

Goose