ifeilong / feilong-core

:gem: Reduce development, Release ideas
Apache License 2.0
529 stars 155 forks source link

JsonUtil.toJSON(Object, JsonConfig) 添加更多的javadoc #529

Closed venusdrogon closed 8 years ago

venusdrogon commented 8 years ago

JsonUtil.toJSON(Object, JsonConfig) 添加更多的javadoc

venusdrogon commented 8 years ago

534 已经移到了 JsonHelper

并且添加了更多的javadoc


    /**
     * 把实体Bean、Map对象、数组、列表集合转换成{@link JSON}.
     * 
     * <h3>说明:</h3>
     * 
     * <blockquote>
     * <ol>
     * <li>如果 <code>null==jsonConfig</code>,将使用 {@link #DEFAULT_JAVA_TO_JSON_CONFIG}</li>
     * 
     * <li>
     * 
     * <p>
     * 以下类型将转成{@link JSONArray}:
     * </p>
     * 
     * <ul>
     * <li>如果是字符串,当是以"["符号开头,"]"符号结尾的时候</li>
     * <li>数组 obj.getClass().isArray()||obj instanceof Object[]</li>
     * <li>集合 obj instanceof Collection</li>
     * <li>枚举 obj instanceof Enum</li>
     * <li>迭代器 obj instanceof Iterator</li>
     * </ul>
     * 
     * </li>
     * <li>其他类型转成 {@link JSONObject}</li>
     * </ol>
     * </blockquote>
     *
     * @param obj
     *            可以是数组,字符串,枚举,集合,map,Java bean,Iterator等类型,内部自动识别转成{@link JSONArray}还是{@link JSONObject}
     * @param jsonConfig
     *            the json config
     * @return the jSON
     * @see net.sf.json.JSONArray#fromObject(Object, JsonConfig)
     * @see net.sf.json.JSONObject#fromObject(Object, JsonConfig)
     * @see net.sf.json.util.JSONUtils#isArray(Object)
     * @see java.lang.Class#isEnum()
     * @see net.sf.json.JsonConfig#registerJsonValueProcessor(Class, JsonValueProcessor)
     * @see org.apache.commons.collections4.IteratorUtils#toList(Iterator)
     * @see org.apache.commons.collections4.IteratorUtils#toList(Iterator, int)
     * @see net.sf.json.JSONSerializer#toJSON(Object)
     */
    static JSON toJSON(Object obj,JsonConfig jsonConfig){
        JsonConfig useJsonConfig = defaultIfNull(jsonConfig, DEFAULT_JAVA_TO_JSON_CONFIG);
        registerDefaultJsonValueProcessor(useJsonConfig);

        if (isNeedConvertToJSONArray(obj)){
            Object arrayJsonObject = obj instanceof Iterator ? IteratorUtils.toList((Iterator<?>) obj) : obj;
            return JsonHelper.toJSONArray(arrayJsonObject, useJsonConfig);
        }
        return JsonHelper.toJSONObject(obj, useJsonConfig);
    }