简体   繁体   English

Gson:序列化瞬态场?

[英]Gson: serialize transient field?

Is there anyway to serialize transient field of a class? 无论如何序列化一个类的瞬态字段? The documentation mentioned it is not supported by default but there might be a way to turn it on? 默认情况下不支持提到它的文档,但可能有办法打开它?

Many thanks 非常感谢

I'm not a Gson expert, but I think you can add the annotation 我不是Gson专家,但我认为你可以添加注释

@com.google.gson.annotations.Expose

javadocs here - note that you have to do a few other things too javadocs在这里 - 请注意,你还必须做其他一些事情

Alternatively, you can call 或者,你可以打电话

GsonBuilder.excludeFieldsWithModifiers(int... modifiers)

and leave out the transient flag. 并忽略了瞬态旗帜。 Search on "Java Modifier Excleusion" (or transient) in this doc 此doc中搜索“Java Modifier Excleusion”(或transient)

However, as Dave and Louis commented, it is unusual to serialize a transient field. 然而,正如戴夫和路易斯评论的那样,序列化瞬态场是不寻常的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM