我在firebase上的数据使用了许多具有字符串类型的字段,但实际上是枚举值(我在验证规则中检查).要按照指南将数据下载到我的Android应用程序中,该字段必须是基本字段String
.我知道我可以使用枚举的第二个(排除的)字段解决这个问题,并根据字符串值设置此字段.一个简短的例子:
class UserData : BaseModel() {
val email: String? = null
val id: String = ""
val created: Long = 0
// ... more fields omitted for clarity
@Exclude
var weightUnitEnum: WeightUnit = WeightUnit.KG
var weightUnit: String
get() = weightUnitEnum.toString()
set(value) { weightUnitEnum = WeightUnit.fromString(value) }
}
enum class WeightUnit(val str: String) {
KG("kg"), LB("lb");
override fun toString(): String = str
companion object {
@JvmStatic
fun fromString(s: String): WeightUnit = WeightUnit.valueOf(s.toUpperCase())
}
}
Run Code Online (Sandbox Code Playgroud)
现在,虽然这有效,但它并不是很干净:
enum …
我正在为我的实体使用autovalue并注释它们以允许json解析.
有新的SDK新的注解:Exclude
,IgnoreExtraProperties
,ThrowOnExtraProperties
和@PropertyName
:https://firebase.google.com/docs/reference/android/com/google/firebase/database/PropertyName.但是,sdk似乎缺少PropertyName ..
我正在使用谷歌的firebase-database SDK for Android,v9.0.1.我将我的应用程序连接到Firebase,可以在不同位置读取和写入数据.
但是,我无法获取一个特定的布尔字段来绑定使用,dataSnapshot.getValue(PingReport.class)
并且我在日志中不断收到错误,该错误表明No setter/field for isUp found on class com.myapp.PingReport
该字段在我的模型中明显存在.
这是Firebase数据库中的JSON:
{
"durationMs": 364,
"isUp": true,
"timestampMillis": 1464916019971
}
Run Code Online (Sandbox Code Playgroud)
这是模型类:
public class PingReport {
private long durationMs;
private boolean isUp;
private long timestampMillis;
public PingReport() {
// required by Firebase
}
public PingReport(long durationMs, boolean isUp, long timestampMillis) {
this.durationMs = durationMs;
this.isUp = isUp;
this.timestampMillis = timestampMillis;
}
public long getDurationMs() {
return durationMs;
}
public boolean isUp() {
return isUp;
}
public …
Run Code Online (Sandbox Code Playgroud) 嗨我有一个Kotlin数据类如下
data class User (
@get:Exclude val gUser: Boolean,
@get:Exclude val uid: String,
@get:PropertyName("display_name") val displayName: String,
@get:PropertyName("email") val email: String,
@get:PropertyName("account_picture_url") val accountPicUrl: String,
@get:PropertyName("provider") val provider: String
)
Run Code Online (Sandbox Code Playgroud)
我能够没有问题地序列化对象.但是在执行firebase查询时,我无法反序列化对象.目前这是我正在做的获取数据
_firebaseReference.child(getString(R.string.firebase_users_key)).child(user.uid)
.setValue(user).addOnCompleteListener{
_firebaseReference.child("users").child(user.uid)
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if (p0.exists()) {
val userHash = p0.value as HashMap<*, *>
var currentUser: User
if (userHash[getString(R.string.provider_key)]
!= getString(R.string.provider_google)) {
currentUser = User(false, p0.key!!,
userHash["display_name"].toString(),
userHash["email"].toString(),
userHash["account_picture_url"].toString(),
userHash["provider"].toString())
} else {
currentUser = …
Run Code Online (Sandbox Code Playgroud) 我想将我的 firebase 数据对象映射到我的 pojo。但是,我的 firebase 对象属性名称是蛇形大小写,例如;“用户名”。我想在我的 pojo 上使用 camelCase,例如;“用户名”
我找到了这样一个漂亮的答案,但是,我找不到任何关于snake_case 到camelCase 映射的示例。
我的 pojo;
@SerializedName("content")
private String content;
@SerializedName("user_name")
private String userName;
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码行进行映射。'content' 匹配没有问题(有或没有 @SerializedName 注释),但 userName 保持为空。
Story story = storySnapshot.getValue(Story.class);
这也是混淆的一个问题。是否有一种优雅的方式将数据与 pojo 匹配?
java android type-conversion firebase firebase-realtime-database
我正在尝试document
使用自定义对象将a存储在我的android应用程序中的firestore中。如果我使用Proguard构建应用程序,是否可以像Gson
使用@SerializedName
批注提供的方式那样为类中的字段指定序列化名称?