How to ignore new fields for the object model with Firebase 1.0.2

I am using the latest version of the Firebase dependency, which is 1.0.2, and I am having problems getting my pojos to figure it out correctly.

The fact is that at any moment the scheme can change, but I do not want my application to crash with this:

D / AndroidRuntime (14097): shutdown VM W / dalvikvm (14097): threadid = 1: thread output with an uncaught exception (group = 0x40a451f8) E / AndroidRuntime (14097): FATAL EXCEPTION: main E / AndroidRuntime (14097): com. firebase.client.FirebaseException: crash for type selection E / AndroidRuntime (14097): when com.firebase.client.DataSnapshot.getValue (DataSnapshot.java:213)

Looking into the dependency tree, I realized that Firebase uses Jackson mapper 1.9.7, so the @JsonIgnoreProperties(ignoreUnknown = true") annotation is not an option. Moreover, the mapper object is wrapped into this Firebase object, so I cannot configure the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES property DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES ( DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES for Jackson 1.9 and earlier).

Is there a way to set this property both as an annotation at the class level, and when setting up a display device or any other mechanism?

The best solution would be for Firebase 1.0.3 to start using Jackson 2.0, but I don't know if this is really what they care about right now.

Note. I already thought about eliminating the transitive dependency of Jackson 1.9.7 and adding Jackson 2.0 so that I can access this ignoreUnknown function, but I don't think this is a viable choice, since I would change the mayor.

+6
source share
4 answers

Firebase 1.0.3 was released and now uses Jackson 2.2.2, so the @JsonIgnore annotation is the way to go.

Edit: At the moment, in 2017, Firebase no longer uses Jackson. correct @Exclude annotation.

+4
source

For those who have switched to the official version of Google Firebase (as of May 29, 2016), you can use @Exclude instead of @JsonIgnore or @JsonProperty. There is a link to their document.

Example:

 public class dataPacket{ public String data; ... @Exclude public String getData(){return data;} } 
+26
source

According to the accepted answer, Firebase now uses Jackson, so you can annotate the desired methods that you want to ignore with

@JsonIgnore

Edit:

Firebase has changed everything. Woot. Now use this instead:

@Exclude

+13
source

Update:

As others have pointed out, the @Exclude annotation is the right way to use it. But if you use Kotlin, this will not work. To use Kotlin

 @get:Exclude var data: String? = nil //or @set:Exclude var data: String? = nil //or both @set:Exclude @get:Exclude var data: String? = nil 

Because annotation can only be applied to generated fields, not to properties.

Old answer:

I go to Firebase from GSON, I used the transient keyword. And it works with firebase too

 public transient String data; 
+9
source

Source: https://habr.com/ru/post/951309/


All Articles