How does Firebase setValue () work for objects in Java?

The Firebase documentation on lists provides an example where you create a Java object and write that object to a Firebase link through setValue() like this:

 private static class MyObject { private String property1; private int property2; public MyObject(String value1, int value2) { this.property1 = value1; this.property2 = value2; } public String getFirstProperty() { return property1; } } private void populateList() { Firebase ref = new Firebase("https://MyDemo.firebaseIO-demo.com/myObjects"); ref.push().setValue(new MyObject("myString", "7")); } 

How does it work within the organization, i.e. when you did not write the toString() method, etc., what value will be stored in the Firebase link exactly? And one more step, will the Firebase client be able to restore the old object from the stored value? How?

Do I need to have a private static class so that Firebase can read fields?

+4
source share
1 answer

The documentation explains how it works :

Set the data in this place to the set value. The native types accepted by this method for the value correspond to JSON types:

  • Boolean
  • Long
  • Double
  • Map<String, Object>
  • List<Object>

In addition, you can install instances of your own class in this place if they satisfy the following restrictions:

  • The class must have a default constructor that takes no arguments
  • The class must define public getters for the assigned properties. Properties without a public getter will be set to their default if the instance is deserialized

So you have to create getters for all the properties you want to keep. In your example, your firstProperty property will be written, although it cannot be read because you did not define a default constructor.

+5
source

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


All Articles