Add "Ignore" annotation to ignore some fields during serialization or d… #1262
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ignore
is an annotation that indicates this member should be ignored for JSONserialization or deserialization.
This annotation has no effect unless you build
com.google.gson.Gson
with a
com.google.gson.GsonBuilder
and invokeGsonBuilder.excludeFieldsWithIgnoreAnnotation()
method.If you create Gson via
new Gson()
, thetoJson()
andfromJson()
methods will use all fields for serialization and deserialization. However, you can create
Gson via
new GsonBuilder().excludeFieldsWithIgnoreAnnotation().create()
, then thetoJson()
will exclude theinfo
field and thepassword
field,because those fields were marked with the
@Ignore
annotation and setserialize = true
. Similarly, thefromJson()
will excludepassword
sincedeserialize
is set to false.What's the difference from
Expose
annotation?The
Expose
annotation indicates this member should be exposed for JSON serializationor deserialization, it means all the fields that you wanna expose, you should be marked with
Expose
:You can Create Gson with
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
,but the fields of
Account
class will be excluded because they are not marked byExpose
.