Taha Sami
Taha Sami

Reputation: 1697

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

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?

Upvotes: 0

Views: 36

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138969

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;
//👆

Now, the annotation will take into account both the field name as well as the getter when serializing.

Upvotes: 1

Related Questions