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

核能气质少年 提交于 2019-12-10 10:47:20

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17377549/best-method-in-hiding-attribute-in-json-serialization-from-java-transient-or-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!