0

Firebase documentation for android reads:

The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized

Why are getters neccessary to assign properties in deserialization?

prompteus
  • 1,051
  • 11
  • 26

1 Answers1

1

firebaser here

The JSON serializer/deserializer in the Firebase Android SDK builds a list of candidate properties for a class based on its public fields and its JavaBean-style pseudo-properties that have a getter and a setter.

We've discussed whether the latter should be based solely on a getter for serializing to JSON and a setter for deserializing from JSON. But at this moment that would be a breaking change to the behavior, which we're not willing to do.

If you'd like broader support over the serialization/deserialization you can always use Jackson to do so. See my answer here: How to deserialise a subclass in Firebase using getValue(Subclass.class)

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 'list of candidate properties for a class based on its public fields and its JavaBean-style pseudo-properties that have a getter and a setter.' So they don't need a getter as long as they are public? – prompteus Jan 16 '17 at 21:16
  • public fields without a getter and setter are also serialized/deserialized. It's the simplest way to get data into/out of the database. – Frank van Puffelen Jan 16 '17 at 21:24
  • Ok, isn't the documentation kind of misleading then? – prompteus Jan 16 '17 at 21:28
  • I'm not entirely sure what section of the docs we're talking about here. But if you think it could/should be improved, there is a FEEDBACK link at the top right of the page. Be sure to select the section of the page that you're asking about, as the JavaDocs tend to be really long and hard to navigate. – Frank van Puffelen Jan 16 '17 at 22:27
  • I think I've got it. The getters are not neccessary to get properties assigned in deserialization, but since we expect the class instances to be both serialized and deserialized, it's recommended to use them. – prompteus Jan 17 '17 at 18:50