fraggjkee fraggjkee - 2 months ago 38
Java Question

com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead

I am trying to persist a custom object using the following code:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference curWorkoutExercisesRef = databaseReference.child("workouts")
.child(mCurrentWorkout.getId())
.child("workoutExercises");

WorkoutExercise we = new WorkoutExercise(exercise);
curWorkoutExercisesRef.push().setValue(we);


Here's my object:

public class WorkoutExercise {

private String id;
private Exercise exercise;

public WorkoutExercise() {}

// getters, setters, other methods
// ...
}

public class Exercise {

private String id;
private String title;

private List<BodyPart> bodyParts;

public Exercise() {}

// getters, setters, other methods
// ...
}

public class BodyPart {

private String id;
private String name;

public BodyPart() {}

// getters, setters, other methods
// ...
}


And every time I am getting this error -
com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
. My objects don't contain any arrays so this error looks quite confusing. I found a way to fix this error - everything works fine if I add the
@Exclude
annotation to the
bodyParts
list from my
Exercise
class but it is obviously not what I am trying to achieve.

So seems that Firebase is unable to persist objects containing inner lists? Are there any straightforward workarounds or best practices here? Thanks!

P.S. I am using
firebase-database:9.2.1

Answer

I've managed to find the reason causing this crash. I installed the app on another device running Android 5.x and Firebase threw a much less confusing exception there:

com.google.firebase.database.DatabaseException: No properties to serialize found on class android.graphics.Paint$Align

It appeared that Firebase (unlike Gson) tries to serialize all possible getters even though they don't have direct connection with global variables (class members / fields), in my particular case one of my objects contained a method returning Drawable - getDrawable(). Obviously, Firebase doesn't know how to convert drawable into json.

Interesting moment (probably a bug in Firebase SDK): on my older device running Android 4.4.1 I still get my original exception in the same project and configuration:

Caused by: com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead