The new Firebase Android introduced some huge changes ; below the copy of the doc :
[https://firebase.google.com/support/guides/firebase-android] :
Update your Java model objects
As with the 2.x SDK, Firebase Database will automatically convert Java objects that you pass to DatabaseReference.setValue()
into JSON and can read JSON into Java objects using DataSnapshot.getValue()
.
In the new SDK, when reading JSON into a Java object with DataSnapshot.getValue()
, unknown properties in the JSON are now ignored by default so you no longer need @JsonIgnoreExtraProperties(ignoreUnknown=true)
.
To exclude fields/getters when writing a Java object to JSON, the annotation is now called @Exclude
instead of @JsonIgnore
.
BEFORE
@JsonIgnoreExtraProperties(ignoreUnknown=true)
public class ChatMessage {
public String name;
public String message;
@JsonIgnore
public String ignoreThisField;
}
dataSnapshot.getValue(ChatMessage.class)
AFTER
public class ChatMessage {
public String name;
public String message;
@Exclude
public String ignoreThisField;
}
dataSnapshot.getValue(ChatMessage.class)
If there is an extra property in your JSON that is not in your Java class, you will see this warning in the log files:
W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage
You can get rid of this warning by putting an @IgnoreExtraProperties
annotation on your class. If you want Firebase Database to behave as it did in the 2.x SDK and throw an exception if there are unknown properties, you can put a @ThrowOnExtraProperties
annotation on your class.