Gson: serialize transient field?

时间秒杀一切 提交于 2021-01-27 07:52:39

问题


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


回答1:


I'm not a Gson expert, but I think you can add the annotation

@com.google.gson.annotations.Expose

javadocs here - note that you have to do a few other things too

Alternatively, you can call

GsonBuilder.excludeFieldsWithModifiers(int... modifiers)

and leave out the transient flag. Search on "Java Modifier Excleusion" (or transient) in this doc

However, as Dave and Louis commented, it is unusual to serialize a transient field.



来源:https://stackoverflow.com/questions/10919081/gson-serialize-transient-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!