-
Notifications
You must be signed in to change notification settings - Fork 329
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
Creating new annotation to exclude beans fields and classes from the GSON Serialization #763
Conversation
…GSON serialization.
@@ -68,6 +73,7 @@ private boolean isCompatiblePath(Entry<String, Class<?>> path, Class<?> definedI | |||
|
|||
@Override | |||
public boolean shouldSkipClass(Class<?> clazz) { | |||
return false; | |||
GsonOmitField annotation = clazz.getAnnotation(GsonOmitField.class); | |||
return (annotation != null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return clazz.isAnnotationPresent(GsonOmitField)
?
this is a really nice PR, thanks doing it @renatorroliveira! Two points: (1) Can you add a simple example of usage at VRaptor's site? |
Where are the tests? |
GsonOmitField annotation = f.getAnnotation(GsonOmitField.class); | ||
if (annotation != null) | ||
return true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same here for isAnnotationPresent
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GSON FieldAttributes
don't have this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it has, with GSON 2.2.4.
I even tested my app with this implementation and works fine.
The unit tests passed like a charm =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the isAnnotationPresent
, this is not present on FieldAttributes
. right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it isn't. The Class<?> type has it, but FieldAttributes hasn't.
I like this feature, thank you for coding. After changing the code with suggestions, are ready to merge (by my mind). |
Yes, it should be isAnnotationPresent, my mistake =) @Turini I can add the example of usage. I should do this in this same PR? |
Same PR. |
sorry my delay, please add docs at the same PR. thanks |
I looking at the site. |
Cool PR. I just don't like the name You are tying the feature to the implementation. If we choose to replace GSon to another framework in the future, the annotation will be awkward. Rename to something like |
Great Lucas. I like your suggestion. |
You is right @lucascs i will modify these points. |
Have you tried this? |
Yes @lucascs, i've looked at the Expose annotation, but it has 2 problems:
For me these 2 points are enough to justify the creation of a OmitField annotation =) |
Another point that i forget, the capability to omit a class is something you don't acomplish with Expose, since all fields are ommited, except those that were annotated. I see the OmitField as a much more clear am easier approach. |
I'm not arguing against the OmitField... just to see if gson annotations are already supported by default. |
Yes i udnerstood that, i just want to clarify my motivation to create the annotation =) |
I think that if GSON annotations are supported, may we can do this approaching, changing GSON components to always uses annotations. Why? Because if Google team create another annotations in the future will supported without any modifications in vraptor implementation. Makes any sense? |
Makes sense, but i see two concerns here. Another point is that the version 2.2.4 was released in May of 2013, and next release was two weeks ago, the version 2.3, more than one year without updates. Maybe is not necessary to worry about GSON updates. =D |
I believe that we should create a way to enable the Expose annotation without the need to overwrite the GsonBuilderWrapper. |
As I see with this option (enabling expose annotation), we need to annotate all fields with |
Yeah, very boring =) |
public class UserPrivateInfo { | ||
String document; | ||
String phone; | ||
String address; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add private
on fields and a code comment
about omitted getters and setters? Same for the User
.
|
||
When serialiazing to JSON you can use the annotation 'SkipSerialization' to omit the serialization of a field or class. | ||
|
||
~~~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can maybe add an text introducing the situation here, for example:
"The class below has all of your fields omitted for serialization."
I agree, and i will improve the docs. |
thanks @renatorroliveira! |
Creating new annotation to exclude beans fields and classes from the GSON Serialization
This is a feature that is extremely desirable.
Creating an annotation (@GsonOmitField) that marks a field or a class to be skipped at the JSON serialization reduces a lot the duplication of exclude() calls in some cases.
Imagine a User model where are some private information like password and phone number stored.
Today you have to add a .exclude() call for those fields every time you want to serialize a user object.
With this feature, you just annotate those fields with @GsonOmitField and the job is done, you don't need to worry about those informations ending up at the client side.
It is a very simple implementation and adds a wonderful feature.