6

I'm using autovalue for my entities and annotated them to allow json parsing.

There are new annotations in the new sdk: Exclude, IgnoreExtraProperties, ThrowOnExtraProperties and @PropertyName: https://firebase.google.com/docs/reference/android/com/google/firebase/database/PropertyName. But PropertyName seems to be missing from the sdk..

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vinay Nagaraj
  • 1,162
  • 14
  • 26

2 Answers2

6

We missed the @PropertyName annotation in this release of the Firebase SDK for Android, but it was included in a release shortly after.

See this answer for a way to use Jackson explicitly with any version of the Firebase SDK: How to deserialise a subclass in Firebase using getValue(Subclass.class)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Will i be fine if i stick to jackson's `@JsonProperty ` annotation till then? – Vinay Nagaraj May 19 '16 at 19:00
  • Yes. Once we introduce the property I recommend you switch over, since it'll significantly reduce the size of your apk. – Frank van Puffelen May 20 '16 at 01:09
  • 1
    I included compile jackson-annotations:2.7.4 and jackson-databind:2.7.4, but it looks like firebase is ignoring both `@JsonProperty` and `@JsonSerialize` annotations. – Vinay Nagaraj May 20 '16 at 12:48
  • You'll have to serialize it to a `Map` yourself using `new ObjectMapper().converValue(pojoValue, Map.class);` and then pass that map into `setValue()` or `updateChildren()`. – Frank van Puffelen May 20 '16 at 14:16
  • January 2020 and this issue is still not fixed! though the property name WAS included in the code of 21.3.1, it is buggy and simply does not work! – Lena Bru Jan 06 '20 at 13:21
  • @LenaBru This was fixed years ago. See https://firebase.google.com/docs/reference/android/com/google/firebase/database/PropertyName – Frank van Puffelen Jan 06 '20 at 15:13
  • I spent some time on this this morning, with the latest version, it ignores properties which cannot be legal members. I.e property named "find-my-property" cannot be mapped to findMyProperty because it has "-" in it, and is simply ignored, on version 21.3.1 – Lena Bru Jan 06 '20 at 17:13
  • This was about the annotation simply not existing, while you seem to have problems making it work for your class. Please open a new question for that, as it seems like a new issue. – Frank van Puffelen Jan 06 '20 at 18:00
4
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)

Refer to https://firebase.google.com/support/guides/firebase-android#update_your_java_model_objects_numbered