Add "Ignore" annotation to ignore some fields during serialization or d…#1262
Add "Ignore" annotation to ignore some fields during serialization or d…#1262raomuyang wants to merge 1 commit intogoogle:mainfrom
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. |
Ignoreis an annotation that indicates this member should be ignored for JSONserialization or deserialization.
This annotation has no effect unless you build
com.google.gson.Gsonwith a
com.google.gson.GsonBuilderand 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 theinfofield and thepasswordfield,because those fields were marked with the
@Ignoreannotation and setserialize = true. Similarly, thefromJson()will excludepasswordsincedeserializeis set to false.What's the difference from
Exposeannotation?The
Exposeannotation 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
Accountclass will be excluded because they are not marked byExpose.