[英]How to exclude properties from bean to json at runtime
I want to exclude properties from bean to json using json-lib at runtime. 我想在运行时使用json-lib从bean到json排除属性。
How can i do it? 我该怎么做?
I have tried using propertyFilter of jsonconfig, I am not sure if its at runtime. 我尝试使用jsonconfig的propertyFilter,但不确定其是否在运行时。
Here's a code-snippet based on the sample code at Filtering Properties in JSON Advanced Features that might be useful. 这是一个基于JSON高级功能中的过滤属性中的示例代码的代码片段,可能会很有用。
PropertyFilter pf = new PropertyFilter(){
public boolean apply( Object source, String name, Object value ) {
if( value != null && Number.class.isAssignableFrom( value.getClass() ) ){
return true;
}
return false;
}
};
PrimitiveBean bean = new PrimitiveBean();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(pf);
JSONObject json = JSONObject.fromObject( bean, jsonConfig );
You could set a different function to the JSON Config before you serialize the bean into a JSON object... in case that's what you meant by run time. 在将bean序列化为JSON对象之前,可以为JSON Config设置不同的功能...以防万一,这就是运行时的含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.