Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raomuyang
Copy link

@raomuyang raomuyang commented Mar 1, 2018

Ignore is an annotation that indicates this member should be ignored for JSON
serialization or deserialization.

This annotation has no effect unless you build com.google.gson.Gson
with a com.google.gson.GsonBuilder and invoke GsonBuilder.excludeFieldsWithIgnoreAnnotation() method.

  • An example:
public class Account {
  private String accountName;
  @Ignore(deserialize = false) private String info;
  @Ignore private String password;
}

If you create Gson via new Gson(), the toJson() and fromJson()
methods will use all fields for serialization and deserialization. However, you can create
Gson via new GsonBuilder().excludeFieldsWithIgnoreAnnotation().create(), then the
toJson() will exclude the info field and thepassword field,
because those fields were marked with the@Ignore annotation and set
serialize = true. Similarly, the fromJson() will exclude password since deserialize is set to false.


What's the difference fromExpose annotation?
TheExpose annotation indicates this member should be exposed for JSON serialization
or deserialization, it means all the fields that you wanna expose, you should be marked with Expose:

public class Student {
  @Expose private Account account;
  @Expose private String class;
}

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 by Expose.

@googlebot
Copy link

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. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers
  • Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • The email used to register you as an authorized contributor must be the email used for the Git commit. Check your existing CLA data and verify that your email is set on your git commits.
  • The email used to register you as an authorized contributor must also be attached to your GitHub account.

@raomuyang raomuyang force-pushed the add-ignore-expose-build-method branch from 898ef28 to cf794cb Compare March 1, 2018 15:36
@googlebot
Copy link

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Mar 1, 2018
@NightlyNexus
Copy link
Contributor

If you need this functionality, you can mark the fields transient.

@raomuyang
Copy link
Author

@NightlyNexus Thanks for you review. But transient can not replace @Ignore(deserialize = false)

@kyle-cackett
Copy link

I home-brewed the functionality in this pull request because transient was not sufficient for my use case. The problem with transient is that it has implications outside of GSON. In particular, I needed a particular field to be serialized by Java when writing objects to streams but not by GSON -- this is not possible with the transient field.

@Johnny850807
Copy link

Johnny850807 commented Jan 12, 2020

Agreed, @expose makes class so ugly.
And there is a case that I want my field to be non-transient, but be ignored from Gson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants