修改JSONObject.fromObject数字为null时被转换为0

如果未设置的话默认是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

你可能感兴趣的:(java,json)