由傑克遜定義自定義bean的正確方法是空序列化?傑克遜 - 定義自定義bean的方式對於序列化是空的
我想通過Json-> POJO-> Json過濾不需要的字段。如果自定義bean對象中的所有變量都爲空,我想當序列化爲JSON時,自定義bean對象不會出現。如果裏面的任何變量不爲null,則應該出現自定義bean對象。
目前,我有一個額外的方法來分配空值給自定義bean,如果所有變量都屬於它,則爲空。我在尋找的是如果傑克遜能做到這一點。
public class JsonFileContext {
//...Some variables
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonView(Views.In.class)
private ContentRecognize contentRecognize;
//...getter/setter method
}
ContentRecognize類
public class ContentRecognize {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String type;
@JsonInclude(JsonInclude.Include.NON_NULL)
private RecognizePattern targetId;
@JsonInclude(JsonInclude.Include.NON_NULL)
private RecognizePattern msgId;
//...getter/setter method
}
期望輸出
{
"var1":"var1Content",
"var2":"var2Content"
}
和
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{
"type":"typeContent"
}
}
電流輸出
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{}
}
和
{
"var1":"var1Content",
"var2":"var2Content",
"contentRecognize":{
"type":"typeContent"
}
}
能否請你加入ContentRecognize的剪輯代碼'班? –
@TanMaiVan代碼爲ContentRecognize類增加了 – hk6279
試試這個 - http://www.jsonschema2pojo.org/將json轉換爲pojo類 –