Firebase database android getter for property - android

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?

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)

Related

Parcelize complains "Parcelable should be a class" on objects and enums

When I try to annotate an enum class or object with #Parcelize, it results in the error 'Parcelable' should be a class, both as an editor hint and as a compile failure. I can #Parcelize classes just fine, but I can't do things like
#Parcelize object MySingletion : Parcelable
#Parcelize enum class Direction : Parcelable { N, E, W, S }
This happens even though the Kotlin website explicitly states that objects and enums are supported. Is there a way to fix this so that I can #Parcelize these types of classes? And ideally, is there a solution that doesn't involve manually coding the parceler logic?
Since Kotlin 1.2.60, the CHANGELOG states that Parcelize works with object and enum types.
The documented support means, that objects and enums are properly handled when used as properties on the class being parcelized. More importantly both types are implicitly excluded from the usage, as the fields have to be properties defined within the primary constructor:
#Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, #Parcelize can't be applied if some of the primary constructor parameters are not properties.
If you need to use your objects or enums as a property only, there's no issue with it. If you want to use it as a Parcelable, you can't get around implementing the interface by yourself, since both types are a kind of a singleton implementation and #Parcelize only supports types with accessible constructors with properties.

Parsing Json dynamically.

I am working on json currently in android, but now i follow getter and setter methods to parse json. For example. if there are 5 fields in json string, so create 5 getter and setter methods in one class and accordingly parse the string.
But now in future if one more field increases in json string, then again i need to add one more getter and setter method in that class.
So my question is can we parse json without getter setter, without any class? so that if json changes in future it can be handled well without adding new getter and setter method.
Please let me know the dynamic way to handle json.
you can use these web site http://www.jsonschema2pojo.org/
for creating a class if in future any field increase just put the json in that it will automatically create a class for you .

Does Realm models actually require getters and setters?

I cannot find it clearly documented anywhere if getters and setters are actually required for fields in a Realm Model. For example, the documentation at https://realm.io/docs/java/latest/api/io/realm/RealmObject.html says
The only restriction a RealmObject has is that fields are not allowed
to be final, transient' or volatile. Any method as well as public
fields are allowed. When providing custom constructors, a public
constructor with no arguments must be declared and be empty.
Fields annotated with Ignore don't have these restrictions and don't
require either a getter or setter.
Which seems to hint that it is required with getters and setters for non-ignored fields. Yet, the documentation at https://realm.io/docs/java/latest/#customizing-objects says
It is possible to use RealmObjects almost like POJOs. Extending from
RealmObject, you can let the fields be public, and use simple
assignments instead of setters and getter.
and then show the code for a Realm Model that does not have any getters and setters and instead have public fields we should use. Really? I thought Realm didn't even store any values in the actual fields, so reading and writing from them is probably a bad idea? I mean their debugging docs https://realm.io/docs/java/latest/#debugging state:
Unfortunately these values are wrong because the field values are not
used. Realm creates a proxy object behind the scenes and overrides the
getters and setters in order to access the persisted data in the Realm
So could someone please enlighten me? Can I skip getters and setters and just stick with public fields? Is there any thorough docs on this?
public fields work in most cases, and since Realm 2.0.0 they work even in constructors of RealmObjects (allowing "default values"), and work if you directly access the property.
For example,
SomeObject obj = results.get(i);
obj.blah = "Blahblah";
That works, because managed RealmObjects' field access are transformed by the Realm-Transformer into proxy getter/setter calls (in this case, into the realmSet$blah method).
This has been the case since 0.88.0, when Realm started being provided as a Gradle plugin.
However, a major limitation is that the proxy field access doesn't run in instrumentation tests, because the androidTestCompile scope does not run the transformer.

How to use satyan's Sugar ORM with #Table annotation

Im very statisfied with SugarOrm for Android, but I ran into an issue. I'm using it with GSON for Json serializations and I want to get rid of SugarRecord's id attribute. I know I should use #Table annotation and later exclude specific field from serialization using #Expose, but after annotating class with #Table I cannot use .save(), delete(),... methods on the object, as it is the case extending SugarRecord. I don't know how to persist objects using #Table annotation.
I found the documentation here very limited.
The document hasn't been updated for the annotation based persistence yet. The methods save(), delete() will be available as static methods on SugarRecord class.
So instead of doing this:
object.save()
You'd be doing this:
SugarRecord.save(object)
Check out some tests here to understand better.
https://github.com/satyan/sugar/tree/master/example/src/test/java/com/example/sugartest

How Gson is filling values in private member data?

We can use gson for parsing JSON response and save our time. I have used this in numerous projects. What question came in my mind is even if we declare variable final, gson can fill that variables value.
Now this something strange and conflicting with OOP principals. How can any other thing except that class can access those private variables?
Is there any principal or concept which does this, probably as a part of Serialization or something like that ?
Also one more thing, in case of making it Parcelable we always have one parameterised constructor. At that time how Gson can create object of our class in absence of default constructor?
Gson is using Java "Reflection" for accessing the private fields of other class.

Categories

Resources