Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention with Firebase serialization/deserialization?

I wonder to know how Firebase serialize/deserialize POJO object to/from json, does it use Jackson or Gson or any similar library else.
I have trouble about naming convention with Firebase. My model some like this:

class Data {
    private String someFieldName;
    private String anotherFieldName;
    public Data() {}
    public void setSomeFieldName(String) {...}
    public String getSomeFieldName(String) {...}
    public void setAnotherFieldName(String) {...}
    public String getAnotherFieldName() {...}
}

And the expected result in Firebase should be:

{
    "some_field_name" : "...",
    "another_field_name" : "..."
}

with Gson I can use FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES for my purpose, as in Gson doc:

Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":

  • someFieldName ---> some_field_name

  • _someFieldName ---> _some_field_name

  • aStringField ---> a_string_field

  • aURL ---> a_u_r_l


How can I convert my POJO object to "Firebase value" with specific naming convention and vice versa, or there are any way to customize the serialize/deserialize process?

Thanks!

like image 877
Khang .NT Avatar asked Nov 19 '16 08:11

Khang .NT


People also ask

What is the difference between deserialization and serialization?

Deserialization is the reverse of serialization and converts byte stream back to original object. A class must implement Serializable interface to be eligible for serialization. ObjectOutputStream and ObjectInputStream classes help to serialize and deserialize an object respectively. Only non-static data members get serialized.

How to serialize a class in Java?

Let us understand the Serialization through below example: Step 1: Create Class named Employee #create a class named Employee class Emp: #initialize the attributes def __init__... Step 2: Saving data as a pickle file

What is deserialization in Python?

Deserialization is also known as Unmarshalling or Unpickling. Python has a specialized package named “Pickle” to perform Serialization and Deserialization.


2 Answers

When reading the data back from the Firebase database you can use the @PropertyName annotation to mark a field to be renamed when being serialized/deserialized, like so:

@IgnoreExtraProperties
class Data {
    @PropertyName("some_field_name")
    public String someFieldName
    @PropertyName("another_field_name")
    private String anotherFieldName;
    public Data() {}
}

Make sure that your field is public and not private or else the annotation will not work (I also believe that Firebase uses Jackson to handle the object mapping under the hood, but don't think you can actually customize HOW it uses it).

like image 57
crymson Avatar answered Oct 24 '22 18:10

crymson


Personally I prefer keeping explicit control over the serialization/deserialization process, and not relying on specific framework and/or annotations.

Your Data class can be simply modified like this :

class Data {
    private String someFieldName;
    private String anotherFieldName;
    public Data() {}
    public Data(Map<String, Object> map) {
        someFieldName = (String) map.get("some_field_name") ;
        anotherFieldName = (String) map.get("another_field_name") ;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("some_field_name", someFieldName);
        map.put("another_field_name", anotherFieldName);
        return map ;
    }
}

For writing value to firebase, simply do :

dbref.setValue(data.toMap());

For reading :

Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
data = new Data(map);

They are some advantages with this solution :

  • No assumption is made on underlying json framework
  • No need to use annotations
  • You can even further decouple you Object model from your Json model by externalizing the methods toMap() and constructor to a DataMapper (snippet hereunder)

public static Data fromMap(Map<String, Object> map) {
    String someFieldName = (String) map.get("some_field_name") ;
    String anotherFieldName = (String) map.get("another_field_name") ;
    return new Data(someFieldName, anotherFieldName);
}

public static Map<String, Object> toMap(Data data) {
    Map<String, Object> map = new HashMap<>();
    map.put("some_field_name", data.getSomeFieldName());
    map.put("another_field_name", data.getAnotherFieldName());
    return map ;
}
like image 27
Benoit Avatar answered Oct 24 '22 16:10

Benoit