如果未设置的话默认是DefaultDefaultValueProcessor
Java代码
- 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
Java代码
- // 设置Integer类型为空的默认值 json-lib默认是0
- jsonConfig.registerDefaultValueProcessor(Integer.class,
- new DefaultValueProcessor() {
- public Object getDefaultValue(Class type) {
- return null;
- }
- });
这样转换时Integer类型如果为null转换还是null,不会被转为0
转自:http://yaoba.iteye.com/blog/555806