使用Firebase序列化/反序列化命名约定? [英] Naming convention with Firebase serialization/deserialization?
问题描述
我不知道Firebase如何序列化/反序列化json中的POJO对象,是否使用Jackson或Gson或其他类似的库。
命名约定与Firebase。我的模型是这样的:
$ p $ class Data {
private String someFieldName;
private String anotherFieldName;
public Data(){}
public void setSomeFieldName(String){...}
public String getSomeFieldName(String){...} $ b $ public void setAnotherFieldName(String) {...}
public String getAnotherFieldName(){...}
}
Firebase中的预期结果应该是:
$ b $ $ $ $ $ $ $ $ $ $ $ $ ,
another_field_name:...
}
with < Gson 我可以使用 FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES
作为我的目的,就像在Gson文档中一样:
下面是一些Java Field Name--->JSON Field Name的例子:
- someFieldName ---> some_field_name
_someFieldName ---> _some_field_name
aStringField ---> a_string_field
- aURL ---> a_u_r_l
如何使用特定的命名约定将我的POJO对象转换为Firebase值,反之亦然,或者有什么方法可以自定义序列化/反序列化过程?
感谢!
从Firebase数据库读取数据时,可以使用<$
@PropertyName
注解标记一个字段在被序列化/反序列化时被重命名。 c $ c> @IgnoreExtraProperties
class Data {
@PropertyName(some_field_name)
public String someFieldName
@PropertyName(another_field_name)
private String anotherFieldName;
public Data(){}
}
确保您的字段是公开的而不是私人的,否则注解将无法工作(我也相信Firebase使用杰克逊处理引擎盖下的对象映射,但不认为你可以实际定制它如何使用它)。
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!
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).
这篇关于使用Firebase序列化/反序列化命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!