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

前端 未结 7 533
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 19:57

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

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Databas         


        
相关标签:
7条回答
  • 2021-01-07 20:25

    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
    
    0 讨论(0)
  • 2021-01-07 20:26

    Here's how this was resolved for me. The issue occurred when I was trying to update an int field in database. I was getting text from edit text field and forgot to convert that into an int. This caused the failure. So try checking the data type you're sending to the database if this issue occurs.

    0 讨论(0)
  • 2021-01-07 20:32

    I had a similar problem, I was trying to use groovy instead of Java, I guess that groovy is generating other get methods for me. I converted my domain class from Pogo (src/main/groovy) to Pojo (src/main/java) and now all is working well

    0 讨论(0)
  • 2021-01-07 20:35

    In case this helps others that are receiving the same error, I was getting the same error and the solution ended up being the following for me:

    Changing:

    databaseReference.child("somechild").setValue(etName.getText());
    

    To:

    databaseReference.child("somechild").setValue(etName.getText().toString());
    

    As @fraggjkee points out, Firebase attempts to serialize getters, and this was the similar issue here with firebase attempting to serialize the result of the .getText().

    Hope this helps others!

    0 讨论(0)
  • 2021-01-07 20:43

    Add @Exclude to the functions that FB tries to interpret, like those that return a Drawable.

    @Exclude
    public Drawable getUnitColorImage(){
        return TextDrawable.builder().buildRound("", prefColor);
    }
    
    0 讨论(0)
  • 2021-01-07 20:47

    Add @IgnoreExtraProperties to your class

    @IgnoreExtraProperties
    public class YourPojo {
        public String name;
    
        // Default constructor for Firebase
        public YourPojo(){} 
    
        // Normal constructor
        public YourPojo(String name) {
            this.name = name;   
        }
    }
    
    0 讨论(0)
提交回复
热议问题