问题
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