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

百般思念 提交于 2019-12-06 13:15:46

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.

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.

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