JS对象序列化为JSON对象的方法

var $ = $ || {};



/** 
 * 将JS对象序列化为JSON字符串 
 * @param {Mixed} o The variable to decode 
 * @return {String} The JSON string 
 * String json = $.encode(o); 
 */  
$.encode = (function() {
    if ( typeof(JSON)!=='undefined' && typeof(JSON.stringify)!=='undefined') {
        return JSON.stringify;
    }
    var I = !!{}.hasOwnProperty, _ = function(I) {
        return I < 10 ? "0" + I : I;
    }, A = {
        "\b" : "\\b",
        "\t" : "\\t",
        "\n" : "\\n",
        "\f" : "\\f",
        "\r" : "\\r",
        "\"" : "\\\"",
        "\\" : "\\\\"
    };
    return (function(C) {
        if (typeof C == "undefined" || C === null) {
            return "null";
        } else {
            if (Object.prototype.toString.call(C) === "[object Array]") {
                var B = ["["], G, E, D = C.length, F;
                for (E = 0; E < D; E += 1) {
                    F = C[E];
                    switch (typeof F) {
                        case "undefined" :
                        case "function" :
                        case "unknown" :
                            break;
                        default :
                            if (G) {
                                B.push(",");
                            }
                            B.push(F === null ? "null" : $.encode(F));
                            G = true;
                    }
                }
                B.push("]");
                return B.join("");
            } else {
                if ((Object.prototype.toString.call(C) === "[object Date]")) {
                    return "\"" + C.getFullYear() + "-" + _(C.getMonth() + 1) + "-" + _(C.getDate()) + "T" + _(C.getHours()) + ":" + _(C.getMinutes()) + ":" + _(C.getSeconds()) + "\"";
                } else {
                    if (typeof C == "string") {
                        return "\"" + C.replace(/([\x00-\x1f\\"])/g, function(B, _) {
                            var I = A[_];
                            if (I) {
                                return I;
                            }
                            return '';
                        }).replace(/[^\u0000-\u00FF]/g, function($0) {
                            return escape($0).replace(/(%u)(\w{4})/gi, "\\u$2")
                        }) + "\"";
                    } else {
                        if (typeof C == "number") {
                            return isFinite(C) ? String(C) : "null";
                        } else {
                            if (typeof C == "boolean") {
                                return String(C);
                            } else {
                                B = ["{"], G, E, F;
                                for (E in C) {
                                    if (!I || C.hasOwnProperty(E)) {
                                        F = C[E];
                                        if (F === null) {
                                            continue;
                                        }
                                        switch (typeof F) {
                                            case "undefined" :
                                            case "function" :
                                            case "unknown" :
                                                break;
                                            default :
                                                if (G) {
                                                    B.push(",");
                                                }
                                                B.push($.encode(E), ":", $.encode(F));
                                                G = true;
                                        }
                                    }
                                }
                                B.push("}");
                                return B.join("");
                            }
                        }
                    }
                }
            }
        }
    });
})();

 

posted @ 2013-09-16 15:36  relucent  阅读(1254)  评论(0编辑  收藏  举报