//揭开JsonConfig的神秘面纱,for bean to json
JsonConfig jsonConfig = new JsonConfig();
//忽略掉bean中含后某个注解的field,不转换成json,可以多次增加不同注解
//jsonConfig.addIgnoreFieldAnnotation(Person.class);//一定是注解的类,我这里没有例子,大家可以自己做
//同上
//jsonConfig.addIgnoreFieldAnnotation("person");
//把某种类型的class,转换成自定义的结果
jsonConfig.registerDefaultValueProcessor(Address.class, new DefaultValueProcessor() {
@Override
public Object getDefaultValue(Class type) {
return null;
}
});
//返回成自己设定的jsonObject
// jsonConfig.registerJsonBeanProcessor(Person.class, new JsonBeanProcessor() {
//
// @Override
// public JSONObject processBean(Object bean, JsonConfig jsonConfig) {
//
// return null;
// }
// });
//jsonConfig.registerPropertyNameProcessor(target, propertyNameProcessor);
//被registerJsonPropertyNameProcessor取代
//在到json层的时候的转换,比上面javapropertyName靠后执行
jsonConfig.registerJsonPropertyNameProcessor(Person.class, new PropertyNameProcessor() {
@Override
public String processPropertyName(Class beanClass, String name) {
if(name.equals("name")){
return "nameJson";
}
return name;
}
});
//在json层转换的
jsonConfig.registerJsonValueProcessor(String.class, new JsonValueProcessor() {
@Override
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
if(key.equals("emptyStr")){
return "asdfa";
}
return value;
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
// TODO Auto-generated method stub
return null;
}
});
jsonConfig.registerJsonValueProcessor("nullStr", new JsonValueProcessor() {
@Override
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return "nullStr2";
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return null;
}
});
//jsonConfig.registerJsonValueProcessor(beanClass, propertyType, jsonValueProcessor);//更加细化
//jsonConfig.registerJsonValueProcessor(beanClass, key, jsonValueProcessor);//更加细化
//剔除哪个属性
jsonConfig.registerPropertyExclusion(Person.class, "sameTest");
//剔除哪些属性
//jsonConfig.registerPropertyExclusions(target, properties);
//是否允许空
jsonConfig.setAllowNonStringKeys(true);
//内部如果有嵌套引用时候,如何处理 DEFAULT_CYCLE_DETECTION_STRATEGY = CycleDetectionStrategy.STRICT;
//jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);
//可以实现吧A class 转换成B Class
// jsonConfig.setDefaultValueProcessorMatcher(new DefaultValueProcessorMatcher() {
//
// @Override
// public Object getMatch(Class target, Set set) {
// //return B.class
// return null;
// }
// });
//忽略掉哪些属性
//jsonConfig.setExcludes(excludes);
//jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);
//jsonConfig.setIgnoreJPATransient(ignoreJPATransient);
//jsonConfig.setIgnorePublicFields(ignorePublicFields);
//jsonConfig.setIgnoreTransientFields(ignoreTransientFields);
//jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);
//jsonConfig.setJsonPropertyNameProcessorMatcher(propertyNameProcessorMatcher);
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object value) {
// TODO Auto-generated method stub
return false;
}
});
//jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
//jsonConfig.setJavascriptCompliant(javascriptCompliant);
System.out.println(format(JSONObject.fromObject(p, jsonConfig).toString()));
Json系列之四 揭开JsonConfig的神秘面纱 java to json
最新推荐文章于 2024-07-26 03:07:25 发布