如果未设置的话默认是DefaultDefaultValueProcessor
public class DefaultDefaultValueProcessor implements DefaultValueProcessor {
public Object getDefaultValue( Class type ) {
if( JSONUtils.isArray( type ) ){
return new JSONArray();
}else if( JSONUtils.isNumber( type ) ){
if( JSONUtils.isDouble( type ) ){
return new Double( 0 );
}else{
return new Integer( 0 );
}
}else if( JSONUtils.isBoolean( type ) ){
return Boolean.FALSE;
}else if( JSONUtils.isString( type ) ){
return "";
}
return JSONNull.getInstance();
}
}
在jsonConfig 注册defaultValueProcessor
// 设置Integer类型为空的默认值 json-lib默认是0
jsonConfig.registerDefaultValueProcessor(Integer.class,
new DefaultValueProcessor() {
public Object getDefaultValue(Class type) {
return null;
}
});
这样转换时Integer类型如果为null转换还是null,不会被转为0
本文介绍如何在JSON配置中设置默认值处理器(defaultValueProcessor),以指定不同类型数据在未提供时的默认值。例如,可以定制Integer类型的默认行为,使得在遇到null时返回null而非默认的0。
4708

被折叠的 条评论
为什么被折叠?



