9

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!

Community
  • 1
  • 1
Khang .NT
  • 1,504
  • 6
  • 24
  • 46

2 Answers2

23

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).

crymson
  • 1,170
  • 7
  • 12
4

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 ;
}
Benoit
  • 5,118
  • 2
  • 24
  • 43
  • I can appreciate an alternative approach or more complex requirements, but dismissing annotations out of hand is overkill. Annotations are quite powerful and were designed specifically to avoid extra boilerplate coding as you have written in your solution. @PropertyName("your_name") gives you both read and write mappings and are easily maintainable in the majority of cases. – angryITguy Sep 04 '17 at 23:46
  • @giulio I Agree with you... No need to use annotations is not a real advantage. If they were annotations that frameworks-independent (Not bound to Jackson) and recognized by Jackson or Firebase, I would use them. – Benoit Sep 05 '17 at 11:48