`
yaoba
  • 浏览: 41869 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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

    博客分类:
  • JSON
阅读更多
如果未设置的话默认是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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics