Java 如何使用 Firebase 1.0.2 忽略对象模型的新字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18125697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:33:37  来源:igfitidea点击:

How to ignore new fields for an object model with Firebase 1.0.2

javaandroidfirebase

提问by saiyancoder

I'm using the last version at the momentof Firebase dependency, which is 1.0.2 and I'm having problems into getting my pojos parsed correctly.

我使用的最后一个版本,此刻火力地堡的依赖,这是1.0.2的,我有问题,到让我的POJO的正确分析。

The thing is, at any time the schema can changed but I don't want my app to crash with this:

问题是,架构可以随时更改,但我不希望我的应用程序崩溃:

D/AndroidRuntime(14097): Shutting down VM W/dalvikvm(14097): threadid=1: thread exiting with uncaught exception (group=0x40a451f8) E/AndroidRuntime(14097): FATAL EXCEPTION: main E/AndroidRuntime(14097): com.firebase.client.FirebaseException: Failed to bounce to type E/AndroidRuntime(14097): at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:213)

D/AndroidRuntime(14097): Shutting down VM W/dalvikvm(14097): threadid=1: thread exiting with uncaught exception (group=0x40a451f8) E/AndroidRuntime(14097): FATAL EXCEPTION: main E/AndroidRuntime(14097): com .firebase.client.FirebaseException:无法弹回输入 E/AndroidRuntime(14097):在 com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:213)

Looking into the dependency tree I get that Firebase is using Hymanson mapper 1.9.7, so the annotation @JsonIgnoreProperties(ignoreUnknown = true")is not an option. Moreover, the object mapper is wrapped into this Firebase object so I can't configure the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESproperty (DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIESfor Hymanson 1.9 and before).

查看依赖关系树,我发现 Firebase 使用的是 Hymanson mapper 1.9.7,因此注释@JsonIgnoreProperties(ignoreUnknown = true")不是一个选项。此外,对象映射器被包装到这个 Firebase 对象中,所以我无法配置该DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES属性(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES对于 Hymanson 1.9 及更早版本)。

Is there any way to set this property, either as a class-level annotation or configuring the mapper or any other mechanism whatsoever?

有没有办法设置这个属性,作为类级别的注释或配置映射器或任何其他机制?

The best solution would be that Firebase 1.0.3 started using Hymanson 2.0, but don't know if this is something they care about right now.

最好的解决方案是 Firebase 1.0.3 开始使用 Hymanson 2.0,但不知道这是否是他们现在关心的问题。

Note: I've already thought about excluding the transitive Hymanson 1.9.7 dependency and adding Hymanson 2.0 so that I can access to this ignoreUnknown feature, but I don't think it is a viable choice since I would be changing the mayor version.

注意:我已经考虑过排除可传递的 Hymanson 1.9.7 依赖项并添加 Hymanson 2.0,以便我可以访问此 ignoreUnknown 功能,但我认为这不是一个可行的选择,因为我将更改市长版本。

采纳答案by saiyancoder

Firebase 1.0.3 was released and now uses Hymanson 2.2.2, so annotation @JsonIgnoreis the way to go.

Firebase 1.0.3 已发布,现在使用 Hymanson 2.2.2,因此注释@JsonIgnore是要走的路。

Edit: as of now in 2017, Firebase doesn't use Hymanson anymore. the correct annotation is @Exclude.

编辑:截至 2017 年,Firebase 不再使用 Hymanson。正确的注释是@Exclude.

回答by Chad Bingham

As the accepted answer states, Firebase now uses Hymanson, so you can annotate the desired methods you wish to ignore with

正如公认的答案所述,Firebase 现在使用 Hymanson,因此您可以注释您希望忽略的所需方法

@JsonIgnore

@JsonIgnore

Edit:

编辑:

Firebase changed everything. Woot. Now use this instead:

Firebase 改变了一切。呜。现在改用这个:

@Exclude

@排除

回答by mineralwasser

For those who have moved over to Google's official version of Firebase (As of May 29, 2016), you can use @Excludeinstead of @JsonIgnore or @JsonProperty. Hereis the link to their document.

对于已迁移到 Google 正式版 Firebase(截至 2016 年 5 月 29 日)的用户,您可以使用@Exclude而不是 @JsonIgnore 或 @JsonProperty。是他们文档的链接。

Example:

例子:

public class dataPacket{
    public String data;
    ...
    @Exclude
    public String getData(){return data;}
}

回答by Dima Rostopira

Update:

更新:

As others pointed, annotation @Excludeis right way to use it now. But if you use Kotlin that won't work. For Kotlin use

正如其他人指出的那样,注释@Exclude是现在使用它的正确方法。但是,如果您使用 Kotlin,那将不起作用。对于 Kotlin 使用

@get:Exclude var data: String? = nil
//or
@set:Exclude var data: String? = nil
//or both
@set:Exclude @get:Exclude var data: String? = nil

Because annotation can be applied only for generated fields and not to properties.

因为注释只能应用于生成的字段,而不能应用于属性。

Old answer:

旧答案:

I'm coming to Firebase from GSON were I used transient keyword. And that works with Firebase too

如果我使用了瞬态关键字,我将从 GSON 来到 Firebase。这也适用于 Firebase

public transient String data;