[英]How to convert firebase data (snake_case) to Java object (camelCase)
I want to map my firebase data object to my pojo. 我想将我的firebase数据对象映射到我的pojo。 However, my firebase object property name is snake case, such as;
但是,我的firebase对象属性名是snake case,例如; "user_name".
“用户名”。 I want to use camelCase on my pojo, such as;
我想在我的pojo上使用camelCase,例如; "userName"
“用户名”
I found beautiful answers like this one, however, I couldn't find any sample about snake_case to camelCase mapping. 我找到了像这样的漂亮答案,然而,我找不到任何关于snake_case的样本来camelCase映射。
My pojo; 我的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.
'content'匹配没有问题(有或没有@SerializedName注释)但userName保持为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?
有没有一种优雅的方式将数据与pojo相匹配?
The problem was @SerializedName annotation. 问题是@SerializedName注释。 Firebase has its own annotation, which is @PropertyName.
Firebase有自己的注释,即@PropertyName。
It is important to be careful about getter name because annotation cares about its name too. 注意getter名称非常重要,因为注释也关注它的名称。 The property must be public too.
该物业也必须公开。
There is a perfect answer about that on this link. 在这个链接上有一个完美的答案。
Final state of my pojo; 我的pojo的最终状态;
@PropertyName("content")
public String content;
@PropertyName("user_name")
public String userName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.