-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "Ignore" annotation to ignore some fields during serialization or d… #1262
base: main
Are you sure you want to change the base?
Conversation
Thanks for your pull request. t looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
898ef28
to
cf794cb
Compare
CLAs look good, thanks! |
If you need this functionality, you can mark the fields |
@NightlyNexus Thanks for you review. But |
I home-brewed the functionality in this pull request because |
Agreed, @expose makes class so ugly. |
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
.