Best method in hiding attribute in JSON serialization from java transient or @Transient annotation? [closed]

StackOverflow https://stackoverflow.com/questions/17377549

Question

In Spring, ExtJs application I use JSON serialization of java objects. When I want to exclude a property from JSON, I use @Transient annotation.

I see java transient keyword also works with this. I like to know which of these are best and more efficient in performance perspective and security.

Était-ce utile?

La solution 2

I prefer to use annotation based mapping. Because this may dependent on you JSON parser API. So, there can be variations between java annotation Vs @Transient.

Autres conseils

It's better to use the @Transient annotation, this will make it more obvious what your intentions are.

Take a look at this:

private transient Somefield myField;

and this:

@Transient
private Somefield myField;

Which one is more obvious?

transient as a keyword is used "only" for ignoring certain fields during Java serialization. You can say that the annotation is more domain specific and your JSON processor will be better off using it; security and performance wise since it will know what you are trying to do.

Not to mention, it's certainly possible (check your implementation, though) that if a field is marked transient, but not annotated, when your object is going over the wire, your implementation will expect a field with a null value, but instead it will get nothing and that might lead to some problems.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top