Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the property @JSONIgnore be set on a per call basis?

Tags:

java

json

jackson

I am working on a site that uses promo codes to potentially provide users free memberships to the site. The level that a member signs up at is an attribute of a promo code object and stored in the database as an int, either 1 or 2. However, the only time the member level attribute is relevant to the UI of the site is for a landing page associated with a specific promo code. So for that case, I don't want the member level to be ignored by JSON. After a user returns to the site and has a promo code object associated with their account, we no longer care about their member level so I would like the member level attribute to be ignored by JSON.

So my question is this, is it possible to set an object's attribute to @JSONIgnore on a per access basis, or can an attribute only be ignored or not ignored? In other words is there a method like object.getAttribute().setAttributeJSONIgnore(true | false)?

like image 816
EJay Avatar asked Jul 11 '12 18:07

EJay


People also ask

What is the use of @JsonIgnore annotation?

@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.

When should I use JsonIgnore?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property.

What is the difference between JsonIgnore and JsonIgnoreProperties?

@JsonIgnore is to ignore fields used at field level. while, @JsonIgnoreProperties is used at class level, used for ignoring a list of fields. Annotation can be applied both to classes and to properties.

What is @JsonIgnore in spring boot?

The. @JsonIgnore. annotation marks a field in a POJO to be ignored by Jackson during serialization and deserialization. Jackson ignores the field in both JSON serialization and deserialization.


1 Answers

No. Annotation-based approaches are static, since although you could use custom AnnotationIntrospector, resulting JsonSerializer instances are reused since their construction is costly.

But there are ways to filter out properties, check out "Filtering Properties" article, for example.

like image 115
StaxMan Avatar answered Sep 28 '22 09:09

StaxMan