Home > Mobile >  Why does @PropertyName fail to automatically serialize maps, while it successfully serializes other
Why does @PropertyName fail to automatically serialize maps, while it successfully serializes other

Time:01-16

ScreenModel.java

public class ScreenModel {

    @PropertyName("TEST_FIELD")
    private HashMap<String, Object> testFieldHashMap;

    public HashMap<String, Object> getTestFieldHashMap() {
        return testFieldHashMap;
    }

}

addSnapshotListener

FirebaseFirestore.getInstance().collection("SCREENS").document(sharedPreferences.getString("documentId", null)).addSnapshotListener((value, error) - > {
if (error != null) return;
    
if (value != null && value.exists()) {
    ScreenModel screenModel = value.toObject(ScreenModel.class);
    if (screenModel.getTestFieldHashMap() == null)
        Log.w("ABC", "111"); // It always print this, why always null?
}});

enter image description here

Why does @PropertyName fail to automatically serialize maps, while it successfully serializes other types?

CodePudding user response:

Isn't about the type of the field, but about the access modifier. You set the field to be private. To solve this, it's necessary to set the field public:

@PropertyName("TEST_FIELD")
public HashMap<String, Object> testFieldHashMap;
//           
  • Related