杰克逊 - 动态地抑制属性的序列化(写入) [英] Jackson - suppressing serialization(write) of properties dynamically
问题描述
我正在尝试使用Jackson将Java对象转换为Tomcat / jersey中的JSON对象。并且想要动态地抑制某些属性的序列化(写入)。
I am trying to convert java object to JSON object in Tomcat/jersey using Jackson. And want to suppress serialization(write) of certain properties dynamically.
我可以使用JsonIgnore,但我想在运行时做出忽略决定。任何想法??
I can use JsonIgnore, but I want to make the ignore decision at runtime. Any ideas??
以下是一个例子,当我将User对象序列化为JSON时,我希望禁止id字段。
So as an example below, I want to suppress "id" field when i serialize the User object to JSON..
new ObjectMapper.writeValueAsString(user);
class User {
private String id = null;
private String firstName = null;
private String lastName = null;
//getters
//setters
}//end class
推荐答案
是的,JSON View是要走的路。
Yes, JSON View is the way to go.
如果你是需要让客户端决定编组哪些字段,这个例子可能会有所帮助: http://svn.codehaus.org/jackson/tags/1.6/1.6.3/src/sample/CustomSerializationView.java
If you e.g. need to let the client to decide which fields to marshal, this example might help: http://svn.codehaus.org/jackson/tags/1.6/1.6.3/src/sample/CustomSerializationView.java
这篇关于杰克逊 - 动态地抑制属性的序列化(写入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!