2

I want to map my firebase data object to my pojo. However, my firebase object property name is snake case, such as; "user_name". I want to use camelCase on my pojo, such as; "userName"

I found beautiful answers like this one, however, I couldn't find any sample about snake_case to camelCase mapping.

My pojo;

@SerializedName("content")
private String content;
@SerializedName("user_name")
private String userName;

I'm using the following line of code for mapping. 'content' matches with no problem(with or without @SerializedName annotation) but userName stays as null.

Story story = storySnapshot.getValue(Story.class);

That is also an issue for obfuscation. Is there an elegant way to match the data to pojo?

Efe Budak
  • 659
  • 5
  • 27

1 Answers1

6

The problem was @SerializedName annotation. Firebase has its own annotation, which is @PropertyName.

It is important to be careful about getter name because annotation cares about its name too. The property must be public too.

There is a perfect answer about that on this link.

Final state of my pojo;

@PropertyName("content")
public String content;
@PropertyName("user_name")
public String userName;
Efe Budak
  • 659
  • 5
  • 27