Kevin
Kevin

Reputation: 6292

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

Upvotes: 4

Views: 5263

Answers (1)

user949300
user949300

Reputation: 15729

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.

Upvotes: 6

Related Questions