0
我想問問它是否可以在Json中自定義在運行時序列化過濾器?可能在運行時自定義FilterProvider?
我Sourcelooks像:
FilterProvider filters = new SimpleFilterProvider()
.addFilter("XXX",SimpleBeanPropertyFilter.serializeAllExcept(JsonFilterSerializer.getInstance().getMyView()));
String json= mapper.writer(filters).withDefaultPrettyPrinter().writeValueAsString(obj);
我需要的解決方案是這樣的:
FilterProvider filters = new SimpleFilterProvider();
if(x==true)
filters.addFilter("XXX",SimpleBeanPropertyFilter.serializeAllExcept(JsonFilterSerializer.getInstance().getXXXView()));
if(y==true)
filters.addFilter("YYY",SimpleBeanPropertyFilter.serializeAllExcept(JsonFilterSerializer.getInstance().getYYYView()));
if(z==true)
filters.addFilter("ZZZ",SimpleBeanPropertyFilter.serializeAllExcept(JsonFilterSerializer.getInstance().getZZZView()));
String json= mapper.writer(filters).withDefaultPrettyPrinter().writeValueAsString(obj);
事情是我喜歡做的過濾器視圖添加到通過如果我的映射。否則,我必須在每種情況下添加它們,分開我認爲是非常迂迴的。